### Quick Question on Loop

If I have a Do, While statement. How do I make this repeat and output ten times?
I know it is repeating until the condition is met, but how do I do this an additional nine times?

For example,

do
{
stufffffff
}

while(this condition);
 ``123456`` ``````int i = 0; do { stuff... cout << "this is your answer!" << endl; } while (++i < 10);``````

But it's more conventiional to use a for loop:
 ``12345`` ``````for (int i = 0; i < 10; ++i) { stuff... cout << "this is your answer!" << endl; }``````
Last edited on
Even for this scenario? This do while runs until it meets the condition and gives an answer. I just want to do ten scenarios of this. Ten different answers from ten instances of this run.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include # include 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; }``````
Exactly the same as before:
 int i = 0; do { //stuff... cout << "this is your answer!" << endl; } while (++i < 10);

I second using a 'for' statement...

My second choice would be:
 ``123456`` ``````int i = 0; while (++i <= 10) { //stuff... cout << "this is your answer!" << endl; }``````
Last edited on
How about if I ask this at the end? How to repeat the whole shabang?

 ``1234567891011`` ``````cout <<"Would you like to try again? (yes/no)" << endl; cin >> yes_no; if (yes_no == "yes") { //repeat somehow? } else { cout <<"Goodbye!" <
Last edited on
Topic archived. No new replies allowed.