User Tools

Site Tools


cplusplus:selection_examples

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cplusplus:selection_examples [2013/02/03 02:53] mithatcplusplus:selection_examples [2013/02/08 01:59] – [validating input] mithat
Line 15: Line 15:
  
     if (grade > 60)     if (grade > 60)
-        cout << "Passed!" << endl;+        cout << "Passed" << endl;
  
     return 0;     return 0;
Line 56: Line 56:
  
     cout << "The student received a letter grade of ";     cout << "The student received a letter grade of ";
-    if ( grade >= 90 )+    if (grade >= 90 )
         cout << "A";         cout << "A";
-    else if ( grade >= 80 )+    else if (grade >= 80)
         cout << "B";         cout << "B";
-    else if ( grade >= 70 )+    else if (grade >= 70)
         cout << "C";         cout << "C";
-    else if ( grade >= 60 )+    else if (grade >= 60)
         cout << "D";         cout << "D";
     else     else
         cout << "F";         cout << "F";
     cout << "." << endl;     cout << "." << endl;
 +
 +    return 0;
 +}
 +</file>
 +
 +===== validating input =====
 +
 +<file c++ validating_input_example.cpp>/* Using logical operators to validate input */
 +#include <iostream>
 +using namespace std;
 +
 +int main()
 +{
 +    int grade;
 +    cout << "Enter a numerical grade, and I will tell you" << endl;
 +    cout << "how well the student performed: ";
 +    cin >> grade;    // read an integer
 +
 +    if (grade >= 0 && grade <= 100)
 +    {
 +        cout << "The student received a letter grade of ";
 +        if (grade >= 90 )
 +            cout << "A";
 +        else if (grade >= 80)
 +            cout << "B";
 +        else if (grade >= 70)
 +            cout << "C";
 +        else if (grade >= 60)
 +            cout << "D";
 +        else
 +            cout << "F";
 +        cout << "." << endl;
 +    }
 +    else
 +        cout << "The grade must be between 0 and 100." << endl;
  
     return 0;     return 0;
 }</file> }</file>
  
cplusplus/selection_examples.txt · Last modified: 2013/02/11 01:00 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki