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
cplusplus:selection_examples [2013/02/03 04:57] mithatcplusplus:selection_examples [2013/02/11 01:00] (current) – [switch] mithat
Line 85: Line 85:
     cin >> grade;    // read an integer     cin >> grade;    // read an integer
  
-    if (grade >= 0 && grade <=100)+    if (grade >= 0 && grade <= 100)
     {     {
         cout << "The student received a letter grade of ";         cout << "The student received a letter grade of ";
Line 105: Line 105:
     return 0;     return 0;
 }</file> }</file>
 +
 +===== comparing characters =====
 +
 +<file c++ comparing_characters.cpp>/* Comparing characters */
 +#include <iostream>
 +using namespace std;
 +
 +int main()
 +{
 +   char ch;
 +
 +   // Get a character from the user.
 +   cout << "Enter a letter: ";
 +   ch = cin.get();
 +
 +   // Did user enter a letter?
 +   if (ch >= 'a' && ch <= 'z')
 +      cout << "You entered a character between 'a' and 'z'." << endl;
 +   else
 +      cout << "You entered something else." << endl;
 +
 +   return 0;
 +}</file>
 +
 +===== switch =====
 +<file c++ simple_switch_example.cpp>/* A simple switch statement example */
 +#include <iostream>
 +using namespace std;
 +
 +int main()
 +{
 +    int selection;
 +
 +    // Prompt the user for a selection:
 +    cout << "Enter:" << endl
 +         << "1 for a foo" << endl
 +         << "2 for a bar" << endl
 +         << "3 for a baz" << endl
 +         << "> ";
 +    cin >> selection;
 +
 +    // Give the user what they asked for:
 +    switch (selection)
 +    {
 +    case 1:
 +        cout << "You entered 1." << endl;
 +        cout << "A foo on all your houses!" << endl;
 +        break;
 +    case 2:
 +        cout << "You entered 2." << endl;
 +        cout << "A bar on all your houses!" << endl;
 +        break;
 +    case 3:
 +        cout << "You entered 3." << endl;
 +        cout << "A baz on all your houses!" << endl;
 +        break;
 +    default:
 +        cout << "You did not enter 1, 2, or 3." << endl;
 +    }
 +
 +    return 0;
 +}</file>
 +
  
cplusplus/selection_examples.1359867438.txt.gz · Last modified: 2013/02/03 04:57 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki