C++ program skips switch case | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

C++ program skips switch case

Need help, my program always skips the switch case after entering the item code. It goes directly to “Do you want to choose other product?” How to fix this problem and why is it like this https://code.sololearn.com/cKOc8lE9xjlq/?ref=app

11th Feb 2021, 12:26 PM
thatstupidcoder
thatstupidcoder - avatar
13 Answers
+ 6
You are comparing "productmenu" which is uninitialised along with other variables like "qty" etc. Looking at the program it looks like you wanted to put "chooseprod" in there here's the fix for that :- https://code.sololearn.com/cicJ9Y5gK0Pb/?ref=app warning: I haven't fixed the uninitialised variable "qty" which will result in garbage values in the program
11th Feb 2021, 12:31 PM
Arsenic
Arsenic - avatar
+ 5
I already did “switch(chosenprod)” and it did not skip the switch case however I wasn’t able to input the quantity of the item and the total bill computation is incorrect it always output 200ex10... what is the fix for these ?
11th Feb 2021, 6:24 PM
thatstupidcoder
thatstupidcoder - avatar
+ 4
Your code cin chosenprod, and switch productmenu.
11th Feb 2021, 12:30 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
Arsenic I tried your code but it’s not working
11th Feb 2021, 12:34 PM
thatstupidcoder
thatstupidcoder - avatar
+ 3
CarrieForle what do you mean ? can you explain further
11th Feb 2021, 12:34 PM
thatstupidcoder
thatstupidcoder - avatar
+ 3
You are not given value to qty or taking value into "qty". So its value is 0 always.. Take its value as input by input statement or assign a valid value..
11th Feb 2021, 6:47 PM
Jayakrishna 🇮🇳
+ 3
because I noticed that some people just declare their variables “int variable;” <- they do this
11th Feb 2021, 7:00 PM
thatstupidcoder
thatstupidcoder - avatar
+ 2
You are taking input to chosenprod by : cin>>chosenprod; But using productmenu value in switch case by switch(productmenu) Here productmenu value is always 0. So cases dont match. Use switch(chosenprod) { ..
11th Feb 2021, 6:22 PM
Jayakrishna 🇮🇳
+ 2
Oh so I have to initialized the qty=0 ? so whenever I want the user to input a number into a variable, I have to set that variable first into 0 ?
11th Feb 2021, 6:50 PM
thatstupidcoder
thatstupidcoder - avatar
+ 2
Yes. In c/c++, uninitialized values are set to garbage values or to 0.(compiler dependend). So its recommend to set a default value before you use it, otherwise result is unpredictable. edit: not need to 'before taking input' value.. (inputed value will be new value). its needed before you use its value..thatstupidcoder
11th Feb 2021, 6:55 PM
Jayakrishna 🇮🇳
+ 2
what are the circumstances wherein I will just declare the variable and will not initialize it to 0. For example I am only going to code “int variable;” what are the circumstances that I am going to use that
11th Feb 2021, 6:59 PM
thatstupidcoder
thatstupidcoder - avatar
+ 1
in c/c++, int var1; printf("%d", var1); // in c, outputs garbage cout<<var1; // in c++, 0 or garbage value Here in both var1 is uninitialized stil but we are using it. So we get garbage values.(deferent values in deferent compilers ..) . int var1=0; Now we get 0 as value. cin>>var1; after this statement, var1 value will be the value we given as input.
11th Feb 2021, 7:08 PM
Jayakrishna 🇮🇳
12th Feb 2021, 2:09 PM
Mahesh Muttinti
Mahesh Muttinti - avatar