-1

I am trying to implement a feature into my program that says "Do you want to quit?" If 'Y or 'y' exit the program. If 'N' or 'n' rerun the menu and let the user do whatever they want.

The issue that I am facing is that my menu runs the first time and the user can say yes or no. Then the second time they can say yes or no. However the third time when it reaches the while loop it infinitely outputs the menu.

Can anyone please advise?

The code for the menu is

        char exitInput = NULL;
        char Y = 'Y', y = 'y', N = 'N', n = 'n';
        while (exitInput != Y || exitInput != y || exitInput == N || exitInput == n)
        {
            cout << "*\t Please choose one of the following options. \t *" << endl;

            cout << "1. \t" << "Transfer an amount \n"
                 << "2. \t" << "List recent transactions \n"
                 << "3. \t" << "Display account details and current balance \n"
                 << "4. \t" << "Quit \n";

            int menuSelection;
            cout << "Enter your option here: ";
            cin >> menuSelection;

            switch (menuSelection)
            {
            case 1:
            .......
            case 2:
                cout << "Do you want to exit the application? ";
                cin >> exitInput;
            }
        }
PapaSmurf
  • 161
  • 4
  • 15
  • 1
    are you using `break` statement after every `case` right? – Ankur Dec 16 '14 at 16:54
  • your condition is always true ! and you code should be improved – Engine Dec 16 '14 at 16:55
  • it usually helps to create online working examples. You can create one for C++ here and share links http://cpp.sh/ – Shaunak Dec 16 '14 at 16:55
  • Sure about these conditions in the `while()`: `exitInput != Y || exitInput != y || exitInput == N || exitInput == n`? – πάντα ῥεῖ Dec 16 '14 at 16:56
  • Hi, I have tried changing to strings and using conditions suggested however, My program works the first time then when I enter a character the second time my program just infinitely outputs the information. Please see screenshots: i.imgur.com/6Tv1EKM.png i.imgur.com/OcOE4Md.png – PapaSmurf Dec 16 '14 at 18:12
  • what is the user input? what is in exitInput on the second iteration of the loop? That is all that matters here. – tdbeckett Dec 16 '14 at 20:28
  • consider making the variables Y, y, N, and n const. – tdbeckett Dec 16 '14 at 20:29
  • Based on the output of the program on the screenshot, the input turns bad (i.e., `std::cin` becomes `false`). In your code I don't see any reason why that would be the case, though. – Dietmar Kühl Dec 16 '14 at 20:46
  • I'm getting that idea too Dietmar but I'm not sure why this is the case. The user input on the second iteration is n. – PapaSmurf Dec 16 '14 at 20:52
  • Well, did you try to check `std::cin` before entering the loop? Once it is confirmed that it is indeed the case, the next step is then to find out _why_ it becomes bad... – Dietmar Kühl Dec 16 '14 at 20:55
  • The cin seems to be fine before the loop. – PapaSmurf Dec 16 '14 at 23:55

4 Answers4

1

I'm not sure I have well understood your problem, but if it is "when I say 'YES', the menu comes back again", it's normal. exitInput can't be equal to Y and y at the same time, so the while condition is always true.

you should try :

while ((exitInput != Y && exitInput != y) || exitInput == N || exitInput == n)

EDIT

I could see your bug in a simple win32 application. I have solved it by changing the type of menuselection from int to char, but I don't know why it's working sometimes with integer. here is my test program :

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char exitInput = 'n';
    char menuSelection;

    while (exitInput !='Y' && exitInput !='y')
    {
        cout << "*\t Please choose one of the following options. \t *" << endl;

        cout << "1. \t" << "Transfer an amount " << endl
                << "2. \t" << "List recent transactions " << endl
                << "3. \t" << "Display account details and current balance " << endl
                << "4. \t" << "Quit " << endl;

        cout << "Enter your option here: ";
        cin >> menuSelection;

        switch (menuSelection)
        {
        case '2':
            cout << "Do you want to exit the application? ";
            cin >> exitInput;
            break;
        default:            
            cout << "Command is : " << menuSelection << endl;
            cout << "Command not found !" << endl;
            break;
        }
    }
    return 0;
}

EDIT 2

I found this thread where there the problem is explained. tested and the problem is gone. why-would-we-call-cin-clear-and-cin-ignore-after-reading-input

Community
  • 1
  • 1
grorel
  • 1,408
  • 15
  • 21
  • Hi, using this new while statement give me a similar issue. I have also changed the char to exitInput = 'n' My program works the first time then when I enter a character the second time my program just infinitely outputs the information. Please see screenshots: i.imgur.com/6Tv1EKM.png i.imgur.com/OcOE4Md.png – PapaSmurf – PapaSmurf Dec 16 '14 at 17:45
1

This condition

while (exitInput != Y || exitInput != y || exitInput == N || exitInput == n)

Will always be true. You have to change it to something like:

while (exitInput != Y && exitInput != y)

Also, the initial value of exitInput does not make much sense, just put char exitInput = 'n'

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Hi, using this new while statement give me a similar issue. I have also changed the char to exitInput = 'n' My program works the first time then when I enter a character the second time my program just infinitely outputs the information. Please see screenshots: http://i.imgur.com/6Tv1EKM.png http://i.imgur.com/OcOE4Md.png – PapaSmurf Dec 16 '14 at 17:40
  • @PapaSmurf I suspect the problem is using a char variable. Try to print exitInput, probably anyway the best option is to use a std::string instead. Note that in that case you have to update also your exit condition accordingly. – Antonio Dec 16 '14 at 19:18
0

Just change condition in the while loop to:

while ((exitInput != Y) && (exitInput != y))

And it will work as intended. Your original condition is always true, whatever value exitInput has - that's why you program failed. Note, that there is no need to check if exitInput is n/N. y/Y means quit, and any other input means not quit, so you really have to check only y/Y.

-1

You have an error in the compare in your while() check. Try something like this:

while (exitInput != Y && exitInput != y )
{
    cout << "*\t Please choose one of the following options. \t *" << endl;
    ...

There are other things I could comment on, but they don't address your question. HTH

Frecklefoot
  • 1,660
  • 2
  • 21
  • 52