#I improved my C++ calculator, I found out how to use functions.

1 messages · Page 1 of 1 (latest)

glacial crow
#

I forgot to make a system that checks if the second number is zero for division.

gentle rune
# glacial crow It doesn't have millions lines of code now.

You should generally avoid global variables, especially in a simple case like this.
You could, for example, write it like so:

struct Numbers {
    double first;
    double second;
};

Numbers number_input() {
    double first, second;
    std::cout << "Enter your two numbers\n";
    std::cin >> first >> second;
    return Numbers{first, second};
}

int main() {
    auto [first_number, second_number] = number_input();
    // Alternatively:
    // Numbers numbers = number_input();
    // double first_number = numbers.first;
    // double second_number = numbers.second;
    // Or just use the 'numbers.first' and 'numbers.second' directly
}
#

system("pause"); is also a bit sketchy, because I don't think that's a Linux command? Is that a Windows command?

#

And then, for the main function only, you don't need to explicitly put a return 0; at the end, but it's also fine to do so

mystic wraith
#

I suggest you add more binary operators and maybe some unary operations

gray fossil
#

globals are bad like what the other guy said

#

if you want to use it like how you did

#

i would do

#
void number_input(double& first_number, double& second_number)
{  
    std::cout << "\nEnter the first number: ";
    std::cin >> first_number;

    std::cout << "Enter the second number: ";
    std::cin >> second_number;
}
#

then you would use it like this:
number_input(x, y)z
and the first input would be assigned to x and the second to y

#

but nice job!

#

i would recommend you to add square roots and exponents next

glacial crow
#

thank you guys

molten hornet
#

@glacial crow you're better off using @gray fossil 's way - references are better.

solemn oracle
gray fossil
#

but a good point you have