简单的C语言问题
先不说这个源码的别问题。。。为啥老是算的加法啊。。郁闷死了debug了judge明明不是1的啊。
- C/C++ code
#include<stdio.h>char choice(void);char get(void);void now(int h);int main(void){ int ch,judge; float first,second; while((ch=choice())!='q'){ switch(ch) { case 'a':judge=1;break; case 'b':judge=2;break; case 'c':judge=3;break; case 'd':judge=4;break; default:printf("error");break; } now(judge); } printf("bye\n"); return 0;}char choice(void){ int ch; printf("please enter the operation of your choice\n"); printf("a.add b.subtract c.multiply d.divide q.quit\n"); ch=get(); while((ch<'a'||ch>'d')&& ch!='q'){ printf("please respond with a,b,c,d or q\n"); ch=get();} return ch;}char get(void){ int ch; ch=getchar(); while(getchar()!='\n') continue; return ch;}void now(int h){ float a,b; char ch; printf("the first number:"); while((scanf("%f",&a)!=1)) { while((ch=getchar())!='\n') putchar(ch); printf(":is not a unmber\n"); printf("please enter number\n"); printf("the first number:"); } printf("the second number:"); while((scanf("%f",&b)!=1)) { while((ch=getchar())!='\n') putchar(ch); printf(":is not a unmber\n"); printf("please enter number\n"); printf("the second number:"); } if(h=1) printf("%f+%f=%f\n",a,b,a+b); else if(h=2) printf("%f-%f=%f\n",a,b,a-b); else if(h=3) printf("%f*%f=%f\n",a,b,a*b); else if(h=4) printf("%f/%f=%f\n",a,b,a/b); }先不说这个源码的别问题。。。为啥老是算的加法啊
[解决办法]
if(h=1)
都要该成
if(h==1)