0

I decided to make a calcutor using code but my program just wont work. When i enter my operand and new number it wont seem to scan the operand and number and it wont start the loop. Thanks for the help.

#include <stdio.h>
#include <math.h>

float add(float x,float y);
float sub(float x,float y);
float div(float x,float y);
float exp(float x,float y);
float mult(float x,float y);
int main(){

float y,x;
char op;

printf("Type in a number\n");
scanf("%f",&x);
printf("Type in your operand and desired number\n");
scanf("%c",&op);
scanf("%f",&y);


while (!(op=='q')){
    if(op=='+'){
    printf("Your result is %.1f\n",add(x,y));
    scanf("%c",&op);
    scanf("%f",&y);
    }
    else if(op=='-'){
    printf("Your result is %.1f\n",sub(x,y));
    scanf("%c",&op);
    scanf("%f",&y);
    }
    else if(op=='*'){
    printf("Your result is %.1f\n",mult(x,y));
    scanf("%c",&op);
    scanf("%f",&y);
    }
    else if(op=='/'){
    printf("Your result is %.1f\n",div(x,y));
    scanf("%c",&op);
    scanf("%f",&y);
    }
    else if(op=='^'){
    printf("Your result is %.1f\n",exp(x,y));
    scanf("%c",&op);
    scanf("%f",&y);
    }

}

    printf("Your final result is %.1f\n",x);

        return(0);
}

float add(float x,float y){
return (x+y);

}

float sub(float x,float y){
return (x-y);
}

float div(float x,float y){
return (x/y);
}

float exp(float x,float y){
x=pow(x,y);
return(x);
}
float mult(float x,float y){
return (x*y);
}
Nez Y
  • 31
  • 1
  • 1
  • 3
  • Your function exp is conflicting rename it . – Indra Yadav Sep 05 '13 at 10:19
  • yes i know i renamed it something else thanks – Nez Y Sep 05 '13 at 10:29
  • The idea is pretty simple but Your solution isn't good, Mixing reading numbers and characters with scanf is really painful because of the white spaces scanf leaves. Also, those ifs look nicer when put in switch. You don't handle the case when user doesn't input correct operand. – zubergu Sep 05 '13 at 10:43
  • im still a beginner in c so im learning but this while(getchar()!='\n') continue; – Nez Y Sep 05 '13 at 10:51
  • is getting me confused and should i type in that getchar like right after the first scanf in the loop or after both of the scanfs...sorry if im having trouble understanding where to place them – Nez Y Sep 05 '13 at 10:52
  • getchar is in the loop condition, it's at the right place, use it and it will work. Why it can be there is another subject. What it does, it reads character and checks if it's new line, if it isn't \n it goes to the next char and discards what it reads. When it reads '\n' it stops. Buffor is empty and you can read your character safely now:) – zubergu Sep 05 '13 at 10:56

2 Answers2

1

when you do

scanf("%c",&op);

you read first char that is in the input buffer. previous scanf left \n char in it, so you read that char.

What you want to do, is to get rid of all what's left behind scanf.

while(getchar()!='\n')
  continue;

That will empty the buffer before you try to read.

Every use of scanf here will leave new line character in the buffer so to get rid of him, use above loop every time you try to read a character from input and you know that newline is there.

zubergu
  • 3,646
  • 3
  • 25
  • 38
  • Code asks for a number, reads it, and then it asks for operand and second number so it will work fine. – zubergu Sep 05 '13 at 10:26
  • Nothing? Goes to scanf(%c...) as it's supposed to. – zubergu Sep 05 '13 at 10:36
  • between first and 2nd scanf you put the loop I gave you. – zubergu Sep 05 '13 at 10:39
  • And in every place when you read character from input right after scanf. That's the price for using scanf for reading both values and characters. You Just need to concentrate on what's in the buffer all the time. – zubergu Sep 05 '13 at 10:45
  • its working for the first part but then when i type in another operand and value it just doesnt calcuate – Nez Y Sep 05 '13 at 10:48
  • im typing in while(getchar()!='\n') continue; – Nez Y Sep 05 '13 at 10:48
  • @NezY look at my previous comments, every time you read a character after scanf you need to empty the buffer with this loop. So it means use it in every if after you print your calculations. too. – zubergu Sep 05 '13 at 10:51
  • Yes, I tried.Have You? It's you not understanding me or me not understanding you. – zubergu Sep 05 '13 at 11:08
  • IT won't execute the loop when finds \n! You clearly didn't try it as i asked you gently. – zubergu Sep 05 '13 at 11:16
  • Ooops. I did have it backwards. Sorry. I'll delete all my comments. :( – luser droog Sep 05 '13 at 11:17
  • @luserdroog finally:) you almost convinced me that it's wrong even if i used it for long time:) – zubergu Sep 05 '13 at 11:18
  • I did it backwards in my own answer. Fixed now (I hope). Thanks for your patience. :) And +1! – luser droog Sep 05 '13 at 11:20
  • 1
    @luserdroog this can happen to anyone, anywhere, anytime :) – zubergu Sep 05 '13 at 11:30
  • so its doing the calculations but now it wont work with the last value...like lets say i typed in 10,then *2 ,t will calcuate 20,then when i type in + 3 it will calculate 13,not 23 as it should...what seems to be the problem? – Nez Y Sep 05 '13 at 16:26
1

I think what's happening is that the newline character (the return/enter key) is left over in the input stream after the scanf("%f",&y); call and that's what being stored as the single character in the scanf("%c",&op); call.

So you'll need to discard the newline character at that point. Simplest way is to call scanf("%c",&op); twice when you need to read the single character. This should work on Mac and Unix. For Windows, you may need to read the character three times because Windows often considers the sequence "\r\n" as a newline sequence.

For portability, you can use a loop like this:

do {
    op = getchar();
} while (op == '\n' || op == '\r');

And remove the scanf("%c",&op);. This loop replaces it.


Another option is to ask scanf itself to discard initial whitespace.

scanf(" %c",&op);
//     ^ space

Also, see my answer to this very similar question.

Community
  • 1
  • 1
luser droog
  • 18,988
  • 3
  • 53
  • 105