943,808 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 692
  • C RSS
Jan 28th, 2009
0

If else statement

Expand Post »
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.
Similar Threads
Reputation Points: 19
Solved Threads: 0
Light Poster
lonely_girl is offline Offline
29 posts
since Nov 2008
Jan 28th, 2009
0

Re: If else statement

Use int main(), indent your code, use switch cases and a while loop instead of goto and if cases.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Jan 28th, 2009
0

Re: If else statement

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
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jan 28th, 2009
0

Re: If else statement

Click to Expand / Collapse  Quote originally posted by csurfer ...
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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Jan 28th, 2009
0

Re: If else statement

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jan 29th, 2009
0

Re: If else statement

hmmm thx devnar and csurfer that helped i understood my mistake and the new logic...
Reputation Points: 19
Solved Threads: 0
Light Poster
lonely_girl is offline Offline
29 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: HELP..!!
Next Thread in C Forum Timeline: How to read null-terminated strings from file?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC