简体   繁体   中英

C++ cannot figure out how to jump into the switch from user input

ok so i am writing a console application that uses a switch to display products and prices. however i am having hell figuring out how to make it jump into the switch when the user inputs the product number

this is the code:

using namespace std;

int main()
{
int productNum = 0;  
int quantityArray[5];
for (int i = 0; i < 5; ++i)
quantityArray[i] = 0;
char answer = ('y' || 'n' || 'e' || 'Y' || 'N' || 'E');
double priceArray[5] = { 2.98, 4.50, 9.98, 4.99, 6.87 };
double total1 = (quantityArray[0] * priceArray[0]);
double total2 = (quantityArray[1] * priceArray[1]);
double total3 = (quantityArray[2] * priceArray[2]);
double total4 = (quantityArray[3] * priceArray[3]);
double total5 = (quantityArray[4] * priceArray[4]);
double checkout = total1 + total2 + total3 + total4 + total5;

cout << "Please select a product number 1, 2, 3, 4, or 5" << endl;
cin >> productNum;
do
    {

       switch (productNum)
       {
       case '1':
           cout << "1 inch x 1 inch sticker is $" << priceArray[0] << endl;
       cout << "How many of these stickers would you like to purchase?" << endl;
        cin >> quantityArray[0];
              cout << "You have selected " << quantityArray[0] << "at the price of $" << priceArray[0] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '2':
              cout << "3 inch x 2 inch sticker is $" << priceArray[1] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[1];
              cout << "You have selected " << quantityArray[1] << "at the price of $" << priceArray[1] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '3':
              cout << "7 inch x 7 inch sticker is $" << priceArray[2] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[2];
              cout << "You have selected " << quantityArray[2] << "at the price of $" << priceArray[2] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '4':
              cout << "3 inch x 3 inch sticker is our current special at $" << priceArray[3] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[3];
              cout << "You have selected " << quantityArray[3] << "at the price of $" << priceArray[3] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '5':
              cout << "5 inch x 4 inch sticker is $" << priceArray[4] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[4];
              cout << "You have selected " << quantityArray[4] << "at the price of $" << priceArray[4] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
                if (answer = 'y' || 'Y')
                {
                    break;
                }
                else if (answer = 'n' || 'N')
                {
                    cout << "Please select a qantity." << endl;
                }
                else if (answer = 'e' || 'E')
                {
                    cout << checkout << endl;
                }
       }
    }while (answer != 'e' || 'E');

}

my book uses a header file to show how to initialize a swtich but it uses multiple methods. I am pretty sure that I can get this into one method without using a header.

Your switch statements are "wrong" because you read an integer then compare it with characters, which doesn't mean the same thing. You should also have a break; at the end of each case unless you actually WANT to fall through to the next case.

Code like this:

 (answer = 'y' || 'Y')

can be rewritten as this (to clarify what it does):

 ((answer = 'y') || 'Y')

It is wrong in two ways: First answer = 'y' sets the variable answer to 'y' (and it turns the statement to 'true' because the value of the whole expression is 'y' which is compared with zero (which would make it false) - it is not zero, so the statement ends there and executes what is inside the if. You should use the "is equal to" operator == , rather than "assignment operator" = to check if something is matching. If you fix that, and answer == 'y' isnt' true, the second part || 'Y' doesn't do at all what you want, but rather checks if 'Y' is zero or not (and it is not).

To fix this, you need to use if (answer == 'y' || answer == 'Y') and similar.

Same applies to this char answer = ('y' || 'n' || 'e' || 'Y' || 'N' || 'E'); , it sets answer to true [or 1 ], because the logical or of the values is not zero - and only 'y' gets checked, because C and C++ is defined to "stop checking once we've found something that determines the whole sequence".

I have two suggestions: 1. Only write a LITTLE bit of code at a time. Test that it works before you continue with the next step. } 2. Enable warnings in your compiler. Compile often. This will help you detect and fix problems before you written a lot of code that is "wrong".

It's because you read the input as an integer, but the cases are on character.

For example, the character '1' is (on the vast majority of computers) the same as the integer 49 .

change the cases to the integers instead:

case 1:
    // ...

case 2:
    // ...

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM