cs161AWinter2024/input2.cpp

32 lines
782 B
C++

/*
Now we're going to abstract the idea of checking whether a number is correct into its own function
Otherwise this is going to be just like input1.cpp
*/
#include <iostream>
using namespace std;
int readInt(){
int ourInput;
cout << "Enter a whole number, only." << endl;
cin >> ourInput;
while(cin.fail()){
cout << "Nope, enter a number" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> ourInput;
}
cin.ignore(256,'\n'); // check what happens if you comment this line out and enter something like 10.3 as your first number, does it make sense why we need it?
return ourInput;
}
int main() {
int myInt = readInt();
int anotherInt = readInt();
cout << "Let's add our numbers together: " << myInt + anotherInt << endl;
return 0;
}