How to make Try Again in my Atmosphere program?

I am a beginner C++ programmer, I think I manage to create this program almost 97% complete! But my problem is how make Try Again choice? When I try to run it, I press Y, but instead this program come to end, and I want to make when you choose N, the program will quit. Am I missing something? And, can I make this program by using void?

Sorry for bad english
PS : Any correction and tips are appreciate

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 // This simple program will let you know which Atmosphere type

#include <iostream>
using namespace std;

int main()
{
    int choice, km;
    int Y,N;

    menu:
    cout << "\t Welcome guest! \n";
    cout << "\t This program will let you know about atmosphere layers \n" << endl;

    cout << "Enter a number in Km : ";
    cin >> km;

    while (km > 0)
    if (km <= 12)
        {
            cout << "This is Troposphere layer! \n";
            cout << "Try Again? ";
            cin >> choice;

            if (choice == Y)
                goto menu;
            else
                return 0;
        }

    else if (km <= 50)
    {
        cout << "This is Stratosphere layer! \n";
        cout << "Try Again? ";
        cin >> choice;

        if (choice == Y)
            goto menu;
        else
            return 0;
    }

    else if (km <= 80)
    {
        cout << "This is Mesosphere layer! \n";
        cout << "Try Again? ";
        cin >> choice;

        if (choice == Y)
            goto menu;
        else
            return 0;
    }

    else if (km <= 400)
    {
        cout << "This is Termosphere layer! \n";
        cout << "Try Again? ";
        cin >> choice;

        if (choice == Y)
            goto menu;
        else
            return 0;
    }

    else if (km > 401)
    {
        cout << "This is Exosphere layer! \n";
        cout << "Try Again? ";
        cin >> choice;

        if (choice == Y)
            goto menu;
        else
            return 0;
    }


}

When comparing against the users input on the choice on whether to continue, you need to compare that to 'y' or 'Y' not int Y; - this is a number.

Also, don't use gotos, use a while loop.

1
2
3
4
5
6
7
8
9
10
11
char userChoice = 'y'; // default to continue

while(userChoice == 'y')
{
    // your code starting at line 12, minus all the choice stuff
    // ...

    cout << "Try again? ";
    cin >> userChoice;

} // end while loop which will check the user choice 


Also what are you doing with this while (km > 0) loop on 18?
Still, it didn' work. When I press Y, the program still end itself same as I press N.
I went ahead and fixed up the things I noticed that could be improved, and got a working "Try again?" input.

Things I did:

- Made the 'Try again?' at the end of the if-else's to eliminate redundance.
- Removed unused variables.

Here ya go!

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
62
63
#include <iostream>
using namespace std;

int main()
{
    int nKM;
    char chAgain;
	bool bAgain = 1;

    cout << "\t Welcome guest! \n";
    cout << "\t This program will let you know about atmosphere layers! \n" << endl;

	while(bAgain == 1)
	{

    cout << "Enter a number in KM: ";

    cin >> nKM;

    if (nKM <= 12)
    {
        cout << "This is Troposphere layer! \n";
    }

    else if (nKM <= 50)
    {
        cout << "This is Stratosphere layer! \n";
    }

    else if (nKM <= 80)
    {
        cout << "This is Mesosphere layer! \n";
    }

    else if (nKM <= 400)
    {
        cout << "This is Termosphere layer! \n";
    }

    else if (nKM > 401)
    {
        cout << "This is Exosphere layer! \n";
	}

	cout << "Try again? (Y/N): ";
	cin >> chAgain;

	switch(chAgain)
	{
	case 'Y':
	case 'y':
		continue;
		return;
	case 'N':
	case 'n':
		bAgain = 0;
		break;
	}

	cout << endl;

	return 0;
}


Sorry if anything here is silly or doesn't make sense, it was 6AM when I made this.

Let me know if you have any other questions!
Last edited on
@TrulyRazor


1
2
3
bool bAgain = true;

while(bAgain) {


I wouldn't have used a switch for a yes / no answer. Make use of the toupper function, then have an if statement that compares with 'Y'.

It is a good idea to always have an else clause, to catch invalid input - in this case a negative value say.

The code for the yes no answer should be separate from the other, at the moment one has to enter a value above 401 before the question is asked.

Hope all goes well.
Topic archived. No new replies allowed.