PDA

View Full Version : [C] printf/scanf in loop problem



johnlouis
February 19th, 2007, 05:03 AM
#include <stdio.h>
int a,b;
int add(a,b){
int sum = a+b;
return(sum);
}
int sub(a,b){
int diff = a-b;
return(diff);
}
int mult(a,b){
int prod = a*b;
return(prod);
}
int div(a,b){
int quot = a/b;
return(quot);
}
int rem(a,b){
int rem = a%b;
return(rem);
}
main()
{
int x,y,ans;
char op;
while(op!='x'||op!='X'){
printf("\n[A] Addition\n[b] Subtraction\n[c] Multiplication\n[D] Division\n[E] Remainder\n");
printf("\nWhich operation? ");
scanf("%c",&op);
if(op=='a'||op=='A'){
printf("\nInput 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = add(x,y);
printf("\nAnswer is: %d\n",ans);

}else if(op=='b'||op=='B'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = sub(x,y);
printf("\nAnswer is: %d\n",ans);

}else if(op=='c'||op=='C'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = mult(x,y);
printf("\nAnswer is: %d\n",ans);

}else if(op=='d'||op=='D'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = div(x,y);
printf("\nAnswer is: %d\n",ans);

}else if(op=='e'||op=='E'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = rem(x,y);
printf("\nAnswer is: %d\n",ans);

}else if(op=='x'||op=='X'){
break;
}else{
printf("\nTry again\n");

}
}
printf("\n**Program Terminated**\n");
}we were given an assignment in class today. in the above code, i am trying to achieve where a user chooses an operation(a,b,c,d,e), and inputs 2 numbers and outputs the answers. it works fine but after the first loop, the first 3 lines of the loop are executed twice.. :puzzled:
why is this and how do i fix it? :book:

Ben H
February 21st, 2007, 01:04 PM
<IGNORE>
Don't have time to read all of your code, I'm in Physics right now :P, but it looks like this is the problem:


while(op!='x'||op!='X'){
as both of the conditions are being met.
Try this:


while( op!=('x'||'X') ){
Hope that helps! :thumb:

-Ben
</IGNORE>
EDIT: Ignore that, didn't read properly :P. You're checking for the value of op before you have assigned it one, so it loops 'til it has one.

If you don't want to display the menu every time the loop starts, then use this:



char op;
printf("\n[A] Addition\n[b] Subtraction\n[c] Multiplication\n[D] Division\n[E] Remainder\n");
printf("\nWhich operation? ");
scanf("%c",&op);
while(op!='x'||op!='X'){
If you do want it to display, then use this:



char op;
while(op!='x'||op!='X'){
if (!op){
printf("\n[A] Addition\n[b] Subtraction\n[c] Multiplication\n[D] Division\n[E] Remainder\n");
printf("\nWhich operation? ");
scanf("%c",&op);
}
and at the end of the loop:


op = "\0";
}
Ugly, but I can't be bothered to think it out now.

johnlouis
February 22nd, 2007, 10:03 AM
hey, thanks Ben H. i've solved it using %s instead of %c(anyone care to explain why?).. but i'll try your suggestion too. thanks! :)

Ben H
February 22nd, 2007, 12:08 PM
hey, thanks Ben H. i've solved it using %s instead of %c(anyone care to explain why?).. but i'll try your suggestion too. thanks! :)

%s reads a string of characters, %c reads a character. If you've got it working (with a more elegant solution than mine ;)), then you don't need to change it. Don't bother trying mine :P

-Ben