1
I’m having trouble incrementing using the switch
, for example, I want to use an accelerator and every time I call the case '+':
it makes two example increments:
potencia=potencia+10;
altitude=altitude+1000;
One of them, the second is returning incorrect values, I made the code in javascript, works perfectly, now in C am not getting.
Follows code below and image attached.
int main(){
int potencia=0;
int altitude=0;
char acelerador;
do{
printf("\n +Use (+) para aumentar velocidade/ (-) deminuir:");
gets(&acelerador);
switch(acelerador){
case '+':
potencia=potencia+10;
altitude=altitude+1000;
printf("\n %d",potencia);
printf("\n %d",altitude);
}
}while(acelerador !='s');
system("PAUSE");
return 0;
}
You made a mistake with the throttle variable. Declared as a char (a single character) but is using the gets function to read such a variable, however the gets function reads a string or array of characters and adds the ' 0' terminator. It may be that your reading is destroying the memory area of the variable that is in trouble. We usually close the commands of a case with the break command; otherwise it will continue the execution of the following commands even if they belong to another case.
– anonimo
I ran your code smoothly here, the values came out correct. Detail, do not forget to finish one
case
ofswitch
withbreak
.– Miyukii
It really, I circled on another platform and consisted error, I switched to scanf on the char reading and rode quiet. @anonimo
– Miyukii