Getting a crazy number??

Why doesnt this work? The problem lies where i declare answer3. When i build and run this it gives me some outrageous number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;


int main()
{
    
   int num1; 
   int num2; 
   int answer3 = num1 * num2;
   
   cout << "Enter first number:" << endl;
   cin >> num1;
   cout << "Enter second number:" << endl;
   cin >>  num2;
   
   cout << "Here's the answer" << answer3 << endl;

   
}
You are setting answer3 equal to num1 * num2 when neither of them have any values assigned to them at that point, so it just gives you a random number instead.

Move
int answer3 = num1 * num2;

So it is after the user enters num1 and num2.
oh so it would work if i moved that below my cin>>num1; and cin>>num2;?
Yep. Then the compiler would know what numbers to use.
Is this because it reads top down?
Correct, C++ reads left to right and top to bottom just like a person would. So you always need to declare/define things before you use them.
Topic archived. No new replies allowed.