Reputation: 469
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";
system("PAUSE");
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 478
Reputation: 11379
cin.get()
only retrieves one character of input. Why not use
int input1, input2;
cout << "Enter the first int: \n";
cin >> input1;
cout << "Enter the second int: \n";
cin >> input2;
Using std::cin
this way (with operator>>
) also takes care of any surplus newline or space characters a user entered.
Upvotes: 9