| | |
If else statement
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
HI
I need a little help with this program
Ive used goto statement in case if wrong character is given for input and this goto statement will rerun the program till the time correct character is given as input what should i do if i want to run it only for selected number of times(in case of wrong character)????
I need a little help with this program
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main () { float num1,num2,result=0; char ch; clrscr(); printf("\nEnter 1st num:"); scanf("%f",&num1); printf("\nEnter 2nd num:"); scanf("%f",&num2); wrong: printf("\nEnter + for addition" "\nEnter - for subtraction" "\nEnter / for division" "\nEnter * for multiplication\n"); scanf(" %c",&ch); if (ch=='+') result=num1+num2; else if (ch=='-') result=num1-num2; else if (ch=='/') result=num1/num2; else if (ch=='*') result=num1*num2; else { printf("NO MATCH FOUND"); goto wrong; } printf("Result=%f",result); getch(); }
Ive used goto statement in case if wrong character is given for input and this goto statement will rerun the program till the time correct character is given as input what should i do if i want to run it only for selected number of times(in case of wrong character)????
Last edited by lonely_girl; Jan 28th, 2009 at 12:32 pm.
Loops are always better than goto but with goto I think this can be done:
You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:
cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;
if(cnt>0)
goto wrong;
----------------------------------------------
And your ' / ' operator will generate an error if num2 is 0 which is not taken care of in the code.
You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:
cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;
if(cnt>0)
goto wrong;
----------------------------------------------
And your ' / ' operator will generate an error if num2 is 0 which is not taken care of in the code.
Last edited by csurfer; Jan 28th, 2009 at 12:56 pm. Reason: Mistake in some satements
I Surf in "C"....
•
•
•
•
Loops are always better than goto but with goto I think this can be done:
You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:
cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;
if(cnt>0)
goto wrong;
![]() |
Similar Threads
- missing return statement (Java)
- MySQL LIKE statement (MySQL)
- loop in main function to an "if" statement (C++)
- Switch Case Statement (Java)
- run sql statement in asp (ASP)
- switch/case statement (C++)
- change statement (JSP)
- Reading MSWord Document through an ASP Statement (ASP)
Other Threads in the C Forum
- Previous Thread: HELP..!!
- Next Thread: How to read null-terminated strings from file?
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi





