From abe44e96d150bc379cf7f0898edc396d6b84064f Mon Sep 17 00:00:00 2001 From: left_adjoint Date: Mon, 5 Feb 2024 10:42:57 -0800 Subject: [PATCH] catching up on entries --- array1.cpp | 15 +++++++++++++++ badarray.cpp | 18 ++++++++++++++++++ cinfail.cpp | 21 +++++++++++++++++++++ cinfail2.cpp | 18 ++++++++++++++++++ cinfail3.cpp | 39 +++++++++++++++++++++++++++++++++++++++ whileCin1.cpp | 13 +++++++++++++ whileCin2.cpp | 37 +++++++++++++++++++++++++++++++++++++ whileCin3.cpp | 20 ++++++++++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 array1.cpp create mode 100644 badarray.cpp create mode 100644 cinfail.cpp create mode 100644 cinfail2.cpp create mode 100644 cinfail3.cpp create mode 100644 whileCin1.cpp create mode 100644 whileCin2.cpp create mode 100644 whileCin3.cpp diff --git a/array1.cpp b/array1.cpp new file mode 100644 index 0000000..88b4d0d --- /dev/null +++ b/array1.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main(){ + int intArray[10]; + + for(int i = 0; i < 10; i++){ + intArray[i] = i*i; + } + for(int i = 0; i < 10; i++){ + cout << "The " << i << "th thing is: " << intArray[i] << endl; + } + + return 0; +} \ No newline at end of file diff --git a/badarray.cpp b/badarray.cpp new file mode 100644 index 0000000..d92b97b --- /dev/null +++ b/badarray.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main(){ + int myArray[10]; + for(int i = 0; i < 10; i++){ + myArray[i] = i*i + 1; + } + cout << myArray[0] << endl; + cout << myArray[1] << endl; + //... + + myArray[200] = 12345; + cout << myArray[200] << endl; + + cout << "What is the value of the array variable: " << myArray << endl; + return 0; +} \ No newline at end of file diff --git a/cinfail.cpp b/cinfail.cpp new file mode 100644 index 0000000..6f8430b --- /dev/null +++ b/cinfail.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main(){ + + int num1; + int num2; + string rest; + + cout << "Enter two numbers, separated by a space: "; + cin >> num1; + cin >> num2; + if(cin.fail()){ + cout << "oh, it failed" << endl; + cin.clear(); + cin >> rest; + cout << "The rest was: " << rest << endl; + } + + return 0; +} \ No newline at end of file diff --git a/cinfail2.cpp b/cinfail2.cpp new file mode 100644 index 0000000..04dea11 --- /dev/null +++ b/cinfail2.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main(){ + int num; + cout << "Enter a whole number: "; + cin >> num; + while(cin.fail() || cin.peek() == '.'){ + cout << "Whoops. Enter a whole number: "; + cin.clear(); + cin.ignore(256,'\n'); + //cout << "Next char is: " << cin.peek() << endl; + cin >> num; + } + cout << "You entered: " << num << endl; + + return 0; +} \ No newline at end of file diff --git a/cinfail3.cpp b/cinfail3.cpp new file mode 100644 index 0000000..7dc6c29 --- /dev/null +++ b/cinfail3.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int parseInt(){ + int num; + cout << "Enter a whole number: "; + cin >> num; + while(cin.fail() || cin.peek() == '.'){ + cout << "Whoops. Enter a whole number: "; + cin.clear(); + cin.ignore(256,'\n'); + //cout << "Next char is: " << cin.peek() << endl; + cin >> num; + } + return num; +} + +double parseDouble(){ + double num; + cout << "Enter a number: "; + cin >> num; + while(cin.fail()){ + cout << "Whoops. Enter a number: "; + cin.clear(); + cin.ignore(256,'\n'); + //cout << "Next char is: " << cin.peek() << endl; + cin >> num; + } + return num; +} + +int main(){ + int num = parseInt(); + cout << "You entered: " << num << endl; + double dubstep = parseDouble(); + cout << "Here's your double: " << dubstep << endl; + + return 0; +} \ No newline at end of file diff --git a/whileCin1.cpp b/whileCin1.cpp new file mode 100644 index 0000000..bff4150 --- /dev/null +++ b/whileCin1.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main(){ + string input = ""; + while(input != "chicken"){ + cout << "Say the magic word: "; + cin >> input; + } + cout << "You said it!" << endl; + + return 0; +} \ No newline at end of file diff --git a/whileCin2.cpp b/whileCin2.cpp new file mode 100644 index 0000000..887bbb4 --- /dev/null +++ b/whileCin2.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main(){ + string input=""; + bool yOrN; + + // && "and", returns true if left and right are true + // || "or", returns true if left or right are true + // ! "not", ! true = false, ! false = true + // ! (a || b) <--> !a && !b + // Haskell: or $ map (==input) ["Yes,"No","y","n"] +/* while(! (input == "Yes" || input == "y" || input == "No" || input == "n")){ + cout << "Enter an option (Yes/y/No/n): "; + cin >> input; + } +*/ + while(input != "Yes" && input != "y" && input != "No" && input != "n"){ + cout << "Enter an option (Yes/y/No/n): "; + cin >> input; + } + + /* + if(input == "Yes" || input == "y"){ + yOrN = true; + } + else{ + yOrN = false; + } +*/ + yOrN = (input == "Yes" || input == "y"); + // will print 1 if yOrN = true + // will print 0 if yOrN = false + cout << "Testing our boolean: " << yOrN << endl; + + return 0; +} \ No newline at end of file diff --git a/whileCin3.cpp b/whileCin3.cpp new file mode 100644 index 0000000..36830ca --- /dev/null +++ b/whileCin3.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main(){ + + int ourNum; + int ourNum2; + string ourString; + + cout << "Enter numbers: "; + cin >> ourNum; + cin >> ourNum2; + // cin >> ourString; + + cout << "You wrote: " << ourNum << " " << ourNum2 << endl; + + + + return 0; +} \ No newline at end of file