Problem with do while loop

Thread Solved

Join Date: Feb 2007
Posts: 31
Reputation: IwalkAlone is an unknown quantity at this point 
Solved Threads: 0
IwalkAlone IwalkAlone is offline Offline
Light Poster

Problem with do while loop

 
0
  #1
May 3rd, 2007
Q.Create an equivalent of four function calculator. The program should request the user to enter two numbers and an operator. It should carry out specified arithmetic operation on the two numbers( using switch case).After displaying the result, the program should ask the user if he/she wants to do another calculation.If 'y', make it perform another operation.

The solution I tried

  1. {
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. float a,b,c;
  7. char sign,answer;
  8. clrscr();
  9. do
  10. {
  11. printf("Enter your choice of operator\n");
  12. printf("+ for addition\n");
  13. printf("- for subtraction\n");
  14. printf("* for multiplication\n");
  15. printf("/ for division\n");
  16. scanf("%c",&sign);
  17. printf("Enter two numbers\n");
  18. scanf("%f %f",&a,&b);
  19. switch(sign)
  20. {
  21. case '+': c=a+b;
  22. printf("The addition is %f\n",c);
  23. break;
  24. case '-': c=a-b;
  25. printf("The subtraction is %f\n",c);
  26. break;
  27. case '*': c=a*b;
  28. printf("The product is %f\n",c);
  29. break;
  30. case '/': c=a/b;
  31. printf("The division is %f\n",c);
  32. break;
  33. }
  34. printf("Do you want to continue?(y or no)\n");
  35. scanf("%c",&answer);
  36. }while(answer!='n');
  37. }
  38. }
Problem is that although everything else works fine, when the question "Do you want to continue ?(y or no)" is asked, it doesn't take in the answer and straight away goes ahead to provide the options again.Help!!!!!
Last edited by WaltP; May 5th, 2007 at 3:40 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Problem with do while loop

 
0
  #2
May 3rd, 2007
Originally Posted by IwalkAlone View Post
Problem is that although everything else works fine, when the question "Do you want to continue ?(y or no)" is asked, it doesn't take in the answer and straight away goes ahead to provide the options again.Help!!!!!
That's because the char you entered is still in the buffer. It's a bug in scanf(), you shouldn't use it. Link
Also:
-the defenition of main is int main(void)
-What is that bracket on the first line?
-clrscr(); isn't standard nor is Conio.h
-main should end with a return 0;
-please use -codetags- the next time you post

Regards Niek
Last edited by niek_e; May 3rd, 2007 at 10:06 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with do while loop

 
0
  #3
May 3rd, 2007
>It's a bug in scanf(), you shouldn't use it.
It's not a bug in scanf, it's a bug in the code that uses scanf. But I do agree that if you don't know how to use something, you shouldn't use it until you do.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 6
Reputation: stupido is an unknown quantity at this point 
Solved Threads: 3
stupido stupido is offline Offline
Newbie Poster

Re: Problem with do while loop

 
0
  #4
May 3rd, 2007
Try put a space before the %c
  1. scanf(" %c",&answer);
What?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with do while loop

 
0
  #5
May 3rd, 2007
>Try put a space before the %c
In my experience, if you say "Try <such and such>", you don't really know what's going on. If you do know that your suggestion works and why it works, please be sure to explain it so that you don't encourage inserting random characters with the "try it and see" attitude.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem with do while loop

 
0
  #6
May 3rd, 2007
Read this and the related articles for a better understanding of scanf.
Last edited by ~s.o.s~; May 3rd, 2007 at 1:43 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Problem with do while loop

 
0
  #7
May 3rd, 2007
actually, i would recommend to use cin and cout, but i assume you are being taught to work with scanf and printf, so i will provide a secondary solution, which i wouldn't recommend to an experienced programmer...
  1. printf("Do you want to continue?(y or no)\n");
  2. getchar();
  3. scanf("%c",&answer);
i works perfectly, as i said... but i wouldn't use it myself...
Last edited by Nichito; May 3rd, 2007 at 2:41 pm.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem with do while loop

 
0
  #8
May 3rd, 2007
cin and cout don't belong to C language...
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with do while loop

 
0
  #9
May 3rd, 2007
>actually, i would recommend to use cin and cout
Those tend not to work in C. Judging from the code, I'd wager the OP is not using C++.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Problem with do while loop

 
0
  #10
May 3rd, 2007
Originally Posted by Narue
I'd wager the OP is not using C++.
there's where i will not agree... I can bet the OP is using Turbo C++...That's exactly the same kind of code they were making me use when i started on Turbo... that's why i came out with that solution... (actually that's what i did when the buffer was filled up...)
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC