How to restart loop again?

I've made the code and all i need to do is have it restart after inputting y. The problem is that inputting y will stop the program instead of restarting. Any help is appreciated.


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
 #include <iostream>

using namespace std;

int main()
{
    int input,option;
    cout << "\tTriangles";
    cout << "\n\t==============";
    cout << "\nInput N: ";

   cin >> input;
    if (input %2 == 0 && input > 0) {

  for( int i = input; i >= 1; i-- )
  {
    for( int j = 1; j <= input; j++)
    {
      cout << (j <= i ? "*" : " " );
    }
    cout << endl;
  }

  }

if (input %2 == 1 && input > 0 )
    {

  for( int i = 1; i <= input; i++ )
  {
    for( int j = 1; j <= input; j++)
    {
      cout << (j <= i ? "*" : " " );
    }
    cout << endl;
  }}
if (input < 1) { cout << "Input needs to be more than 1";}

cout << "Restart (Y/N)?";
cin >> option;

while (a=='y')


}
1
2
3
do{
  //code
}while(a=='y');
The while loop was not initialized properly. I would recommend using a do while loop here.

1
2
3
do {
//input code here
} while(a == 'y' || a == 'Y');
so i used the do while loop that you both suggested but it still doesn't work. What am I doing wrong?

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
 #include <iostream>

using namespace std;

int main()
{

    int input, a;
    cout << "\tTriangles";
    cout << "\n\t==============";
    cout << "\nInput N: ";
   cin >> input;
    do { 
    if (input %2 == 0 && input > 0) {

  for( int i = input; i >= 1; i-- )
  {
    for( int j = 1; j <= input; j++)
    {
      cout << (j <= i ? "*" : " " );
    }
    cout << endl;
  }

  }

if (input %2 == 1 && input > 0 )
    {

  for( int i = 1; i <= input; i++ )
  {
    for( int j = 1; j <= input; j++)
    {
      cout << (j <= i ? "*" : " " );
    }
    cout << endl;
  }}
if (input < 1) { cout << "Input needs to be more than 1";}

cout << "Restart (Y/N)?";
cin >> a;


} while( a == 'y');
}
well i'm pretty new to programming so take my comments with a grain of salt.

couple things. maybe add some comments to your program so it is more clear to someone else what it is you are trying to accomplish. also, your cout statements in lines 9-11 were confusing to me as a user when i ran this program, but then again i don't know what this program is for so maybe it is exactly how you want it worded.

i changed your int variable a to char variable ans (easier to read) and put the code you want executed in a while loop.

the code below works for me and continues to loop if the user enters either y or Y. let me know if it works for you or not. cool program though, good work :)

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
#include <iostream>

using namespace std;

int main()
{
    
    int input;
    char ans;
    cout << "Do you want to see a triangle of stars?\n";
    cin  >> ans;
    
    while ((ans == 'y') || (ans == 'Y'))
    {
    cout << "\tTriangles";
    cout << "\n\t==============";
    cout << "\nInput N: ";
    cin >> input;
    
        if (input %2 == 0 && input > 0) {
            
            for( int i = input; i >= 1; i-- )
            {
                for( int j = 1; j <= input; j++)
                {
                    cout << (j <= i ? "*" : " " );
                }
                cout << endl;
            }
            
        }
        
        if (input %2 == 1 && input > 0 )
        {
            
            for( int i = 1; i <= input; i++ )
            {
                for( int j = 1; j <= input; j++)
                {
                    cout << (j <= i ? "*" : " " );
                }
                cout << endl;
            }}
        if (input < 1) { cout << "Input needs to be more than 1";}
        
        cout << "Restart (Y/N)?";
        cin >> ans;
    }
    
        
    
}

1
2
3
4
5
6
7
for(;;)
	{
		cin>> ch; //char
		if (ch=='n' || ch=='N')
			break;
		//...	
	}
Thank you so much everyone! The program works!
closed account (1CfG1hU5)
title: how to restart loop again?

restarting means you started the loop before. the again is a 2nd restart.

start
restart
restart

:)
Topic archived. No new replies allowed.