Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs-142:input-and-ifs [2016/09/06 22:28]
kseppi
cs-142:input-and-ifs [2016/09/12 20:14] (current)
kseppi
Line 1: Line 1:
 <code cpp> <code cpp>
 #​include<​iostream>​ #​include<​iostream>​
-#​include<​iomanip>​ 
 #​include<​string>​ #​include<​string>​
 +
 +/* Try:
 +-1
 +-2
 +3
 +4
 +5
 +-6
 +6
 +-7
 +-7
 +7
 +-8
 +-8
 +8
 +-9
 +-9
 +-9
 +9
 +Kevin
 + */
  
 using namespace std; using namespace std;
  
 int main() { int main() {
-  
- cout << "​CIN:"​ << endl; 
-  
- // from last time: 
- int favorite_number;​ 
- /*cout << "​Please give me your favorite number:";​ 
- cin >> favorite_number;​ 
  
- cout << "\nHey, I know youyour favorite number is " << favorite_number << endl;+    ​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 = 0; 
 +     
 +    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; 
 +    }
  
- // try again but give "abc" ​as your favorite number. +    ​// Note the style used above. 
- // I wish we had a way to check for bad input and do some thing about it!+     
 +     
 +    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; 
 +    // FYI you only need that much for your lab
  
- // we can do this with strings too +    cout << endl; 
- int users_name+    cout << "What about the bad input the second time? We could:"​ << endl; 
- cout << "​Please give me your first name: "; +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me) "; 
- cin >> ​first_name;+    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. 
 +     
 +     
 +    string ​name = "";​ 
 +     
 +    cout << endl; 
 +    cout << "We can also do similar things with text:" ​<< endl
 +    cout << "​Please enter an name: (try Kevin or Fred) "; 
 +    ​cin >> ​name; 
 +    cout << endl; 
 +     
 +    if ((name != "​Kevin"​) && (name != "​Fred"​)){ 
 +        cout << name << " is a fine name, but Kevin or Fred would have been better."​ << endl; 
 +        cout << "​Please enter a name "; 
 +        cin >> name; 
 +        cout << endl; 
 +    } 
 +    else { 
 +        cout << name << " is a fine name, either Kevin or Fred would have been great choices."​ << endl; 
 +         
 +    } 
 +    cout << "You entered " << name << endl;
  
- cout << first_name << " is a funny name!!\n"​;+     
 +    return 0; 
 +}
  
- //But be careful with stings: 
- cout << "​Please give me your full name: "; 
- cin >> full_name; 
- 
- cout << full_name << " really!!\n";​ 
- cin >> last_name; 
- cout << last_name << " is the rest of it.\n";​ 
- 
- // back to numbers 
- // errors in cin 
- int interesting_number;​ 
- cout << "I am expecting a number, try something unexpected:";​ 
- cin >> interesting_number;​ 
- cout << "\nI saw: " << interesting_number << endl; 
- cout << "​What?​\n\nMaybe this will help, give bad input here:​\n";​ 
- cout << "​Please give me your favorite number:";​ 
- cin >> favorite_number;​ 
- 
- cout << "​\nHey,​ I know you, your favorite number is " << favorite_number << endl; 
- 
- // I sure wish I could tell when input failed and do something about it!! 
-  
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- // 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) system("​pause"​);​ 
- if (favorite_number < 0) return 0; 
- 
- cout << "\nYou said your favorite number is " << favorite_number << endl; 
- 
- // that was ugly, we need a way to group chucks of code. We have one: 
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- if (favorite_number < 0) { 
- cout << favorite_number << "is negative and therefore bad input" << endl; 
- system("​pause"​);​ 
- return 0; 
- } 
- 
- // note {} style AND note that if-; is bad too! 
- 
- cout << "\nYou said your favorite number is " << favorite_number << endl; 
- 
- cout << "\nWe can now responde more gracefully to bad input:​\n\n";​ 
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- 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 << "\nYou said your favorite number is " << favorite_number << endl; 
- */ 
- cout << "​\nWhat about the bad input the second time? We could:​\n\n";​ 
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- 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;​ 
- 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 << "\nYou said your favorite number is " << favorite_number << endl; 
- 
- cout << "​\nThis is NOT the same as the following, but it has the same effect IN THIS CASE:​\n\n";​ 
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- 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;​ 
- } 
- 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 << "\nYou said your favorite number is " << favorite_number << endl; 
- 
- cout << "​\nWhat about truely difficult:​\n\n";​ 
- 
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)"; 
- cin >> favorite_number;​ 
- 
- // 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: "; 
- cin >> favorite_number;​ 
- } 
- 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;​ 
- } 
- 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;​ 
- } 
- 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;​ 
- } 
- 
- // ... 
- 
- // We will have better ways of dealing with this when we do loops next week. 
- 
- cout << "\nYou said your favorite number is " << favorite_number << endl; 
- 
- // compare strings, tax nesting 
- 
- 
- std::​system("​pause"​);​ 
- return 0; 
-} 
 </​code>​ </​code>​
cs-142/input-and-ifs.1473200921.txt.gz · Last modified: 2016/09/06 22:28 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