{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec9 - Lecture 9 Getting Loopy PIC 10A Todd Wittman Sec 4.4...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 9: Getting Loopy PIC 10A Todd Wittman Sec 4.4 : Input Validation square6 So far we’ve assumed the user gives us proper input. But what if they don’t? Enter a number: four square6 When you do a cin statement, it actually returns a boolean: true if you were able to read the value correctly, false if it failed to read the specified type. square6 Returns the boolean true for a success, so we can use cin in an if statement. square6 Alternatively you could use the member function cin.fail( ) described in Sec 4.4.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Example: Input Validation int main ( ) { int x; cout << “Enter a number: “; if ( cin >> x ) { cout << “Your number is “ << x << “.\n”; return 0; } else { cout << “Supposed to be a number.”; return 1; } } This will end the program. Use the return command with caution. This line does 2 things: 1.) Gets input x from user. 2.) Checks if read successful. Sec 4.5 : The while loop square6 Sometimes it’s useful to repeat a block of code, like HW 3. square6 We call a block of code that is repeated a loop . square6 Each repetition of the code is called an iteration . square6 The while loop acts like a repeating if statement, repeating the code below it as long as the boolean statement is true. Also called a stopping condition . while ( statement is true ) { ** STATEMENTS ** } square6 The braces { } enclose the code to repeat. square6 Like with if , indenting makes it easier to read.
Image of page 2
3 Example of a while loop int x = -1; while ( x <= 0 ) { cout << “Enter a postive number: “; cin >> x; } cout << “Your number is “ << x << “.\n”;
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern