cplusplus:repetition_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:repetition_examples [2013/03/01 01:10] – mithat | cplusplus:repetition_examples [2019/02/11 18:01] – [Input validation] mithat | ||
---|---|---|---|
Line 42: | Line 42: | ||
===== Sentinel-controlled repetition ===== | ===== Sentinel-controlled repetition ===== | ||
- | <file c++ simple-sentinel.cpp>// | + | <file c++ simple-sentinel.cpp> |
- | // enters | + | # |
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const unsigned int PASSING_SCORE = 60; | ||
+ | int score; | ||
+ | |||
+ | cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; | ||
+ | cout << "Enter -1 to quit: "; | ||
+ | cin >> score; | ||
+ | |||
+ | while (score != -1) | ||
+ | { | ||
+ | if (score >= PASSING_SCORE) | ||
+ | { | ||
+ | cout << | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | cout << | ||
+ | } | ||
+ | |||
+ | 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>// | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 49: | Line 83: | ||
int main() | int main() | ||
{ | { | ||
- | int num = 1; | + | |
+ | const int UPPER_BOUND = 100; | ||
+ | int num; | ||
- | cout << " | + | cout << " |
- | | + | << " and " << UPPER_BOUND |
cin >> num; | cin >> num; | ||
- | while (num != -999) | + | |
+ | | ||
{ | { | ||
- | cout << num <<" | + | cout << "Enter a number between |
- | << endl; | + | << |
- | cout << "Enter an integer | + | |
- | cout << "Enter -999 to quit: "; | + | |
cin >> num; | cin >> num; | ||
} | } | ||
+ | |||
+ | cout << " | ||
return 0; | return 0; | ||
- | }</ | + | } |
+ | </ | ||
+ | |||
+ | ===== Running total ===== | ||
+ | <file c++ simple-running-total.cpp>// | ||
+ | // Total all the integers between a lower and an upper bound. | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const int LOWER_BOUND = 0; | ||
+ | const int UPPER_BOUND = 10; | ||
+ | |||
+ | int num = LOWER_BOUND; | ||
+ | int sum = 0; // accumulator | ||
+ | |||
+ | while (num <= UPPER_BOUND) | ||
+ | { | ||
+ | sum += num; // add the new value to the sum | ||
+ | num++; | ||
+ | } | ||
+ | |||
+ | cout << "The sum of integers from " << LOWER_BOUND | ||
+ | << | ||
+ | << | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
===== for ===== | ===== for ===== | ||
- | <file c++ simple-for.cpp>// | + | <file c++ simple-for.cpp>// |
- | // using a for structure. | + | |
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 128: | Line 194: | ||
cin >> choice; | cin >> choice; | ||
if (choice == ' | if (choice == ' | ||
- | break; | + | break; |
num++; | num++; | ||
} | } |
cplusplus/repetition_examples.txt · Last modified: 2019/02/14 21:28 by mithat