From 54a0bff29a98e391d34648e767a22e092df6f7b2 Mon Sep 17 00:00:00 2001 From: left_adjoint Date: Wed, 28 Feb 2024 12:06:02 -0800 Subject: [PATCH] catching up with past lecture examples --- mistakes1.cpp | 28 ++++++++++++++++++++++++++++ nestedfor.cpp | 17 +++++++++++++++++ switch2.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ switchLoop.cpp | 26 ++++++++++++++++++++++++++ switchswitch.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 mistakes1.cpp create mode 100644 nestedfor.cpp create mode 100644 switch2.cpp create mode 100644 switchLoop.cpp create mode 100644 switchswitch.cpp diff --git a/mistakes1.cpp b/mistakes1.cpp new file mode 100644 index 0000000..e8c4878 --- /dev/null +++ b/mistakes1.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int main(){ + int num = 0; + + cout << "Enter an integer" << endl; + cin >> num; + + //while(num = 1){ + // cout << "Enter a non-one number: "; + + // cin >> num; + //} + + //cout << "What does assignment return?: " << (num = 10) << endl; + + if(num == 0 && true){ + cout << "Print true" << endl; + } + else { + cout << "Print false" << endl; + } + + cout << "You got out" << endl; + return 0; + +} \ No newline at end of file diff --git a/nestedfor.cpp b/nestedfor.cpp new file mode 100644 index 0000000..2a4e9b0 --- /dev/null +++ b/nestedfor.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main(){ + int count = 0; + for(int i=0; i < 8; i++){ + for(int j=0; j < 9; j++){ + for(int k=0; k < 10; k++){ + count = count + 1; + } + } + } + + cout << "Loops ran: " << count << " times" << endl; + + return 0; +} \ No newline at end of file diff --git a/switch2.cpp b/switch2.cpp new file mode 100644 index 0000000..dd24aa0 --- /dev/null +++ b/switch2.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main(){ + char opt; + cout << "Welcome: type an option and hit enter to proceed" << endl; + + double grade = 3.2; + string studentID = "ccl1234"; + bool hasBribed = true; + + cout << "(a)" << "Check grade " << endl; + cout << "(b)" << "Student ID" << endl; + cout << "(c)" << "Has Bribed Professor" << endl; + + cin >> opt; + + switch(opt){ + case 'a': + cout << "Your grade is: " << grade << endl; + break; + case 'b': + cout << "Your student ID is: " << studentID << endl; + break; + case 'c': + cout << "Have you gamed the system?: "; + if(hasBribed){ + cout << "The scandal of it all" << endl; + } + else{ + cout << "You have regained humanity" << endl; + } + break; + default: + cout << "Sorry I didn't understand that" << endl; + } + + return 0; + +} \ No newline at end of file diff --git a/switchLoop.cpp b/switchLoop.cpp new file mode 100644 index 0000000..35c5b8d --- /dev/null +++ b/switchLoop.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main(){ + char option; + + cout << "Enter any letter (but q exits)" << endl; + cin >> option; + + while(option != 'q'){ + switch(option){ + case 'a': + cout << "You pressed a" << endl; + break; + case 'b': + cout << "Bee is a bug" << endl; + break; + default: + cout << "That's a nice letter too" << endl; + } + cout << "Enter any letter (but q exits)" << endl; + cin >> option; + } + + return 0; +} \ No newline at end of file diff --git a/switchswitch.cpp b/switchswitch.cpp new file mode 100644 index 0000000..e00d3d0 --- /dev/null +++ b/switchswitch.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +int main(){ + + char option; + char option2; + + cout << "Enter a letter: "; + cin >> option; + cout << "Enter another: "; + cin >> option2; + + switch(option){ + case 'a': + switch(option2){ + case 'a': + cout << "aaaaaaaa" << endl; + break; + case 'b': + cout << "bbbbbbbb" << endl; + break; + default: + cout << "I don't know what that means" << endl; + break; + } + //break; + case 'b': + switch(option2){ + case 'c': + cout << "ccccccc" << endl; + break; + default: + cout << "Nope" << endl; + } + break; + default: + cout << "Why" << endl; + } + + return 0; +} \ No newline at end of file