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/12 15:39]
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<​string>​
  
 /* Try: /* Try:
Line 20: Line 21:
 -9 -9
 9 9
 +Kevin
  */  */
  
Line 25: Line 27:
  
 int main() { int main() {
-    ​+
     cout << "Bad input:"​ << endl;     cout << "Bad input:"​ << endl;
     ​     ​
Line 32: Line 34:
     //     //
     // First let's try values that are out of range.     // First let's try values that are out of range.
-    int favorite_number;​+    int favorite_number ​= 0; 
 +    ​
     cout << "As you have seen in the reading an \"​if\""​ << endl;     cout << "As you have seen in the reading an \"​if\""​ << endl;
     cout << "​statement gives you a way to check for a condition:" ​ << endl;     cout << "​statement gives you a way to check for a condition:" ​ << endl;
Line 99: Line 102:
     }     }
     cout << "Your favorite number is " << favorite_number << endl;     cout << "Your favorite number is " << favorite_number << endl;
 +    // FYI you only need that much for your lab
  
     cout << endl;     cout << endl;
Line 179: Line 183:
     ​     ​
     ​     ​
 +    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;
 +
     ​     ​
     return 0;     return 0;
 } }
 +
 </​code>​ </​code>​
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