| | |
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 |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





