User Tools

Site Tools


cplusplus:repetition_examples

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cplusplus:repetition_examples [2013/03/01 01:05] – created mithatcplusplus:repetition_examples [2019/02/11 17:49] – [Input validation] mithat
Line 42: Line 42:
 ===== Sentinel-controlled repetition ===== ===== Sentinel-controlled repetition =====
  
-<file c++ simple-sentinel.cpp>// This program calculates the squares of integers. The user +<file c++ simple-sentinel.cpp>    // This program determines whether test scores are passing marks
-// enters -999 when finished+    #include <iostream> 
-#include <iostream> +    using namespace std;
-using namespace std;+
  
-int main() +    int main() 
-+    
-    int num 1;+        const unsigned int PASSING_SCORE 60; 
 +        int score;
  
-    cout << "Enter an integer and I will tell you its square. " << endl; +        cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; 
-    cout << "Enter -999 to quit: "; +        cout << "Enter -to quit: "; 
-    cin >> num;+        cin >> score;
  
-    while (num != -999)+        while (score != -1) 
 +        { 
 +            if (score >= PASSING_SCORE) 
 +            { 
 +                cout <<  score << ": PASS" << endl; 
 +            } 
 +            else 
 +            { 
 +                cout <<  score << ": FAIL" << endl; 
 +            } 
 + 
 +            cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; 
 +            cout << "Enter -1 to quit: "; 
 +            cin >> score; 
 +        } 
 + 
 +        return 0; 
 +    } 
 + 
 +</file> 
 + 
 +===== Input validation ===== 
 +<file c++ simple-input-validation.cpp>    // Simple example of input validation. 
 +    #include <iostream> 
 +    using namespace std; 
 + 
 +    int main()
     {     {
-        cout << num <<" squared is " << num * num << "." << endl +        const int LOWER_BOUND = 0; 
-             << endl; +        const int UPPER_BOUND = 100; 
-        cout << "Enter an integer and I will tell you its square. " << endl; +        int num; 
-        cout << "Enter -999 to quit: ";+ 
 +        cout << "Enter a number between " << LOWER_BOUND  
 +             << " and " << UPPER_BOUND << ": ";
         cin >> num;         cin >> num;
-    } 
  
-    return 0; +        // Fall through loop only if input is valid. 
-}</file>+        while (num LOWER_BOUND || num UPPER_BOUND) 
 +        { 
 +            cout << "Enter a number between " << LOWER_BOUND 
 +                 << " and " << UPPER_BOUND << ": "; 
 +            cin >> num; 
 +        }
  
 +        cout << "Congratulations, you can follow directions." << endl;
 +
 +        return 0;
 +    }
 +</file>
 ===== for ===== ===== for =====
-<file c++ simple-for.cpp>// This program counts to an upper limit +<file c++ simple-for.cpp>// This program counts to an upper limit using a for structure.
-// using a for structure.+
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 109: Line 145:
 } }
 </file> </file>
 +
 +===== break and continue =====
 +<file c++ simple-break.cpp>// Print out a list of powers of 2.
 +#include <iostream>
 +#include <cmath>
 +using namespace std;
 +
 +int main()
 +{
 +    const int UPPER_LIMIT = 100;
 +    int num = 1;    // counter
 +    char choice;    // used to get user input
 +
 +    while (num <= UPPER_LIMIT)
 +    {
 +        cout << "2 to the " << num << " power is " << pow(2, num) << endl;
 +        cout << "Should I go on? [y/N]: ";
 +        cin >> choice;
 +        if (choice == 'N' || choice == 'n')
 +            break;    // (try replacing with 'continue')
 +        num++;
 +    }
 +
 +    cout << "Goodbye!" << endl;
 +    return 0;
 +}
 +</file>
 +
cplusplus/repetition_examples.txt · Last modified: 2019/02/14 21:28 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki