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:
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.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; }
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
Linking...
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:
Addition
Subtraction
Type your choice here: -858993460
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:
Addition
Subtraction
Type your choice here: -858993460
Subtraction
Enter the right number: 9+3=
Sorry, wrong answer!Sorry, wrong answer!Enter the right number: 9-3= Sorry, wron
g answer!Sorry, wrong answer!Press any key to continue
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



LinkBack URL
About LinkBacks


