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 23:01]
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 
 +
 +
 +
 +-6 
 +
 +-7 
 +-7 
 +
 +-8 
 +-8 
 +
 +-9 
 +-9 
 +-9 
 +
 +Kevin 
 + */ 
 using namespace std; using namespace std;
- +
 int main() { int main() {
-  + 
- cout << "CIN:" << endl; +    cout << "Bad input:" << endl; 
-  +     
- // from last time: +    // This example builds on the bad input example at 
- int favorite_number;​ +    // end of the previous chapter. 
- /*cout << "​Please give me your favorite number:";​ +    /
- cin >> ​favorite_number;​ +    // First let's try values that are out of range. 
-  +    int favorite_number ​= 0
- cout << "\nHey, I know you, your favorite number is " ​<< favorite_number ​<< endl; +     
-  +    cout << "As you have seen in the reading an \"if\"" ​<< endl; 
- // try again but give "abc" as your favorite number. +    cout << ​"statement gives you a way to check for a condition:" ​ << endl
- // I wish we had a way to check for bad input and do some thing about it! +    cout << "​Please give me your favorite non-negative number(give me a negative number to test me) " ; 
-  +    cin >> ​favorite_number
- // we can do this with strings too +    cout << endl; 
- int users_name+    if (favorite_number < 0) cout << ​favorite_number ​<< " is negative and therefore bad input" ​<< endl
- cout << "​Please give me your first name: "; +     
- cin >> ​first_name+    cout << endl; 
-  +    cout << "We can also have and else part:" << endl; 
- cout << ​first_name ​<< " is a funny name!!\n"; +    cout << "​Please give me your favorite non-negative number(give me a negative number to test me) " ; 
-  +    cin >> ​favorite_number
- //But be careful with stings+    cout << endl; 
- cout << "​Please give me your full name: "; +    if (favorite_number < 0) cout << ​favorite_number ​<< " ​is negative and therefore bad input" ​<< endl
- cin >> ​full_name+    ​else ​cout << ​favorite_number ​<< " is good input!" ​<< endl
-  +     
- cout << ​full_name ​<< " ​really!!\n"; +    cout << ​endl
- cin >> last_name;​ +    cout << "Test the other side of the same if-else statement:" << endl; 
- cout << ​last_name ​<< " is the rest of it.\n"; +    cout << "​Please give me your favorite ​non-negative ​number: " ; 
-  +    cin >> favorite_number;​ 
- // back to numbers +    cout << endl; 
- // errors in cin +    if (favorite_number < 0) cout << favorite_number ​<< " is negative and therefore bad input" ​<< endl; 
- int interesting_number;​ +    else cout << favorite_number ​<< " is good input!" ​<< endl; 
- cout << ​"I am expecting a number, try something unexpected:";​ +     
- cin >> interesting_number+    cout << endl; 
- cout << "\nI saw: " ​<< interesting_number ​<< endl; +    cout << "​We ​could also exit the program on bad input:" << endl; 
- cout << "​What?​\n\nMaybe this will help, give bad input here:​\n";​ +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me (exits)) "; 
- cout << "​Please give me your favorite number:";​ +    cin >> favorite_number;​ 
- cin >> favorite_number;​ +    cout << endl; 
-  +    // WARNINGSTUPID CODE USED TO MAKE A POINT. DO NOT USE THIS! 
- cout << "\nHey, I know you, your favorite number ​is " << favorite_number << endl; +    if (favorite_number < 0) cout << favorite_number << " is negative and therefore bad input." << endl; 
-  +    if (favorite_number < 0) cout << ​"Exiting..." ​<< endl
- // I sure wish I could tell when input failed and do something about it!! +    if (favorite_number < 0) return 0; 
-  +    ​else ​cout << favorite_number ​<< " is good input!" << ​endl; 
-  +     
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)";​ +     
- cin >> favorite_number;​ +    cout << endl; 
-  +    cout << "Also from the reading c++ lets us group code with {}" << endl; 
- // WARNING STUPID CODE USED TO MAKE A POINT. DO NOT USE THIS! +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me (exits)) "; 
- if (favorite_number < 0) cout << favorite_number << "is negative and therefore bad input" << endl; +    cin >> favorite_number;​ 
- if (favorite_number < 0) system("pause")+    cout << endl; 
- if (favorite_number < 0) return 0; +    if (favorite_number < 0) { 
-  +        cout << favorite_number << " is negative and therefore bad input." << endl; 
- cout << "\nYou said your favorite number ​is " << ​favorite_number ​<< endl; +        cout << ​"Exiting..." ​<< endl
-  +        return 0; 
- // that was ugly, we need a way to group chucks of code. We have one: +    
-  +    ​else ​
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)";​ +        cout << favorite_number ​<< " is good input!" << ​endl; 
- cin >> favorite_number;​ +    } 
-  + 
- if (favorite_number < 0) { +    // Note the style used above. 
- cout << favorite_number << "is negative and therefore bad input" << endl; +     
- system("pause")+     
- return 0; +    cout << endl; 
-+    cout << "We can now responde more gracefully to bad input more gracefully and ask again:" ​<< endl
-  +    cout << endl; 
- // note {} style AND note that if-; is bad too! +     
-  +    ​cout << "​Please give me your favorite non-negative number: (give me a negative number to test me) "; 
- cout << "\nYou said your favorite number ​is " << ​favorite_number ​<< endl; +    cin >> favorite_number;​ 
-  +    cout << endl; 
- cout << "\nWe can now responde more gracefully to bad input:\n\n"; +    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: (give me a negative number to test me)";​ +        cout << "​Please give me your favorite NON-NEGATIVE number: "; 
- cin >> favorite_number;​ +        cin >> favorite_number;​ 
-  +        cout << endl; 
- if (favorite_number < 0) { +    ​
- cout << favorite_number << "is negative and therefore bad input" << endl; +    cout << "Your favorite number is " << favorite_number << endl; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +    // FYI you only need that much for your lab 
- cin >> favorite_number;​ + 
-+    cout << endl; 
-  +    ​cout << "What about the bad input the second time? We could:" ​<< endl
- cout << "\nYou said your favorite number is " << favorite_number << endl; +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me) "; 
- *+    cin >> favorite_number;​ 
- cout << "\nWhat ​about the bad input the second time? We could:\n\n"; +    cout << endl; 
-  +     
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)";​ +    // there are better ways to handle this, but notice that we can put groups in groups: 
- cin >> favorite_number;​ +    ​ 
-  +    if (favorite_number < 0) { 
- if (favorite_number < 0) { +        cout << favorite_number << " is negative and therefore bad input" << endl; 
- 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) "; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +        cin >> favorite_number;​ 
- cin >> favorite_number;​ +        cout << endl; 
- if (favorite_number < 0) { +        ​if (favorite_number < 0) { 
- cout << favorite_number << "is negative and therefore bad input" << endl; +            cout << favorite_number << " is negative and therefore bad input" << endl; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +            cout << "​Please give me your favorite NON-NEGATIVEnumber: "; 
- cin >> favorite_number;​ +            cin >> favorite_number;​ 
- +            cout << endl; 
-+        ​
-  +    
- cout << "\nYou said your favorite number is " << favorite_number << endl; +    cout << "You 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 << 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)";​ +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me) "; 
- cin >> favorite_number;​ +    cin >> favorite_number;​ 
-  +    cout << endl; 
- if (favorite_number < 0) { +    if (favorite_number < 0) { 
- cout << favorite_number << "is negative and therefore bad input" << endl; +        cout << favorite_number << " is negative and therefore bad input" << endl; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +        cout << "​Please give me your favorite NON-NEGATIVE number: ​(give me a negative number to test me) "; 
- cin >> favorite_number;​ +        cin >> favorite_number;​ 
-+        cout << endl; 
- if (favorite_number < 0) { +    ​
- cout << favorite_number << "is negative and therefore bad input" << endl; +    // is it still negative? 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +    ​if (favorite_number < 0) { 
- cin >> favorite_number;​ +        cout << favorite_number << " is negative and therefore bad input" << endl; 
-+        cout << "​Please give me your favorite NON-NEGATIVEnumber: "; 
-  +        cin >> favorite_number;​ 
- cout << "\nYou said your favorite number is " << favorite_number << endl; +        cout << endl; 
-  +    ​
- cout << "\nWhat ​about truely difficult:\n\n"; +    cout << "You said your favorite number is " << favorite_number << endl; 
-  +     
- cout << "​Please give me your favorite non-negative number: (give me a negative number to test me)";​ +    cout << endl; 
- cin >> favorite_number;​ +    ​cout << "What about truely difficult:" ​<< endl
-  +    cout << "​Please give me your favorite non-negative number: (give me a negative number to test me) "; 
- // ALSO STUPID CODE: +    cin >> favorite_number;​ 
- if (favorite_number < 0) { +    cout << endl; 
- cout << favorite_number << "is negative and therefore bad input" << endl; +    ​ 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +    // ALSO STUPID CODE: 
- cin >> favorite_number;​ +    if (favorite_number < 0) { 
-+        cout << favorite_number << " is negative and therefore bad input" << endl; 
- if (favorite_number < 0) { +        cout << "​Please give me your favorite NON-NEGATIVE number: ​(give me a negative number to test me) "; 
- cout << favorite_number << "is negative and therefore bad input" << endl; +        cin >> favorite_number;​ 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +        cout << endl; 
- cin >> favorite_number;​ +    ​
-+    if (favorite_number < 0) { 
- if (favorite_number < 0) { +        cout << favorite_number << " is negative and therefore bad input" << endl; 
- 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) "; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +        cin >> favorite_number;​ 
- cin >> favorite_number;​ +        cout << endl; 
-+    ​
- if (favorite_number < 0) { +    if (favorite_number < 0) { 
- cout << favorite_number << "is negative and therefore bad input" << endl; +        cout << favorite_number << " is negative and therefore bad input" << endl; 
- cout << "​Please give me your favorite NON-NEGATIVE number: "; +        cout << "​Please give me your favorite NON-NEGATIVE number: ​(give me a negative number to test me) "; 
- cin >> favorite_number;​ +        cin >> favorite_number;​ 
-+        cout << endl; 
-  +    ​
- // ... +    if (favorite_number < 0) { 
-  +        cout << favorite_number << " is negative and therefore bad input" << endl; 
- // We will have better ways of dealing with this when we do loops next week. +        cout << "​Please give me your favorite NON-NEGATIVE number: "; 
-  +        cin >> favorite_number;​ 
- cout << "\nYou said your favorite number ​is " << ​favorite_number ​<< endl; +        cout << endl; 
-  +    ​
- // compare stringstax nesting +    // ... 
-  +    cout << "You said your favorite number is " << favorite_number << endl; 
-  +    ​ 
- std::​system("pause")+    // We will have better ways of dealing with this when we do loops next week. 
- return 0;+     
 +     
 +    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 nameeither Kevin or Fred would have been great choices."​ << endl; 
 +         
 +    } 
 +    cout << ​"You entered ​" ​<< name << endl
 + 
 +     
 +    ​return 0;
 } }
 +
 </​code>​ </​code>​
cs-142/input-and-ifs.1473202910.txt.gz · Last modified: 2016/09/06 23:01 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