# Problem with simple program

• 02-28-2010
geoffrey
Problem with simple program
Hi guys,
I was wondering if you can help me with that. I wrote a simple program (I have just started learning C++) but it doesn't work as expected. Here is the program and I will explain what I would like to do and what happens when I execue it:

Code:

```//seventh first #include <iostream> using std::cout; using std::cin; using std::endl; class Maths { public: void enterInstructions( int instructions )         {         cout <<"I want to do a: \nAddition\nSubtraction\nType your choice here: "<< instructions << endl;         } void conditions(int instructions)         {         int result;         int Addition;         int Subtraction;         if ( instructions == Addition )         cout << "Enter the right number: 9+3= "<< endl;         cin >> result;         if ( result == 12 )         cout << "Well done, you're a star!";         if ( result != 12 )         cout << "Sorry, wrong answer!";         if ( result < 12 )         cout << "Sorry, wrong answer!";         if ( result > 12 )         cout << "Sorry, wrong answer!";         if ( instructions == Subtraction )         cout << "Enter the right number: 9-3= ";         cin.ignore ( 80, '\n' );         cin >> result;                 if ( result == 6 )         cout << "Well done, you're a star!";         if ( result != 6 )         cout << "Sorry, wrong answer!";         if ( result < 6 )         cout << "Sorry, wrong answer!";         if ( result > 6 )         cout << "Sorry, wrong answer!";         } }; int main() { int myInstructions; Maths operations; cout << "What do you want to do?\n"; operations.enterInstructions( myInstructions ); cin >> myInstructions; operations.conditions( myInstructions ); return 0; }```
Basically I would like this program to let me choose whether I want to guess the result of an addition or a subtraction. Once I made my choice the program should display the addition/subtraction and I should be able to guess the result. If I guess it right the "Well done, you're a star!" messages appear, if not the "Sorry, wrong answer!" message comes up instead.
Now here is what happens. I use Visual C++ as a compiler and when I compile the program it does it with no errors but 3 warning as below:

--------------------Configuration: 10 - Win32 Debug--------------------
Compiling...
10th.cpp
Z:\example\10\10th.cpp(53) : warning C4700: local variable 'myInstructions' used without having been initialized
Z:\example\10\10th.cpp(19) : warning C4700: local variable 'Addition' used without having been initialized
Z:\example\10\10th.cpp(31) : warning C4700: local variable 'Subtraction' used without having been initialized

10.exe - 0 error(s), 3 warning(s)

When I execute the program from the compiler this is what the terminal returns:

What do you want to do?
I want to do a:
Subtraction

I can't explain the above number...
then if I type Subtraction, here's what the terminal display:

What do you want to do?
I want to do a:
Subtraction
Subtraction
Enter the right number: 9+3=

So basically the addition comes up even if I chose subtraction, but what's worse is that it doesn't let me type the result of the operation, it displays straight away "Sorry wrong answer". I can't understand what I have done wrong...could anybody suggest anything?
Thanks
• 02-28-2010
Memloop
You are using variables without initializing them to anything.

For example, what exactly do you expect Addition to be here? You haven't assigned it a value yet, so how can you use it to compare it against anything?
Code:

```int Addition;         int Subtraction;         if ( instructions == Addition )```
• 02-28-2010
geoffrey
Uhm, I didn't know I had to initialize them. The thing is I would like the condition to be largely this: if the parameter instructions = addition then display "Enter the right number: 9+3= ", if instructions = subtraction then display "Enter the right number: 9-3= ". Now, if I need to initialize the "Addition", "Subtraction", "Results" and "myInstructions" - in main - variables, what initial value am I suppose to give them? I have written other programs without the need of initializing a variable, so I am a bit confused on this...
• 02-28-2010
Memloop
When I say initialize, I meant that you should assign the variable a value before you use it.

For example, you have a lot of code that does this:
Code:

```int number; cout << number << endl;```
What is cout going to print here? Who knows, it's going to be a random number that is currently stored at the memory location that the variable number occupies.
• 02-28-2010
geoffrey
Ok I see what you mean in your example. But if I add
Code:

```int number; cin >> number; cout << number << endl;```
then I don't need to initialize the variable anymore, because it will take the value from what the user will type in...or am I wrong?

In my case, I understand that those variables need to be initialised but I can't understand what value they should have if I want the program to execute properly. If I give them an initial value, it will not change during the program execution, because "addition", "subtraction" and "myInstructions" don't actually take any value. So, maybe I got things wrong, sorry. If I want a user to be able to make a choice between addition and subtraction, and the program to be able to present him with respectively "Enter the right number: 9+3= " or "Enter the right number: 9-3= " depending on his choice then I shouldn't use that code?