problem in c code ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

problem in c code ???

I am currently working on C and I have encountered a problem with one of the programs I am trying to apply to. #include <stdio.h> #include <stdlib.h> int main(){ int x,y; char opt; char r; do{ printf("============================== calculator ==============================\n"); printf("enter the first number : ");scanf("%d", &x); printf("enter the second number : ");scanf("%d", &y); do{ printf("chose operation between '+' or '-' or '*' or '/' or '^' :"); scanf("%c", &opt); }while(opt != '+' && opt != '-' && opt != '*' && opt != '/' && opt != '^'); switch (opt) { case '+': printf("%d + %d = %d\n", x, y, x+y);break; case '-': printf("%d - %d = %d\n", x, y, x-y);break; case '*': printf("%d * %d = %d\n", x, y, x*y);break; case '/': printf("%d / %d = %d\n", x, y, x/y);break; case '^': printf("%d ^ %d = %d\n", x, y, x^y);break; } do{ printf("do you would do another calculation y/n ? ");scanf("%c", &r); }while(r != 'y' && r != 'n'); }while(r=='y'); return 0; } The problem is that when I run the program this code is repeated twice for the printf, do{ printf("chose operation between '+' or '-' or '*' or '/' or '^' :"); scanf("%c", &opt); }while(opt != '+' && opt != '-' && opt != '*' && opt != '/' && opt != '^'); he writes the message twice and then asks me to enter the process i don't know why The same problem is repeated in do{ printf("do you would do another calculation y/n ? ");scanf("%c", &r); }while(r != 'y' && r != 'n');

27th Dec 2019, 7:29 PM
Youssef Elouasbi
Youssef Elouasbi - avatar
3 Answers
+ 1
thank you very much :)
27th Dec 2019, 9:18 PM
Youssef Elouasbi
Youssef Elouasbi - avatar
0
The problem is he writes like this chose operation between '+' or '-' or '*' or '/' or '^' :chose operation between '+' or '-' or '*' or '/' or '^' :
27th Dec 2019, 8:18 PM
Youssef Elouasbi
Youssef Elouasbi - avatar
0
can you please write me the code ? I tried but I couldn't
27th Dec 2019, 8:52 PM
Youssef Elouasbi
Youssef Elouasbi - avatar