c++ - Why are my switch cases being skipped and default being printed? -


i have written c++ code in attempt simulate vending machine. whenever user's selection entered, number 1-5 each assigned case in switch statement, none of cases execute: program not recognize valid input, , print out default case statement. why happening?

#include <iostream>  using namespace std;  int main() {     int choice;     double dollars;      cout << "==============================================================\n";     cout << "*                                                            *\n";     cout << "*              vending machine simulator                     *\n";     cout << "*                                                            *\n";     cout << "==============================================================\n";      cout << "your choices are:\n";      cout << "1) coke     - $1.75\n";     cout << "2) pepsi    - $2.25\n";     cout << "3) sprite   - $1.25\n";     cout << "4) 7-up     - $1.15\n";     cout << "5) water    - $2.20\n";      // ask user's selection.      cout << "what purchase today?\n";     cin >> choice;     switch (choice)     {             case '1':              {                     cout << "how many dollar bills use?\n";                     cin >> dollars;                     {                             if (dollars > 1.75)                             {                                     cout << "you purchased: coke\n";                                     cout << "your change is: $"<< (dollars - 1.75);                             }                             else                             {                                     cout << "you not have enough money purchase. goodbye!\n";                             }                     }             }                       break;             case '2':             {                     cout << "how many dollar bills use?\n";                     cin >> dollars;                     {                             if (dollars > 2.25)                             {                                     cout << "you purchased: pepsi\n";                                     cout << "your change is: $"<< (dollars - 1.75);                             }                             else                             {                                     cout << "you not have enough money purchase. goodbye!\n";                             }                     }             }             case '3':              {                     cout << "how many dollar bills use?\n";                     cin >> dollars;                     {                             if (dollars > 1.25)                             {                                     cout << "you purchased: sprite\n";                                     cout << "your change is: $"<< (dollars - 1.25);                             }                             else                             {                                     cout << "you not have enough money purchase. goodbye!\n";                             }                     }             }                       break;             case '4':              {                     cout << "how many dollar bills use?\n";                     cin >> dollars;                     {                             if (dollars > 1.15)                             {                                     cout << "you purchased: 7-up\n";                                     cout << "your change is: $"<< (dollars - 1.15);                             }                             else                             {                                     cout << "you not have enough money purchase. goodbye!\n";                             }                     }             }                       break;             case '5':              {                     cout << "how many dollar bills use?\n";                     cin >> dollars;                     {                             if (dollars > 2.20)                             {                                     cout << "you purchased: water\n";                                     cout << "your change is: $"<< (dollars - 2.20);                             }                             else                             {                                     cout << "you not have enough money purchase. goodbye!\n";                             }                     }             }                        break;             default: cout << "your choice invalid. goodbye!\n";       }     return 0; } 

drop single quotes used in case statements.

change

 case '1': 

to

case 1:  

similarly others.

'1' character constant while 1 integer constant. when ascii encoding used, '1' evaluates number 49, not equal number 1. hence, case: branch not executed when enter 1 input.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -