-1

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;
}

3 Answers3

2

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;
}
Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
  • I vaguely understand what you are trying to say, i am a beginner to C++. I changed the code to work like suggested, but it still does not stop the code if i chose "N" when prompted //non recognized input else{ cout << "\nYour input was not recognised...Would you like to restart? (Y/N)\n"; cin >> yes_no; if( (yes_no == 'Y') || (yes_no == 'y') ) { goto start; } else { return 0; } – user3330762 Feb 20 '14 at 02:43
  • @user3330762 I took your code and applied my change and it works for me. – Eric Fortin Feb 20 '14 at 02:53
  • @user3330762 Added a smaller example, can you give it a try ? – Eric Fortin Feb 20 '14 at 02:57
  • Oh my! I tested that and it worked, so i added it to the code and it works great now. I see what you are saying now to have it ask for NO and if not, then reboot. – user3330762 Feb 20 '14 at 03:09
  • Thank you all so much, especially Eric. I knew it had to be something simple. – user3330762 Feb 20 '14 at 03:09
0

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')
anonymous
  • 1,317
  • 8
  • 7
0

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.
Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26