loop asking user to do program again

I have a program that asks the user to enter an account number and I have an array of numbers and it outputs if the user's number is valid or invalid( in the array or not). That works fine. I am having trouble with the loop that asks the user if they want to do the program again(y/n).

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
#include <iostream>
using namespace std;
		
int main()
{
	int account[ 18 ] = {5658845, 4520125, 7895122,8777541, 8451277, 1302850,
					8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
					1005231, 6545231, 3852085, 7576651,7881200, 4581002};
	int number;
	int choice = 0;
	for (int i=0; i < 18; i++)
	{
		cout << "Please enter your account number(7 numbers).\n";
		cin >> number;

		while (number < 1000000 || number > 9999999)
			{
			cout << "Please enter a 7 digit number.\n";
			cin >> number;
			}

		if (number == account[i])
			{cout << "The number is valid.\n";
			}
			
		if (number != account[i])
			{cout << "The number is invalid.\n";
			}
		do
		{
		cout << "Do you want to enter another account number?(y/n)\n";
		cin >> choice;
		}
		while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N');
		{
			cout << "Please enter 'y' for yes or 'n' for no.\n";
			cin >> choice;
		}
		
			while (choice == 'y' || choice =='Y');
			{
		cout << "Thank you and have a nice day!\n";
			return 0;
			}
	}
return 0;		
}


I tried moving it around everywhere except the correct place lol. Can anyone help?
closed account (EwCjE3v7)
What do u suppose return 0 does? In the main function ussualy it ends the program in success. There are a few ways to do this, u could use a while loop, a do while a function or goto statement.

I'll give u an example with a while

1
2
3
4
5
6
char choice = 'y';
while (choice == 'y' || choice =='Y') {
  // rest of code here
  cout << "Do you want to enter another account number?(y/n)\n";
  cin >> choice;
}


Also do not put a semicolon after the while loop that makes the while loop do nothing
Oh ok. I also noticed I had choice as an int while putting a char. I made choice a string and replaced all the ' with ". I could have just made choice a char, but if the user inputs more than 1 letter, the program will freak out.
But my problem is that I can't figure out where to put the while statement.
Here try something like this:
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
#include<iostream>
using namespace std;

int main()
{
	int account[ 18 ] = {5658845, 4520125, 7895122,8777541, 8451277, 1302850,
					8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
					1005231, 6545231, 3852085, 7576651,7881200, 4581002};
	int number;
	char choice;
	bool found=false;
	do
	{
		do
		{
			cout << "Please enter your account number (7 digits).\n";
			 cin >> number;
		} while (number < 1000000 || number > 9999999);
		
		for (int i=0; i < 18; i++)
		{
			if (number == account[i])
			{
				cout << "The number is valid.\n";
				found=true;
				break;
			}
		}
		
		if(found==false)
			cout<<"The number is invalid.\n";

        cout << "Do you want to enter another account number?(y/n)\n";
        cin >> choice;
	} while (choice == 'y' || choice == 'Y');


return 0;
}
ahh thanks. So I had to put the do while loops that asks to do it again at the beginning?
Topic archived. No new replies allowed.