-9

I'm attempting to use a while(1) loop in the getCommand function but it runs infinitely and doesn't break. I've included the HELP function because it is called in the getCommand function. Here is the code in question:

void HELP(string inCommand) {
    if (inCommand == "invLoad?")
        cout << "This command allows the user to load in new inventory information into the report." << endl;
    else if (inCommand == "invReport?")
        cout << "This command allows the user to access an inventory report of a requested item." << endl;
    else if (inCommand == "invPriceUpdate?")
        cout << "This command allows the user to update the prices of a requested item." << endl;
}

//This is the function that acquires the SIMS command from the user 
string getCommand()
{
    string commandInput = "";
    char quit = '.'; 
    cout << "Please enter the command for the Simple Inventory Management System:" << endl <<
    "invLoad, invReport, invPriceUpdate." << endl << endl;
    cout << "To learn what each command does add a '?' at the end of the command input" << endl;
    cin >> commandInput;

    while (1) {
        if (commandInput == "invLoad") {
            return "invLoad";
        }
        else if (commandInput == "invReport") {
            return "invReport";
        }
        else if (commandInput == "invPriceUpdate") {
            return "invPriceUPdate";
        }
        else if (commandInput == "invLoad?")
            HELP(commandInput);
        else if (commandInput == "invPriceUpdate?")
            HELP(commandInput);
        else if (commandInput == "invReport?")
            HELP(commandInput);
        else
            cout << "Please enter an appropriate command!" << endl;
        switch (quit)
        {
            case 'y':
                return 0;
                break;

            case 'n':
                cout << "Enter another command" << endl;
        }
    }
}

Unfortunately the while loop runs infinitely and I can't figure out how to break it.

Striezel
  • 3,693
  • 7
  • 23
  • 37
KyleLV
  • 11
  • It will run infinitely, check http://stackoverflow.com/questions/24278724/purpose-of-while1-statement-in-c – Harshul Routhu Feb 16 '17 at 21:38
  • 2
    You can use a debugger to find out whats going on. – pokey909 Feb 16 '17 at 21:38
  • Try put the cin instruction in the loop to let the user a chance to update the commandInput value on each loop. – utopman Feb 16 '17 at 21:38
  • 1
    Of course it runs infinitely. The contents of the loop keep checking the same variables, hoping that their values would somehow magically change, on their own. Of course, C++ doesn't work this way. There's nothing inside the `while` loop that will change the values in the variables; as such each iteration of the loop will always come up with the same results. – Sam Varshavchik Feb 16 '17 at 21:39
  • Have you tried stepping through the code using a debugger? – Roland Illig Feb 16 '17 at 21:39
  • there is no `break` in your loop, so how do expect it to break? telepathy? – 463035818_is_not_an_ai Feb 16 '17 at 21:45
  • 1
    `commandInput == "invReport"` this is not how you compare two strings. – stark Feb 16 '17 at 22:55

1 Answers1

0

The switch loop : It shouldn't be written like that .

The input " commandInput " is what should be looped on

The correct statement is : switch(commandInput)

you had already given the variable quit a value "." which will lead the compiler not to enter the loop as the condition will never be satisfied as you are always asking fo the commandInput variable as an input so when you debug your code you will actually find that the loop is not entered and that's why you loop remains infinite