more examples for cin

This commit is contained in:
left_adjoint 2024-01-31 07:00:36 -08:00
parent 60c7ed5278
commit e86a7718ce
2 changed files with 45 additions and 0 deletions

19
cinTest.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
using namespace std;
int main(){
int num1;
int num2;
string str1;
cout << "Enter a couple of numbers" << endl;
cin >> num1;
cin >> num2;
if(cin.fail()){
cout << "Parsing two ints didn't work" << endl;
cin.clear();
cin >> str1;
cout << "Here's what's left over: " << str1 << endl;
}
return 0;
}

26
input3.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
int readInt(){
int ourInput;
cout << "Enter a whole number, only." << endl;
cin >> ourInput;
while(cin.fail() || cin.peek() == '.'){
cout << "Error: enter a whole number" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> ourInput;
}
return ourInput;
}
int main() {
int myInt1 = readInt();
int myInt2 = readInt();
cout << "Our numbers, added together, are... " << myInt1 + myInt2 << endl;
return 0;
}