**This is an old revision of the document!**


#include<iostream>
 
/* Try:
-1
-2
3
4
5
-6
6
-7
-7
7
-8
-8
8
-9
-9
-9
9
 */
 
using namespace std;
 
int main() {
 
    cout << "Bad input:" << endl;
 
    // This example builds on the bad input example at
    // end of the previous chapter.
    //
    // First let's try values that are out of range.
    int favorite_number;
    cout << "As you have seen in the reading an \"if\"" << endl;
    cout << "statement gives you a way to check for a condition:"  << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) " ;
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) cout << favorite_number << " is negative and therefore bad input" << endl;
 
    cout << endl;
    cout << "We can also have and else part:" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) " ;
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) cout << favorite_number << " is negative and therefore bad input" << endl;
    else cout << favorite_number << " is good input!" << endl;
 
    cout << endl;
    cout << "Test the other side of the same if-else statement:" << endl;
    cout << "Please give me your favorite non-negative number: " ;
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) cout << favorite_number << " is negative and therefore bad input" << endl;
    else cout << favorite_number << " is good input!" << endl;
 
    cout << endl;
    cout << "We could also exit the program on bad input:" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me (exits)) ";
    cin >> favorite_number;
    cout << endl;
    // WARNING: STUPID CODE USED TO MAKE A POINT. DO NOT USE THIS!
    if (favorite_number < 0) cout << favorite_number << " is negative and therefore bad input." << endl;
    if (favorite_number < 0) cout << "Exiting..." << endl;
    if (favorite_number < 0) return 0;
    else cout << favorite_number << " is good input!" << endl;
 
 
    cout << endl;
    cout << "Also from the reading c++ lets us group code with {}" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me (exits)) ";
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input." << endl;
        cout << "Exiting..." << endl;
        return 0;
    }
    else {
        cout << favorite_number << " is good input!" << endl;
    }
 
    // Note the style used above.
 
 
    cout << endl;
    cout << "We can now responde more gracefully to bad input more gracefully and ask again:" << endl;
    cout << endl;
 
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) ";
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input, try again." << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: ";
        cin >> favorite_number;
        cout << endl;
    }
    cout << "Your favorite number is " << favorite_number << endl;
 
    cout << endl;
    cout << "What about the bad input the second time? We could:" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) ";
    cin >> favorite_number;
    cout << endl;
 
    // there are better ways to handle this, but notice that we can put groups in groups:
 
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: (give me a negative number to test me) ";
        cin >> favorite_number;
        cout << endl;
        if (favorite_number < 0) {
            cout << favorite_number << " is negative and therefore bad input" << endl;
            cout << "Please give me your favorite NON-NEGATIVE! number: ";
            cin >> favorite_number;
            cout << endl;
        }
    }
    cout << "You said your favorite number is " << favorite_number << endl;
 
    cout << endl;
    cout << "This is NOT the same as the following, but it has the same effect IN THIS CASE:" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) ";
    cin >> favorite_number;
    cout << endl;
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: (give me a negative number to test me) ";
        cin >> favorite_number;
        cout << endl;
    }
    // is it still negative?
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE! number: ";
        cin >> favorite_number;
        cout << endl;
    }
    cout << "You said your favorite number is " << favorite_number << endl;
 
    cout << endl;
    cout << "What about truely difficult:" << endl;
    cout << "Please give me your favorite non-negative number: (give me a negative number to test me) ";
    cin >> favorite_number;
    cout << endl;
 
    // ALSO STUPID CODE:
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: (give me a negative number to test me) ";
        cin >> favorite_number;
        cout << endl;
    }
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: (give me a negative number to test me) ";
        cin >> favorite_number;
        cout << endl;
    }
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: (give me a negative number to test me) ";
        cin >> favorite_number;
        cout << endl;
    }
    if (favorite_number < 0) {
        cout << favorite_number << " is negative and therefore bad input" << endl;
        cout << "Please give me your favorite NON-NEGATIVE number: ";
        cin >> favorite_number;
        cout << endl;
    }
    // ...
    cout << "You said your favorite number is " << favorite_number << endl;
 
    // We will have better ways of dealing with this when we do loops next week.
 
 
 
    return 0;
}
cs-142/input-and-ifs.1473694764.txt.gz · Last modified: 2016/09/12 15:39 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0