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
cout << this is your answer! << endl;
}

while(this condition);
1
2
3
4
5
6
int i = 0;
do
{
  stuff...
  cout << "this is your answer!" << endl;
} while (++i < 10);


But it's more conventiional to use a for loop:
1
2
3
4
5
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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;
}
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:
1
2
3
4
5
6
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?

1
2
3
4
5
6
7
8
9
10
11
cout <<"Would you like to try again? (yes/no)" << endl; 
cin >> yes_no;

if (yes_no == "yes")
{
	//repeat somehow?
}
else
{
	cout <<"Goodbye!" <<endl;
}
Last edited on
Topic archived. No new replies allowed.