/* This program demonstrates * 1. Fall through and character inputs for a switch statement * 2. sizeof operator * 3. unsigned qualifier * 4. Working with characters * 5. Conditional Operator * 6. The while loop * 7. Looping on Error Input * * Using character variables * A char variable is an integer type, so it can be assigned integer values. However, * doing so might result in unexpected behavior, since the numbers translate to * different characters using an internal code (ASCII - to be covered later). * * To assign a character value to a char variable, we need to pace the character * in single quotes. * Single quotes char values can only be a single charcater or in certain cases, * an escaped character (ones that begin with \, followed by a character. * * Using if/switch statements with char variables * char variables might be used in boolean expressions just like ints and doubles. * The only difference is that chracter literals must be a single chracter or an * existing escape sequence, enclosed in single quotes. * * The Conditional operator * A simple if-else statement with ONLY ONE LINE in the if block and the * else block can be written using the conditional operator. * The conditional operator is the only ternary operator (operator with 3 * operands) we'll cover in this course. * This is the syntax: * (condition used for the if) ? line in the if block : line in the else block; * * while loops * while loops are the first type of loop. It checks the condition first. * If true, the loop body is executed. If false, it skips over the loop body * and goes to the line after the loop. * * Looping on Error Input * This continues on from the 3 methods of validating user input in * conditionals.cpp. Even if we ask the user to enter the corrected * value, there is no guarantee that the user would follow instructions. * It would be better if we can loop until the user gives us a value that * we can work with. * */ #include using namespace std; int main() { /* The following example will read in a character representing a month and print the * name of the month and the number of days in that month in a standard calendar year. * This demonstrates how to use a char variable in a switch statement. The case labels * need to be single quoted. */ char month; cout<<"Enter the month (letter): "; cin>>month; int daysInMonth=0; switch(month) { case 'F': daysInMonth=28; // February break; case 'A': // April case 'j': // June case 'S': // September case 'N': daysInMonth=30; // November break; case 'J': // January case 'M': // March case 'm': // May case 'U': // July, since both J's were taken already case 'a': // August case 'O': // October case 'D': daysInMonth=31; // December break; default: cout<<"Invalid month"<>a; cout<<"User entered: "<>x>>y>>z; (x>y) ? (x>z) ? largest = x : largest = z : (y>z) ? largest = y : largest = z ; cout<<"Largest number is "<>grade; while ( grade< 0 || grade > 100 ) // invert the condition for a "valid" grade { cout<<"You entered an invalid grade. Please enter a grade between 0 and 100: "; cin>>grade; // next read - increment } if (grade >= 70) cout<<"Pass"<