Week 3 (CS 200)
Table of Contents
Structs, branching, strings
1. Class schedule
- Tuesday: Boolean logic, If statements, Switch statements, Lab - program 1
- Thursday: Structs, Strings, Lab - programs 2 and 3
2. Assignment links
| Assignment | Reading link | Canvas link |
|---|---|---|
| 📚 Intro: Boolean logic | Read | Canvas quiz |
| 📚 Intro: If statements | Read | Canvas quiz |
| 📚 Intro: Switch statements | Read | Canvas quiz |
| 📚 Intro: Structs | Read | Canvas quiz |
| 📚 Intro: String library | Read | Canvas quiz |
| 💾 Lab: Branching, structs, and strings | Read | Canvas assignment |
| 💬 Discussion: User Interfaces | Canvas discussion | |
| 🙆 Update: Week 3 | Canvas assignment |
3. Example code
3.1. Branching
3.1.1. Standalone if: Absolute value
#include <iostream> using namespace std; int main() { float number; float absValue; cout << "Enter a value (positive or negative): "; cin >> number; absValue = number; if (number < 0) { absValue = -absValue; } cout << "The absolute value of " << number << " is " << absValue << endl; return 0; }
3.1.2. If/else: Card declined?
#include <iostream> // cin and cout using namespace std; // removes std:: prefix (std::cin, std::cout) int main() { float price; // The price of some item float balance; // The user's account balance cout << "What is your account balance: "; cin >> balance; cout << "How much does the item cost? "; cin >> price; // WAY 1: if ( price > balance ) // User doesn't have enough money { cout << "Card declined. :(" << endl; } else // User does have enough money { cout << "Card accepted! :)" << endl; balance = balance - price; } // WAY 2: if ( price <= balance ) // User does have enough money { cout << "Card accepted! :)" << endl; balance = balance - price; } else // User doesn't have enough money. { cout << "Card declined. :(" << endl; } cout << "Your balance is now: $" << balance << endl; return 0; }
3.1.3. If/else if: Pizza builder
#include <iostream> #include <iomanip> using namespace std; int main() { cout << "PIZZA HAT" << endl << endl; cout << "1. Cheese pizza" << endl; cout << "2. Pepperoni pizza" << endl; cout << "3. Pineapple + canadian Bacon pizza" << endl; cout << "4. Buffalo chicken pizza" << endl; cout << "5. Lotsa veggies pizza" << endl; cout << "6. Vegan veggies pizza" << endl; int pizzaChoice; cout << endl << "Which pizza would you like? "; cin >> pizzaChoice; const float BASE_PIZZA_PRICE = 9.00; const float VEGGIE_TOPPINGS_PRICE = 0.40; const float MEAT_TOPPINGS_PRICE = 0.50; const float CHEESE_PRICE = 0.30; const float SAUCE_PRICE = 0.25; float price; cout << fixed << setprecision( 2 ); if ( pizzaChoice == 1 ) // Cheese pizza { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Cheese ($" << CHEESE_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + CHEESE_PRICE; } else if ( pizzaChoice == 2 ) // Pepperoni pizza { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Cheese ($" << CHEESE_PRICE << ")" << endl; cout << " * Pepperoni ($" << MEAT_TOPPINGS_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + CHEESE_PRICE + MEAT_TOPPINGS_PRICE; } else if ( pizzaChoice == 3 ) // Pineapple / bacon pizza { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Cheese ($" << CHEESE_PRICE << ")" << endl; cout << " * Pineapple ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; cout << " * Canadian bacon ($" << MEAT_TOPPINGS_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + CHEESE_PRICE + VEGGIE_TOPPINGS_PRICE + MEAT_TOPPINGS_PRICE; } else if ( pizzaChoice == 4 ) // Buffalo chicken pizza { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Buffalo sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Cheese ($" << CHEESE_PRICE << ")" << endl; cout << " * Chicken ($" << MEAT_TOPPINGS_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + SAUCE_PRICE + CHEESE_PRICE + MEAT_TOPPINGS_PRICE; } else if ( pizzaChoice == 5 ) // Veggies { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Cheese ($" << CHEESE_PRICE << ")" << endl; cout << " * Tomato ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; cout << " * Black olives ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; cout << " * Mushrooms ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + CHEESE_PRICE + VEGGIE_TOPPINGS_PRICE + VEGGIE_TOPPINGS_PRICE + VEGGIE_TOPPINGS_PRICE; } else if ( pizzaChoice == 6 ) // Vegan { cout << "Pizza ($" << BASE_PIZZA_PRICE << ") and:" << endl; cout << " * Tomato sauce ($" << SAUCE_PRICE << ")" << endl; cout << " * Tomato ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; cout << " * Black olives ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; cout << " * Mushrooms ($" << VEGGIE_TOPPINGS_PRICE << ")" << endl; price = BASE_PIZZA_PRICE + SAUCE_PRICE + VEGGIE_TOPPINGS_PRICE + VEGGIE_TOPPINGS_PRICE + VEGGIE_TOPPINGS_PRICE; } cout << endl << "TOTAL PRICE: $" << price << endl; return 0; }
3.1.4. Nested if statement: Restaurant
#include <iostream> // cin and cout using namespace std; // removes std:: prefix (std::cin, std::cout) int main() { float price; int menuChoice, age; cout << "RESTAURANT" << endl; cout << "1. Hamburger ($10)" << endl; cout << "2. Fries ($5)" << endl; cout << "3. Beer ($12)" << endl; cout << "4. Soda ($4)" << endl; cout << "What do you want to order? "; cin >> menuChoice; if ( menuChoice == 1 ) { // hamburger price = 10; } else if ( menuChoice == 2 ) { // fries price = 5; } else if ( menuChoice == 3 ) { // beer cout << "What is your age? "; cin >> age; // Card the user if ( age >= 21 ) { price = 12; } else { cout << "You can't have any beer." << endl; price = 0; } } else if ( menuChoice == 4 ) { // soda price = 4; } cout << endl << "Price: $" << price << endl; return 0; }
3.1.5. Logic operators: Milk, eggs, and cheese
#include <iostream> using namespace std; int main() { bool milk, cheese, eggs; cout << "Do you have milk? (0 = false, 1 = true): "; cin >> milk; cout << "Do you have cheese? (0 = false, 1 = true): "; cin >> cheese; cout << "Do you have eggs? (0 = false, 1 = true): "; cin >> eggs; cout << endl; // Using AND if ( cheese && eggs ) { cout << "Make an cheese omelette" << endl; } else if ( milk && cheese ) { cout << "Make macaroni" << endl; } else { cout << "Go to the grocery store" << endl; } // Using OR if ( milk || cheese ) { cout << "You have dairy in the fridge" << endl; } else // Opposite of ( milk || cheese ) == !( milk || cheese ) { // == !milk && !cheese cout << "You have no dairy in the fridge" << endl; } return 0; }
3.1.6. Switch statement: Sandwich shop
#include <iomanip> #include <iostream> using namespace std; int main() { cout << "SANDWICH SHOP" << endl; cout << "1. Grilled Cheese" << endl; cout << "2. BLT" << endl; cout << "3. Turkey Club" << endl; cout << endl << "Your choice: "; int choice; cin >> choice; float price; cout << endl; switch (choice) { case 1: cout << "GRILLED CHEESE SELECTED" << endl; price = 5.99; break; case 2: cout << "BLT SELECTED" << endl; price = 6.95; break; case 3: cout << "TURKEY CLUB SELECTED" << endl; price = 6.50; break; default: cout << "INVALID SELECTION" << endl; } cout << fixed << setprecision(2); cout << endl << "Price: $" << price << endl; return 0; }
3.2. Structs
3.2.1. Basic struct: Menu items
#include <iostream> #include <iomanip> #include <string> using namespace std; struct MenuItem { string name; float price; }; int main() { MenuItem item1, item2, item3; item1.name = "Burrito"; item1.price = 1.29; item2.name = "Taco"; item2.price = 1.19; item3.name = "Quesadilla"; item3.price = 3.45; cout << fixed << setprecision(2); cout << "RESTAURANT" << endl << endl; cout << "1. $" << item1.price << "\t " << item1.name << endl; cout << "2. $" << item2.price << "\t " << item2.name << endl; cout << "3. $" << item3.price << "\t " << item3.name << endl; return 0; }
3.2.2. Structs in structs: Building and rooms
#include <iostream> #include <iomanip> #include <string> using namespace std; struct Room { string name; float width; float length; float area; }; struct Building { string name; float totalArea; Room room1; Room room2; Room room3; }; int main() { Building house; house.name = "House"; house.totalArea = 0; house.room1.name = "Livingroom"; house.room1.width = 15; house.room1.length = 20; house.room1.area = house.room1.width * house.room1.length; house.totalArea += house.room1.area; house.room2.name = "Bedroom"; house.room2.width = 12; house.room2.length = 16; house.room2.area = house.room2.width * house.room2.length; house.totalArea += house.room2.area; house.room3.name = "Bathroom"; house.room3.width = 6; house.room3.length = 10; house.room3.area = house.room3.width * house.room3.length; house.totalArea += house.room3.area; cout << "HOUSE INFORMATION" << endl << endl; cout << "Building: " << house.name << endl; cout << "Total SQFT: " << house.totalArea << endl; cout << endl << "ROOM 1: " << house.room1.name << endl; cout << "* width: " << house.room1.width << " ft" << endl; cout << "* length: " << house.room1.length << " ft" << endl; cout << "* area: " << house.room1.area << " sqft" << endl; cout << endl << "ROOM 2: " << house.room2.name << endl; cout << "* width: " << house.room2.width << " ft" << endl; cout << "* length: " << house.room2.length << " ft" << endl; cout << "* area: " << house.room2.area << " sqft" << endl; cout << endl << "ROOM 3: " << house.room3.name << endl; cout << "* width: " << house.room3.width << " ft" << endl; cout << "* length: " << house.room3.length << " ft" << endl; cout << "* area: " << house.room3.area << " sqft" << endl; return 0; }
3.3. Strings
3.3.1. Size
cout << "Please enter a string: "; string text; //cin >> text; // stops at spaces cin.ignore(); getline( cin, text ); // get a whole line of text (with spaces) cout << "There are " << text.size() << " characters in the string." << endl;
3.3.2. Subscript operator [ ]
cout << "Please enter a string: "; string text; cin.ignore(); getline( cin, text ); cout << "The first letter is " << text[0] << endl; cout << "The second letter is " << text[1] << endl; cout << "The third letter is " << text[2] << endl; if ( text[0] == 'H' ) { cout << "Are you typing Hello?" << endl; }
3.3.3. Find
string text = "Hello, world!"; cout << "What do you want to search for? "; string findme; cin.ignore(); getline( cin, findme ); int findPosition = text.find( findme ); if ( findPosition == string::npos ) { cout << "String not found" << endl; } else { cout << "String found at position " << findPosition << endl; }
3.3.4. Substring
string text = "Hello, world!"; cout << "Enter a starting position: "; int start; cin >> start; cout << "Enter a length: "; int length; cin >> length; string newString = text.substr( start, length ); cout << "The new string is: \"" << newString << "\"" << endl;
3.3.5. Compare
string string1, string2; cout << "Enter string 1: "; cin.ignore(); getline( cin, string1 ); cout << "Enter string 2: "; getline( cin, string2 ); int result = string1.compare( string2 ); if ( result == 0 ) { cout << "The strings are the same" << endl; } else if ( result < 0 ) { cout << string1 << " < " << string2 << endl; } else if ( result > 0 ) { cout << string1 << " > " << string2 << endl; }
3.3.6. Concatenate
string concatenate documentation
cout << "EXAMPLE 1" << endl; string string1, string2; cout << "Enter string 1: "; cin.ignore(); getline( cin, string1 ); cout << "Enter string 2: "; getline( cin, string2 ); string string3 = string1 + string2; cout << "Result: " << string3 << endl; cout << endl << "EXAMPLE 2" << endl; string text = "Hello"; cout << "Original text " << text << endl; cout << "What do you want to append to the text: "; string appendMe; getline( cin, appendMe ); text += appendMe; cout << "Text is now " << text << endl;
3.3.7. Insert
string text = "HelloWorld"; cout << "Original text: \"" << text << "\"" << endl; cout << "Enter a string to insert: "; string insertMe; cin.ignore(); getline( cin, insertMe ); cout << "Enter a position to insert at: "; int insertLocation; cin >> insertLocation; text = text.insert( insertLocation, insertMe ); cout << "New text: \"" << text << "\"" << endl;
3.3.8. Erase
string text = "HelloWorld"; cout << "Original text: \"" << text << "\"" << endl; cout << "Enter a position to begin erasing at: "; int start; cin >> start; cout << "Enter a length of chars to remove: "; int length; cin >> length; text = text.erase( start, length ); cout << "New text: \"" << text << "\"" << endl;
3.3.9. Replace
string text = "HelloWorld"; cout << "Original text: \"" << text << "\"" << endl; cout << "Enter a position to begin erasing at: "; int start; cin >> start; cout << "Enter a length of chars to remove: "; int length; cin >> length; cout << "Enter a string to insert at that position: "; string addMe; cin.ignore(); getline( cin, addMe ); text = text.replace( start, length, addMe ); cout << "New text: \"" << text << "\"" << endl;