Question

I have tried many things and i can not seem to figure out why this program will not stop the code if you select N when it prompts to try again or not.

I feel as though i had this working earlier, but i can not find any code from when it was working, and i see no reason this should not work. Can anyone help out?

#include <iostream>
using namespace std;

int main ()
{
    char color[10];
    char reboot, yes_no;

    start:
        cout << "What color is the light?\n";
        cin >> color; 

    //if the light is Green
    if (!strcmp(color, "green")) { 
        cout << "The light is Green, you may go ahead and drive thru the intersection.\n"; 
    } else if (!strcmp(color, "Green")) { 
        cout << "The light is Green, you may go ahead and drive thru the intersection.\n"; 

        //if the light is Yellow
    } else if (!strcmp(color, "yellow")) { 
        cout << "The light is Yellow, safely stop at the intersection, or proceed thru.\n"; 
    } else if (!strcmp(color, "Yellow")) { 
        cout << "The light is Yellow, safely stop at the intersection, or proceed thru.\n"; 

        //if the light is Red
    } else if (!strcmp(color, "red")) { 
        cout << "The light is Red, you need to stop.\n"; 
    } else if (!strcmp(color, "Red")) { 
        cout << "The light is Red, you need to stop.\n"; 
    } 

    //non recognised input
    else{
        cout << "\nYour input was not recognised...Would you like to restart? (Y/N)\n";
        cin >> yes_no;
        if(yes_no == 'Y'||'y'){
            goto start;
        }
    }

    //restart program
    restart:
        cout << "\nWould you like to run the program again? (Y/N)\n";
        cin >> reboot;
        if(reboot == 'Y'||'y'){
            goto start;
        }
    return 0;
}
Was it helpful?

Solution

Your condition is not well formed it should be

if( (reboot == 'Y') || (reboot ==  'y') )
{
    goto start;
}

As it is, it always evaluates to true since 'y' evaluates to true and true || anything always gives true.

Same thing applies to yes_no check.

EDIT Since you are having trouble, I made a simple program to test it more easily, this should work as expected:

#include <iostream>

using namespace std;

int main()
{
    char yes_no;

    while (true)
    {
        cout << "Enter 'N or 'n' to quit\n";
        cin >> yes_no; 

        if(yes_no == 'N'|| yes_no == 'n')
            break;
    }
    return 0;
}

OTHER TIPS

These 2 lines looks a bit strange

if(yes_no == 'Y'||'y') 
if(reboot == 'Y'||'y')

maybe you meant below instead??

if(yes_no == 'Y' || yes_no == 'y')
if(reboot == 'Y' || reboot == 'y')

Starting with the real reason your code doesn't work - operator precedence and associativity:

reboot == 'Y'||'y'

always returns true, since it's parsed as (reboot=='Y')||'y'. If you want to test if reboot is equal one of the two chars, test it like that: reboot=='Y'||reboot=='y'.

That should fix your code. Although here are some advices:

  • Don't use the goto statement. You can loop your code using loops (while, for or do while).
  • If you're using C++, use std::string for storing text, you can then use text=="some Text" instead of testing the output of strcmp.
  • For future reference on operator precedence, you can always check Wikipedia.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top