From e86a7718ce9dc1f65629291c5a592407da0d4ebe Mon Sep 17 00:00:00 2001 From: left_adjoint Date: Wed, 31 Jan 2024 07:00:36 -0800 Subject: [PATCH] more examples for cin --- cinTest.cpp | 19 +++++++++++++++++++ input3.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 cinTest.cpp create mode 100644 input3.cpp diff --git a/cinTest.cpp b/cinTest.cpp new file mode 100644 index 0000000..62a263d --- /dev/null +++ b/cinTest.cpp @@ -0,0 +1,19 @@ +#include +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; +} diff --git a/input3.cpp b/input3.cpp new file mode 100644 index 0000000..21943ec --- /dev/null +++ b/input3.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +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; +}