#I improved my C++ calculator, I found out how to use functions.
1 messages · Page 1 of 1 (latest)
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
I suggest you add more binary operators and maybe some unary operations
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
thank you guys
@glacial crow you're better off using @gray fossil 's way - references are better.
Tbh, we could have a major discussion here. In general they are bad, unless you have a valid reason for them. It's the type of thing that the rule of thumb is "it's bad" but in reality it's more of an "it depends"
yes correct, the usage here tho is bad
but a good point you have