# Repeat this!

• 10-02-2012
Repeat this!
If I have a do-while loop like this below: and then what follows, how would I repeat the whole shabang?

Code:

``` #include <iostream> # include <time.h> using namespace std ;int main() {int maximum; int minimum; int random_number; int req_subtraction; int current_total; int total; cout << "Total Limit?" << endl;  //can't go over this cin >> total; cout << "Max you can add?" << endl; cin >> maximum; cout << "Minimum you can add?" << endl; cin >> minimum; cout << "Required Subtraction?" << endl; cin >> req_subtraction;  srand(time(0));  //seed the random number int high=maximum; int low=minimum; do {random_number = rand () % (high - low + 1) + low;//random number between max and min cout << "Random Number is " << random_number << endl; current_total += random_number - req_subtraction;    if(current_total < 0)  //make so negatives equal out to zero    {    current_total = 0;    }  cout << "The current total is " << current_total << endl; } while (current_total < total); system("pause"); return 0; }```
and then I say this

Code:

``` cout <<"Would you like to try again? (yes/no)" << endl;  cin >> yes_no; if (yes_no == "yes") {   //repeat somehow? } else {    cout <<"Goodbye!" <<endl; }```
• 10-02-2012
synhyborex
The simplest way (if you don't want to change too much code) is to just set what's called a sentinel value. Essentially, you want to put all the code you want repeated inside a giant while loop that is something like
Code:

`while(sentinel == 0)`
. Inside that if check, just have sentinel's value be changed accordingly. If sentinel is no longer 0, the program will terminate. So you might want this:
Code:

```cout <<"Would you like to try again? (yes/no)" << endl;  cin >> yes_no; if (yes_no == "yes") {   sentinel = 1; } else {    cout <<"Goodbye!" <<endl; }```
Of course, remember to declare and initialize the "sentinel" value correctly!
• 10-02-2012
oogabooga
That's not what I would call a "sentinel". It's just a stinky old useless flag. Phew! Actually, the simplest way would be:
Code:

```do {     //...     cout << "Would you like to try again? (yes/no)\n";     cin >> yes_no; } while (yes_no == "yes"); cout << "Goodbye.\n";```
• 10-02-2012
when I do that and it repeats it ignores one of my variables.

I ask them to enter their name and it just puts the cout and skips the cin. It works fine the first instance though.

Code:

```string your_name; cout <<"What is your name?" << endl;   getline(cin,your_name);```
• 10-02-2012
laserlight
Hint: the use of formatted I/O operator>> to read an integer leaves the newline from entering the integer text in the input buffer. getline will then read until that newline instead of the next line.
• 10-02-2012