Error When I Run Code

Hello,

I cannot seem to figure out what's wrong with this piece of code. It's not doing what I want it to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
steptwo:

cin>> age;
if ( age < 22)
{
cout<< "You are "<<age<<" years old."<<endl;
goto stepthree;
}
else if (age< 21)
{
   cout<< "Please pick an age between 17-21."<<endl;
   goto steptwo;
}
else if ( age>  16)
{
  cout<< "Your age is too low. Please pick an age between 17-21."<<endl;
  }


I'm trying to have users input age, but it doesn't repeat itself when the user types in an age that's not between 17-21. For example, when I ran this certain piece of code I entered the number 13. It accepted the answer instead of rejecting it and repeating the code. Why is this happening?!

Any help is appreciated. Thanks.
closed account (zb0S216C)
Don't use label: ... goto. It's been deprecated and should be avoided. Convert it to a loop instead. For example:

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
int Switch( 0 );

while( /* Condition */ )
{
    std::cin >> age;

    if( age < 22 )
        Switch = 1;

    else if( age < 21 )
        Switch = 2;

    else if( age < 16 )
        Switch = 3;

    switch( Switch )
    {
        case 1:
            // Code...
            break;

        case 2:
            // Code...
            break;

        default:
            // Code...
            break;
    }
}

Maybe then will your program work as expected. Note that label: ... goto is still around because it allows for backwards compatibility for legacy C programs. You should never use it

Wazzak
That might work, but I've figured my problem out :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
steptwo:

cin>> age;
if ( 17< age >22 )
{
cout<< "You are "<<age<<" years old."<<endl;
goto stepthree;
}
else if (age< 21)
{
   cout<< "Please pick an age between 17-21."<<endl;
   goto steptwo;
}
else if ( age>  21)
{
  cout<< "Your age is too high. Please pick an age between 17-21."<<endl;
  goto steptwo;
  }
  else if (age == 21)
  { 
    cout<< "You are "<<age<<" years old."<<endl;
goto stepthree;
}


Why should I never use the goto statement though? Can you explain to me what legacy C programs are?
Last edited on
closed account (zb0S216C)
Code Assassin wrote:
Why should I never use the goto statement though? (sic)

Because it's been deprecated. Not only is it hard to read, it's ugly and it could potentially create buggy code.

Since you're the Code Assassin, you should do us all favour and assassinate label: ... goto :)P

Wazzak
Last edited on
:P

Alright, I won't use the goto statement anymore.

( The deed has been done)
closed account (DSLq5Di1)
if ( 17< age >22 ) // ???

This ^ does not do what you think it will do! and the rest of your logic is a lil screwy..

if (age >= 17 && age <= 21) { ... } else { ... }
That's weird.

It works perfectly on my compiler, and it does exactly what I wanted it to do.
Last edited on
Nevermind.

I see what you mean. I entered a 4-digit number and it kept repeating the same line over and over.

I tried your way and it works. Thanks for the suggestion, sloppy.
Last edited on
Topic archived. No new replies allowed.