1.

Solve : Difficulty with adding?

Answer»

Hi,
I tried writing a simple code that adds two numbers, but things got a little funky... my code is:

Code: [Select]#include <iostream>

int main()
{
int num1;
int num2;
int CALC= num1 + num2;
std::cout << "Please choose two numbers to make your calculation\n";
std::cin >> num1;
std::cout << "Your first number is " << num1 << "!\n";
std::cin >> num2;
std::cout << "Your first number is " << num2 << "!\n";
std::cout << calc;

return 0;
}
When i choose two numbers, it gives me something that just doesnt make sense. For EXAMPLE if I put in 4 and 5 the calc becomes -944204840... any advice?Aren't you trying to make calc equal to 2 values that haven't been entered yet? In the third LINE of main() ? I know modern programming languages are smart but I don't think they include clairvoyance.


So you think i should define the variable after I INPUT the two values?I think you should define calc at the start and input the two values and finally make calc equal to the sum of the two values.
Thanks, that worked LIKE a charm #include <iostream>

int main()
{
int num1;
int num2;
int calc= num1 + num2;
std::cout << "Please choose two numbers to make your calculation\n";
std::cin >> num1;
std::cout << "Your first number is " << num1 << "!\n";
std::cin >> num2;
std::cout << "Your second number is " << num2 << "!\n";
calc = num1 + num2;
std::cout << calc;

return 0;
}



Please ignore above post.


#include <iostream>
int main()
{
int num1;
int num2;
int calc;
std::cout << "Please choose two numbers to make your calculation\n";
std::cin >> num1;
std::cout << "Your first number is " << num1 << "!\n";
std::cin >> num2;
std::cout << "Your second number is " << num2 << "!\n";
calc = num1 + num2;
std::cout << calc;
return 0;
}
one could also use "using namespace std" after the include and refer to cin and cout without the namespace qualifier.



Discussion

No Comment Found