Input to an addition problem

This is a discussion on Input to an addition problem within the C++ Programming forums, part of the General Programming Boards category; So I just started out with C++ and using Visual C++ Express 2008 and I am following the starter tutorials ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    Input to an addition problem

    So I just started out with C++ and using Visual C++ Express 2008 and I am following the starter tutorials on the site and one the second lesson. I would rewrite the code and change it so I can make sure I know. But I am completly stuck on one thing, I know that this was not in the lesson, but I tried it anyway and it failed, but I won't go on until I know this.

    I want to put the input for an answer of a + b, but I am not sure how to put. I knew cin<< a + b; wouldn't work but I tried anyways. Anyone can help me, or if you think is better and I should see a specific lesson.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a , b, c, d;
    
    	cout<<"I am going to test you! Just put random numbers, don't fail now!";
    	cin.ignore();
    	cout<<"Your first number: ";
    	cin>> a;
    	cin.ignore();
    	cout<<"Your second number: ";
    	cin>> b;
    	cin.ignore();
    	cout<<"Your third number: ";
    	cin>> c;
    	cin.ignore();
    	cout<<"Your forth number: ";
    	cin>> d;
    	cin.ignore();
    	cout<<"Q1 - What is "<< a <<" plus "<< b <<"\n";
    	cin>> a + b ;
    	if ( a + b == a + b ) {
    		cout<<"Good, next question.";
    	}
    	else { cout<<"You FAILED, NOW GTFO!!";
    	}
    	cin.get();
    }
    The code is not done for sure.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Store the user's answer in a variable, then compare that variable to a + b.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    OK, so just why did you think a + b would work?
    Normally, wouldn't you just store the result of an addition in another variable?
    Like

    int APlusB;
    cin >> APlusB;

    Hm?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    I am not sure why, I guess I am a stupid newbie, =]
    But I guess I wanted to do one without a variable, but I guess that that is impossible, thanks!
    Last edited by ShinobiRAGE; 08-27-2009 at 02:58 PM.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Ok, sorry for the double post, but is there a way to terminate the program if the user gets the wrong answer.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    And soon as the main function ends, the program terminates, so just exiting the function will terminate the program. To return from a function, you can use the return statement.
    Next you just have to incorporate it into your logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 10:41 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  4. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 10:50 AM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21