Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I do not understand why my code is not working properly everything is displayed

ID: 3854851 • Letter: I

Question

I do not understand why my code is not working properly everything is displayed as invalid

/******************************

Name

Date

File Name template.cpp

Description

********************************/

// Headers

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

// Global variables

// Function declarations

int main()

{

    char userInput;


    do {

    cout << "Welcome to the Serendipity Booksellers POS Software System"

        << endl << endl;

    cout << "Options:" << endl << endl;

    cout << "R - Cash Register" << endl;

    cout << "I - Inventory" << endl;

    cout << "C - Customer" << endl;

    cout << "H - Report" << endl;

    cout << "Q - Quit Program" << endl << endl;

    cout << "Enter option: ";

    userInput = tolower(cin.get());

    

        

        while (cin.get() != ' ');

        

        if (userInput != 'r' || 'i' || 'c' || 'h')

        {

            cout << "Invalid input" << endl;

            

        }

        else

        {

            switch(userInput)

            {

                case 'r': cout << "Cash Register" << endl;

                case 'i': cout << "Inventory" << endl;

                case 'c': cout << "Customer" << endl;

                case 'h': cout << "Report" << endl;

                

            }

        }

        if (userInput == 'q')

        {

            system("PAUSE");

            system("CLS");

        }

        

    }while (userInput != 'q');

return 0;

}

Explanation / Answer

Try the following code :


#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

// Global variables

// Function declarations

int main()
{
char userInput;


do {

cout << "Welcome to the Serendipity Booksellers POS Software System"
<< endl << endl;
cout << "Options:" << endl << endl;
cout << "R - Cash Register" << endl;
cout << "I - Inventory" << endl;
cout << "C - Customer" << endl;
cout << "H - Report" << endl;
cout << "Q - Quit Program" << endl << endl;
cout << "Enter option: ";
userInput = tolower(cin.get());
  
  
while (cin.get() != ' ');
  
  
if (userInput != 'r' && userInput !='i' && userInput != 'c' && userInput != 'h' && userInput != 'q')
{
cout << "Invalid input" << endl;
  
}
else
{
switch(userInput)
{
case 'r': cout << "Cash Register" << endl;
break;
case 'i': cout << "Inventory" << endl;
break;
case 'c': cout << "Customer" << endl;
break;
case 'h': cout << "Report" << endl;
break;
  
}
}
  
if (userInput == 'q')
{
system("PAUSE");
system("CLS");
}
  
  
  
}while (userInput != 'q');
return 0;
}

You were getting invalid input in every case because in a logical statement or will not check furthur when it gets true. If your input is r first check that userInput!='r' will return false but compiler will furthur check whether 'r'!='i' which is true and it won't check furthur and print Invalid input. Rest all the alphabets will be rejected by the firat check userInput!='r' .

Also keep break after each case case : in switch block else if you press r compiler will print all the 4 strings. If you press i compiler will print the 3 strings from inventory onwards.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote