+ 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
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
+ 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 ?
+ 4
Your code cin chosenprod, and switch productmenu.
+ 3
Arsenic I tried your code but it’s not working
+ 3
CarrieForle what do you mean ? can you explain further
+ 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..
+ 3
because I noticed that some people just declare their variables
“int variable;” <- they do this
+ 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)
{
..
+ 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 ?
+ 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
+ 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
+ 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.