question

D

Darklight

why does the program below using swtich function add the last
value enter when exitting the program snip below:

while(choice != 'q'){
printf("Total %.2f\n",result);
variable();
switch(choice){
case 'q':
value = 0;
/* this is to stop last value being add when exiting program*/
puts("exiting program");
case '+':
result += value; break;
case '-':
result -= value; break;
case '*':
result *= value; break;
case '/':
if(value == 0){
printf("Error value == 0\n");
printf("Value ignored\n");
}
else
result /= value; break;
default:
printf("Incorrect operator try again\n"); break;
}
}
printf("Total = %.2f\n",result);
return 0;
}

but if i use if & else to do the same thing
it gives the correct output

while(1) {
printf("result: %.2f\n",result);
variable();

if (( operator == 'q' || operator == 'Q')) /*to quite program */
break;

if(operator == '+') {
result += value;
} else if (operator == '-') {
result -= value;
} else if (operator == '*') {
result *= value;
} else if (operator == '/') {
if (value == 0) {
printf("Error:Divide by zero\n"); /*prints error if number = 0 */
printf(" operation ignored\n");
} else
result /= value;
} else {
printf("Unknown Operator %c\n", operator);
}
}
printf("result: %.2f\n",result);
return(0);
}
 
R

Ryan Reich

why does the program below using swtich function add the last
value enter when exitting the program snip below:

while(choice != 'q'){
printf("Total %.2f\n",result);
variable();
switch(choice){
case 'q':
value = 0;
/* this is to stop last value being add when exiting program*/
puts("exiting program");

You're falling through here; add a 'break;'. The last value isn't actually
added again, but since you have a 'printf()' statement after the loop the
last total will be printed twice.
 
M

Minti

Darklight said:
why does the program below using swtich function add the last
value enter when exitting the program snip below:

while(choice != 'q'){
printf("Total %.2f\n",result);
variable();
switch(choice){
case 'q':
value = 0;
/* this is to stop last value being add when exiting program*/
puts("exiting program");

^^^^^^^

break; // Added by ISA.

case '+':
result += value; break;
case '-':
result -= value; break;
case '*':
result *= value; break;
case '/':
if(value == 0){
printf("Error value == 0\n");
printf("Value ignored\n");
}
else
result /= value; break;
default:
printf("Incorrect operator try again\n"); break;
}
}
printf("Total = %.2f\n",result);
return 0;
}

but if i use if & else to do the same thing
it gives the correct output

while(1) {
printf("result: %.2f\n",result);
variable();

if (( operator == 'q' || operator == 'Q')) /*to quite program */
break;

if(operator == '+') {
result += value;
} else if (operator == '-') {
result -= value;
} else if (operator == '*') {
result *= value;
} else if (operator == '/') {
if (value == 0) {
printf("Error:Divide by zero\n"); /*prints error if number = 0 */
printf(" operation ignored\n");
} else
result /= value;
} else {
printf("Unknown Operator %c\n", operator);
}
}
printf("result: %.2f\n",result);
return(0);
}


Some other points.


a) Transfer the printf("%0.2f\n", result); after the end of the
switch statement. That is if you are not doing anything useful with the
'result' initially, i.e. before entering the while, by that you can snip out
the printf after the while block.
b) Write break statements on next line.
c) Your if-else code exits on both 'q' and 'Q', but your switch
block code only on q.
d) Decide on which of the while statements you want to use. Your
"while" in first snippet/code assumes that operator _could_ be set to 'q'
initially{ i.e. even before it starts to execute the statements inside
while }. But your second snippet does not i.e. you don't test for operation
!= 'q'.
 
F

Felipe Magno de Almeida

Darklight said:
why does the program below using swtich function add the last
value enter when exitting the program snip below:

while(choice != 'q'){
printf("Total %.2f\n",result);
variable();
switch(choice){
case 'q':
value = 0;
/* this is to stop last value being add when exiting program*/
puts("exiting program");
case '+':
result += value; break;
you are forgetting the breaks after the case, your code will run through
all the switch statement, you should do, on the end of each case, unless
you really want it to run the next case statement:
case '-':
result -= value; break; break;
case '*':
result *= value; break;
case '/':
if(value == 0){
printf("Error value == 0\n");
printf("Value ignored\n");
}
else
result /= value; break;
default:
printf("Incorrect operator try again\n"); break;
}
}
printf("Total = %.2f\n",result);
return 0;
}

but if i use if & else to do the same thing
it gives the correct output

while(1) {
printf("result: %.2f\n",result);
variable();

if (( operator == 'q' || operator == 'Q')) /*to quite program */
break;

if(operator == '+') {
result += value;
} else if (operator == '-') {
result -= value;
} else if (operator == '*') {
result *= value;
} else if (operator == '/') {
if (value == 0) {
printf("Error:Divide by zero\n"); /*prints error if number = 0 */
printf(" operation ignored\n");
} else
result /= value;
} else {
printf("Unknown Operator %c\n", operator);
}
}
printf("result: %.2f\n",result);
return(0);
}


--
Felipe Magno de Almeida
UIN: 2113442
email: felipe.almeida@ic unicamp br, felipe.m.almeida@gmail com
I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer
from synergy, and Computer Science student from State
University of Campinas(UNICAMP).
To know more about:
Unicamp: http://www.ic.unicamp.br
Synergy: http://www.synergy.com.br
current work: http://www.mintercept.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top