Allowing user to do program again

So I had to write a program that took the growth of two towns, town 1 being the smaller town with a higher growth rate and town 2 being the larger town with a smaller growth rate. I have it completed to the best of my beginner capability, since I just started learning c++. I want to prompt the user to do the program again. I have looked through the forums and I have tried to add what they have suggested and it has not worked for me. Any suggestions?
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



Towns:	
	cout << "\n\nEnter smaller town 1 population: ";
	cin >> town1;	

	cout <<"\nEnter larger town 2 population: ";
	cin >> town2;
		
		if	(town1<0 || town2<0|| town1>town2)
			{
			
		std::cout << "\n\tError: Please enter correct populations size                    or smaller town first";
		std::cout << "\n";
	        std::cout << "\n";
		std::cout << "\n"; goto Towns;
					}

		else (town1 < town2);
		{		
		cout << "\n\tPlease enter town 1 growth rate: ";
			cin >> growth1;
			growth1 /=100;

		cout << "\n\tPlease enter town 2 growth rate: ";
		cin >> growth2;
		growth2 /=100;		
		
		while(town1<town2)
		{
		town1+= (growth1 * town1);
		town2+= (growth2 * town2);
				years +=1;

		cout << "\n\nYear \tTown1 \tTown2 "<< endl;
		cout <<""<< years << "\t" << town1 <<"\t"<<town2<< setw(1);
				}
cin.ignore(2);
				
  return 0;
  
		}
You can use a while or do..while loop to control the program flow. You can also use a while loop to validate input instead of gotos.

You can easily figure out the smaller of two numbers, so you could let the user enter the town figures in any order.
Ok I will give that a try thank you... I will let you know how it goes.
closed account (j3Rz8vqX)
Sample:
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
#include <iostream>
using namespace std;
int main()
{
    string str = "Hello World";
    const static string display = "\nReversing string: ",
            prompt = "\nEnter the string to reverse: ",
            repeat = "\nWould you like to try again ('yes' or 'no'): ",
            error = "\nInvalid entry, try again ('yes' or 'no'): ",
            yes = "yes", no = "no";
    cout<<"String Reversing Program\nDefault string: 'Hello World'\n";
    do
    {
        cout<<display<<'\n';
        cout<<": "<<str<<'\n'<<": ";
        for(int i=0;i<str.size();++i)                       //Print manipulate: reverse string
            cout<<str[str.size()-1-i];
        cout<<repeat;
        while(getline(cin,str)&&(str!=yes&&str!=no))        //Validate user choice repeat if(!"yes" or !"no")
            cout<<error;
    }while(str!=no && cout<<prompt && getline(cin,str));    //Repeat loop if not "no": prompt user if "no";
    cout<<"\nPress <enter> to exit console: ";
    cin.get();                                              //Console exit prompt
    return 0;
}
String Reversing Program
Default string: 'Hello World'

Reversing string:
: Hello World
: dlroW olleH
Would you like to try again ('yes' or 'no'): incorrect

Invalid entry, try again ('yes' or 'no'): yes

Enter the string to reverse: It worked!

Reversing string:
: It worked!
: !dekrow tI
Would you like to try again ('yes' or 'no'): no

Press <enter> to exit console:
Topic archived. No new replies allowed.