more examples for cin
This commit is contained in:
parent
60c7ed5278
commit
e86a7718ce
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue