Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Similar presentations


Presentation on theme: "Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."— Presentation transcript:

1 Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

2 More About Variable Definitions and Scope The scope of a variable is limited to the block in which it is defined. Usually we defined the variables at the top But, we can define them later on in the code // This program demonstrates late variable definition #include using namespace std; int main() { // Get the annual income. cout << "What is your annual income? "; double income; // Variable definition cin >> income; // Get the number of years at the current job. cout << "How many years have you worked at " << "your current job? "; int years; // Variable definition cin >> years; // Determine the user's loan qualifications. if (income >= 35000 || years > 5) cout << "You qualify.\n"; else { cout << "You must earn at least $35,000 or have\n"; cout << "been employed for more than 5 years.\n"; } return 0; }

3 What is scope? Scope of a variable is the part of the program where the variable may be used Variables defined inside a set of braces have a local, or block scope They may only be used in the part of the program between their definition and the block’s closing brace

4 // This program demonstrates a variable defined in an inner block. #include using namespace std; int main() { // Get the annual income. cout << "What is your annual income? "; double income; //variable definition cin >> income; if (income >= 35000) { // Get the number of years at the current job. cout << "How many years have you worked at " << "your current job? "; int years; //variable definition cin >> years; if (years > 5) cout << "You qualify.\n"; else { cout << "You must have been employed for\n"; cout << "more than 5 years to qualify.\n"; } else { cout << "You must earn at least $35,000 to\n"; cout << "qualify.\n"; } return 0; }

5 Variables with the same name // This program uses two variables with the name number. #include using namespace std; int main() { // Define a variable named number. int number; cout << "Enter a number greater than 0: "; cin >> number; if (number > 0) { int number; // Another variable named number. cout << "Now enter another number: "; cin >> number; cout << "The second number you entered was "; cout << number << endl; } cout << "Your first number was " << number << endl; return 0; }

6 Comparing Strings You must use the strcmp library function to compare C-strings You cannot use relational operators because of the way C-strings are stored in memory Use the strcmp function Strcmp(string1, string2) If the two strings are identical, strcmp returns 0 (false) If string1 < string 2, strcmp returns a negative If string1 > string2, strcmp returns a positive value

7 // This program correctly tests two C- strings for equality // with the strcmp function. #include using namespace std; int main() { const int SIZE = 40; char firstString[SIZE], secondString[SIZE]; // Get two strings cout << "Enter a string: "; cin.getline(firstString, SIZE); cout << "Enter another string: "; cin.getline(secondString, SIZE); // Compare them with strcmp. if (strcmp(firstString, secondString) == 0) cout << "You entered the same string twice.\n"; else cout << "The strings are not the same.\n"; return 0; }

8 // This program uses strcmp to compare the string entered // by the user with the valid stereo part numbers. #include using namespace std; int main() { const double APRICE = 249.0, // Price A BPRICE = 299.0; // Price B const int SIZE = 9; // Array size char partNum[SIZE]; // To hold the part number // Get a part number from the user. cout << "The stereo part numbers are:\n"; cout << "\tBoom Box, part number S147-29A\n"; cout << "\tShelf Model, part number S147-29B\n"; cout << "Enter the part number of the stereo you\n"; cout << "wish to purchase: "; cin.width(SIZE); // Restrict input for code safety. cin >> partNum; // Set the numeric output formatting. cout << fixed << showpoint << setprecision(2); // Determine and display the correct price. if (strcmp(partNum, "S147-29A") == 0) cout << "The price is $" << APRICE << endl; else if (strcmp(partNum, "S147-29B") == 0) cout << "The price is $" << BPRICE << endl; else cout << partNum << " is not a valid part number.\n"; return 0; }

9 Sorting Strings Because strcmp’s return value indicates which of the two strings is higher on the ASCII chart, it can be used in programs that sort strings // This program uses the return value of strcmp to alphabetically // sort two strings entered by the user. #include using namespace std; int main() { const int SIZE = 30; char name1[SIZE], name2[SIZE]; // Get the first name. cout << "Enter a name (last name first): "; cin.getline(name1, SIZE); // Get the second name. cout << "Enter another name: "; cin.getline(name2, SIZE); // Display them sorted in alphabetical order. cout << "Here are the names sorted alphabetically:\n"; if (strcmp(name1, name2) < 0) cout << name1 << endl << name2 << endl; else if (strcmp(name1, name2) > 0) cout << name2 << endl << name1 << endl; else cout << "You entered the same name twice!\n"; return 0; }

10 The Switch Statement The switch statement lets the value of a variable or expression determine where the program will branch switch(IntegerExpression) { case ConstantExpression: //place one or more statements here statements; break; case ConstantExpression: //place one or more statements here statements; break; // repeat as many case statements as necessary default: //place one or more statements here statements; }

11 // The switch statement in this program tells the user something // he or she already knows: what they just entered! #include using namespace std; int main() { char choice; cout << "Enter A, B, or C: "; cin >> choice; switch (choice) { case 'A': cout << "You entered A.\n"; break; case 'B': cout << "You entered B.\n"; break; case 'C': cout << "You entered C.\n"; break; default: cout << "You did not enter A, B, or C!\n"; } return 0; }

12 // This program is carefully constructed to use the "fallthrough" // feature of the switch statement. #include using namespace std; int main() { int modelNum; // Model number // Get a model number from the user. cout << "Our TVs come in three models:\n"; cout << "The 100, 200, and 300. Which do you want? "; cin >> modelNum; // Display the model's features. cout << "That model has the following features:\n"; switch (modelNum) { case 300: cout << "\tPicture-in-a- picture.\n"; case 200: cout << "\tStereo sound.\n"; case 100: cout << "\tRemote control.\n"; break; default: cout << "You can only choose the 100,"; cout << "200, or 300.\n"; } return 0; }

13 // The switch statement in this program uses the "fall through" // feature to catch both uppercase and lowercase letters entered // by the user. #include using namespace std; int main() { char feedGrade; // Get the desired grade of feed. cout << "Our pet food is available in three grades:\n"; cout << "A, B, and C. Which do you want pricing for? "; cin >> feedGrade; // Display the price. switch(feedGrade) { case 'a': case 'A': cout << "30 cents per pound.\n"; break; case 'b': case 'B': cout << "20 cents per pound.\n"; break; case 'c': case 'C': cout << "15 cents per pound.\n"; break; default: cout << "That is an invalid choice.\n"; } return 0; }

14 Useful for building menus // This program displays a menu and asks the user to make a // selection. An if/else if statement determines which item // the user has chosen. #include using namespace std; int main() { int choice; // Menu choice int months; // Number of months double charges; // Monthly charges // Constants for membership rates const double ADULT = 40.0; const double SENIOR = 30.0; const double CHILD = 20.0; // Display the menu and get a choice. cout << "\t\tHealth Club Membership Menu\n\n"; cout << "1. Standard Adult Membership\n"; cout << "2. Child Membership\n"; cout << "3. Senior Citizen Membership\n"; cout << "4. Quit the Program\n\n"; cout << "Enter your choice: "; cin >> choice; // Set the numeric ouput formatting. cout << fixed << showpoint << setprecision(2); // Respond to the user's menu selection. switch (choice) { case 1: cout << "For how many months? "; cin >> months; charges = months * ADULT; cout << "The total charges are $" << charges << endl; break; case 2: cout << "For how many months? "; cin >> months; charges = months * CHILD; cout << "The total charges are $" << charges << endl; break; case 3: cout << "For how many months? "; cin >> months; charges = months * SENIOR; cout << "The total charges are $" << charges << endl; break; case 4: cout << "Program ending.\n"; break; default: cout << "The valid choices are 1 through 4. Run the\n"; cout << "program again and select one of those.\n"; } return 0; }

15 Testing for File Open Errors When opening a file (for input or output), you can test the file stream object to determine if an error occurred. (Can be used with ifstream, ofstream, and fstream) ifstream inputFile; inputFile.open(“info.txt”); if(!inputFile) { cout << “Error opening file!\n”; } ifstream inputFile; inputFile.open(“info.txt”); if(inputFile.fail()) { cout << “Error opening file!\n”; }


Download ppt "Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220."

Similar presentations


Ads by Google