/* This program demonstrates * 1. Working with characters * 2. Conditional Operator * 3. Validating Use Input * 4. While loops * * 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 * * Input validation * When we get inputs from the user, we have to make sure the inputs make sense * from the perspective of the problem statement. For example, if we want to * perform division, the divisor must not be 0. * When we get inputs, we should not assume the user is aware of these limitations, * and check for violations. This is called algorithmic error checking. * There are different approaches to this, including, but not limited to: * a. Printing an error message * b. Correcting the wrong entries in the program * c. Asking the user to correct the wrong entry * d. Looping on an error input * * Loops: * Loops are repetitive statements. They execute the same lines of code * over an over again, as long as a certain condition is true. * The first type of loop is the while loop. "while" is a keyword. * A loop has 4 parts: initialization, condition, loop body and increment. * * 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. * */ #include using namespace std; int main() { /* Just because a char variable is called 'a' doesn't mean its value * is automatically 'a'. * We can read in chars with a cin * We can compare and work with chars just like ints and doubles as long as * char literals are enclosed in single quotes. */ char a = '!'; cout<<"a: "<>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 "<>inputGrade; /* validate user input - with an error message * If the grade is below 0 or above 100, print an error message. Otherwise, * check if the student passed */ cout<<"<=Method 1 : print an error message"< 100) { cout<<"Error. Invalid grade entered"<= 70) cout<<"Passing grade"< 100, set grade to 100 */ cout<<"Method 2 - default to value"<100) grade =100; if(grade >= 70) cout<<"Passing grade "<100) { cout<<"Error. Grade should be between 0 and 100. Enter the correct grade: "; cin>>inputGrade; } grade = inputGrade; if (grade >= 70) cout << "Passing grade" << endl; else cout << "Try again" << endl; /* Method 4 - Looping on an error input * We want an odd number, so we loop as long as the user keeps * entering even numbers. * Please note that the initial read, the check and the next read are * all done with the same variable */ cout<<"Method 4 of input validation - Looping on an error"<100) { cout<<"Error. Grade should be between 0 and 100. Enter the correct grade: "; cin>>grade; } if (grade >= 70) cout << "Passing grade" << endl; else cout << "Try again" << endl; /* Print the frst 200 natural numbers 1 - 200 * Step 1: Start at 1 * Step 2: Check if we're done/not done. If done, go to step 4 * Step 3: Print the number * Move to next number - increment * Go back to step 2 * Step 4: Move on */ cout<<"Before the while loop"<