| | |
Problem with do while loop
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
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
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!!!!!
The solution I tried
c Syntax (Toggle Plain Text)
{ #include<stdio.h> #include<conio.h> void main() { float a,b,c; char sign,answer; clrscr(); do { printf("Enter your choice of operator\n"); printf("+ for addition\n"); printf("- for subtraction\n"); printf("* for multiplication\n"); printf("/ for division\n"); scanf("%c",&sign); printf("Enter two numbers\n"); scanf("%f %f",&a,&b); switch(sign) { case '+': c=a+b; printf("The addition is %f\n",c); break; case '-': c=a-b; printf("The subtraction is %f\n",c); break; case '*': c=a*b; printf("The product is %f\n",c); break; case '/': c=a/b; printf("The division is %f\n",c); break; } printf("Do you want to continue?(y or no)\n"); scanf("%c",&answer); }while(answer!='n'); } }
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...
•
•
•
•
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!!!!!
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.
•
•
Join Date: May 2007
Posts: 6
Reputation:
Solved Threads: 3
Try put a space before the %c
C Syntax (Toggle Plain Text)
scanf(" %c",&answer);
What?
>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.
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.
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.
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...
i works perfectly, as i said... but i wouldn't use it myself...
C Syntax (Toggle Plain Text)
printf("Do you want to continue?(y or no)\n"); getchar(); scanf("%c",&answer);
Last edited by Nichito; May 3rd, 2007 at 2:41 pm.
-->sometimes i wanna take my toaster in a bath<-- •
•
•
•
Originally Posted by Narue
I'd wager the OP is not using C++.
-->sometimes i wanna take my toaster in a bath<-- ![]() |
Similar Threads
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- C++ Calculating even Values Problem (C++)
- problem in displayin records on a form (VB.NET)
- C++ Newbie, stumped with problem (C++)
- help with my for loop (C++)
- My loop won't work, but I'm positive it should. (Python)
- PHP + MySQL Problem (PHP)
- Help with while loop for hangman project (C++)
- varible not working in for loop (Java)
- Help with loop (C++)
Other Threads in the C Forum
- Previous Thread: Anyone know of a good place to look for the bht.h library?
- Next Thread: Tolerance
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






