Help with a simple while statement

Write your question here.

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
// the code needs to display the string twice on the first line 
//four times on the second line and six times on the third
//But idk how to make it do that with the while loop 

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	string first; // holding the string value
	int temp = 3; // condition for while loop
	int second = 0; // counter variable
	
	cout << "Enter in the string you want to repeat:";
	cin >> first; // get the string
	cout << endl;


	while (second < temp)
	{
	cout << first << first << endl;
	second += 1;

	}

	return 0;
}
You need a double loop for this. Put a for-loop inside the while loop.
Well what exactly does that look like i do not understand how this will display the string mulitplying each time
Does it just have to be a while loop or can you use for loops?
1
2
3
4
5
6
7
8
9
10
11
while (second < temp)
	{
         
             for(int i=1; condition; i++)
                   cout << first << " ";
            
               cout << endl; // this endl happens outside the for-loop 

               second += 1;

	}


So the first time the loop iterates twice. On the next while loop the for-loop should iterate 4 times. On the last while loop the for-loop should iterate 6 times.

So think about what 'condition' would be. You'll need to declare and initialize a variable outside the while-loop for this 'condition'. Since the iterations will go from 2 to 4 to 6, you know that the condition will have to modified every time you exit the for loop. Think about how you would modify it.
Last edited on
Topic archived. No new replies allowed.