If else statement

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 29
Reputation: lonely_girl is an unknown quantity at this point 
Solved Threads: 0
lonely_girl's Avatar
lonely_girl lonely_girl is offline Offline
Light Poster

If else statement

 
0
  #1
Jan 28th, 2009
HI
I need a little help with this program

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main ()
  4. {
  5. float num1,num2,result=0;
  6. char ch;
  7. clrscr();
  8. printf("\nEnter 1st num:");
  9. scanf("%f",&num1);
  10. printf("\nEnter 2nd num:");
  11. scanf("%f",&num2);
  12. wrong:
  13. printf("\nEnter + for addition"
  14. "\nEnter - for subtraction"
  15. "\nEnter / for division"
  16. "\nEnter * for multiplication\n");
  17. scanf(" %c",&ch);
  18. if (ch=='+')
  19. result=num1+num2;
  20. else
  21. if (ch=='-')
  22. result=num1-num2;
  23. else
  24. if (ch=='/')
  25. result=num1/num2;
  26. else
  27. if (ch=='*')
  28. result=num1*num2;
  29. else
  30. {
  31. printf("NO MATCH FOUND");
  32. goto wrong;
  33. }
  34. printf("Result=%f",result);
  35. getch();
  36. }

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: If else statement

 
0
  #2
Jan 28th, 2009
Use int main(), indent your code, use switch cases and a while loop instead of goto and if cases.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: If else statement

 
0
  #3
Jan 28th, 2009
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.
Last edited by csurfer; Jan 28th, 2009 at 12:56 pm. Reason: Mistake in some satements
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: If else statement

 
0
  #4
Jan 28th, 2009
Originally Posted by csurfer View Post
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;
That doesn't make much sense. What you said(IF you said what I think you said) can be achieved more conveniently using a loop.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: If else statement

 
0
  #5
Jan 28th, 2009
I just said that loops are the best way to achieve the problem.

And if the person wants to continue in the format she has coded then she can use "goto" as mentioned above.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: lonely_girl is an unknown quantity at this point 
Solved Threads: 0
lonely_girl's Avatar
lonely_girl lonely_girl is offline Offline
Light Poster

Re: If else statement

 
0
  #6
Jan 29th, 2009
hmmm thx devnar and csurfer that helped i understood my mistake and the new logic...
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