Take the 2-minute tour ×

I am building a simple calculator as a learning exercise and I have stumbled - I get the user input for the first number, but it fails to store the int for the second input - do I need to create objects? I assume this is an obvious problem...

//Simple calculator to work out the sum of two numbers (using addition)

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
    cout << "Enter the first int: \n";
    int input1 = std::cin.get();

    cout << "Enter the second int: \n";
    int input2 = std::cin.get();

    cout << "The sum of these numbers is: " << input1 + input2;
    cout << "\n";

    return EXIT_SUCCESS;
share|improve this question
how do u input number ? –  Krishna Sep 21 '12 at 9:42
add comment

1 Answer

up vote 9 down vote accepted

cin.get() only retrieves one character of input. Why not use

int input1, input2;
cin >> input1;
cin >> input2;

Using std::cin this way (with operator>>) also takes care of any surplus newline or space characters a user entered.

share|improve this answer
add comment

Your Answer


