Thread: Problem with simple program

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6

    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
    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

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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 )

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6
    Thanks for your reply.
    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...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM