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;
}
cin.get()only retrieves one character of input. Why not useUsing
std::cinthis way (withoperator>>) also takes care of any surplus newline or space characters a user entered.