944,008 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2024
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2009
0

Simple IF Loop

Expand Post »
Hey guys, maybe I am just worn out today, but can somebody look at my IF loop (the first one for selcting 1-3) and see where I am going wrong?
Everything else seems to be pretty good I think, but this is aggravating me to no end.
Thanks

  1. #include <stdio.h> // standard input output library
  2. #include <string.h> // basic string handling library added for cAgain option
  3.  
  4. //Assigns tax value for calculations
  5. #define DelMar 7.25
  6. #define Encinitas 7.5
  7. #define LaJolla 7.75
  8.  
  9. char cAgain; //repeat variable for Y/N questions
  10.  
  11. int displayMenu() // creates menu
  12. {
  13. int choice;
  14. printf("Tax Calculation for $125 Purchase\n\n");
  15. printf("1. Del Mar \n");
  16. printf("2. Encinitas \n");
  17. printf("3. La Jolla \n");
  18. printf("\n\nPlease Select Store for Which to Calculate [1-3]:");
  19.  
  20. scanf("%d",&choice);
  21. return choice;
  22. }
  23.  
  24. int main()
  25. {
  26. YES:
  27. {
  28. // Sales value for calculations
  29. float sales = 125.00;
  30. int storenum;
  31.  
  32.  
  33. storenum = displayMenu();
  34. if storenum >=1 || if storenum <=3 //While Loop to Request Input of 1,2,or 3, still working on this
  35. {
  36.  
  37. switch (storenum) // simple switch which responds to corresponding menu choice with output
  38. {
  39. case 1:
  40. //Calculates and Displays Store, Sales, Tax rate, Tax for Del Mar
  41. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, DelMar, sales*DelMar/100); //values are correct so I am leaving it this way. ahead of schedule here.
  42. break;
  43. case 2:
  44. //Calculates and Displays Store, Sales, Tax rate, and Tax for Encinitas
  45. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100);
  46. break;
  47. case 3:
  48. //Calculates and Displays Store, Sales, Tax rate, and Tax for La Jolla
  49. printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100);
  50. break;
  51. } //Storenum switch end
  52. }
  53. else
  54. {
  55. fflush(stdin);
  56. printf("\nPlease enter 1,2 or 3\n");
  57.  
  58. printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows repeat
  59. scanf("%c", &cAgain); //scan input to repeat program
  60.  
  61. if (cAgain == 'y' || cAgain == 'Y')//repeats calculator
  62. {
  63. goto YES; //back to menu
  64. }
  65. else if (cAgain == 'n' || cAgain == 'N')//sends to end
  66.  
  67. return 0;
  68. }
  69.  
  70.  
  71. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  72. scanf("%c", &cAgain);
  73.  
  74. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator upper or lower case
  75. {
  76. goto YES; //sends user back to the begining
  77. }
  78. else if (cAgain == 'n' || cAgain == 'N')//ends program upper or lower case
  79. {
  80. printf("\n\tPlease press enter to exit\t\n"); //prompts for exit
  81. }
  82.  
  83. return 0;
  84. }
  85. }
Last edited by John A; Aug 4th, 2009 at 11:29 pm. Reason: fixed code tags
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
1

Re: Simple IF Loop

no1zson> can somebody look at my IF loop (the first one for selcting 1-3) and see where I am going wrong?

I am sure we can guess what you are trying to do, but don't you think it would be more effective if you tell us what is supposed to do and what's doing which is not the expected result?

no1zson> if storenum >=1 || if storenum <=3]

  1. if (storenum >= 1 && storenum <= 3) {
  2. /* do something */
  3. }
Last edited by Aia; Aug 4th, 2009 at 2:59 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 4th, 2009
0

Re: Simple IF Loop

Switch [\code] with [/code].
Shorten your code.
What is an IF loop? I think you mean while loop.

I hope this wasnt suppost to be your loop:
  1. if storenum >=1 || if storenum <=3 //While Loop to Request Input of 1,2,or 3, still working on this

A while loop looks like:
  1. while(/*condition here*/) {
  2. /* Code here */
  3. }
Last edited by Hiroshe; Aug 4th, 2009 at 3:04 pm.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 4th, 2009
0

Re: Simple IF Loop

Whoops! Double post. Sorry Aia, it took about 5 minute's to post (daniweb seems slow today).
Last edited by Hiroshe; Aug 4th, 2009 at 3:03 pm.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 4th, 2009
0

Re: Simple IF Loop

Sorry. You are right. I am just having one of those days.

If the user selects 1, 2 or 3 then they should be sent to the CASE switch, otherwise down to the INVALID error message and forced to reenter the correct number.

Right now I am getting a compile error.
'if storenum >=1 || if storenum <=3 { switch (storenum) { case 1: printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, 7.25, sales*7.25/100)'
aborting compile
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
0

Re: Simple IF Loop

Hiroshe,
I started with a While loop and decided to change.

What I am now trying to do is IF 1 2 or 3, go to the appropriate CASE statement,
ELSE go to my INVALID error statement.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
1

Re: Simple IF Loop

Don't use goto, try a function. You already have a switch in an if statement, don't make it any more complex.

Avoid fflush() and look into a replacement of scanf()
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Aug 4th, 2009
0

Re: Simple IF Loop

Sorry MosaicFuneral, I do not even know what that means.
If I could I would like to get it working correctly as I have it in my mind and then refine from there. I also have a few ideas in that arena, but first want to execute to see how this works.

I have adjusted my code, I think I am close but it will not compile.
  1. if (storenum >=1 && if storenum <=3) //While Loop to Request Input of 1,2,or 3, still working on this
  2. {
  3.  
  4. switch (storenum) // simple switch which responds to corresponding menu choice with output
  5. }
  6. {
  7. case 1:
  8. //Calculates and Displays Store, Sales, Tax rate, Tax for Del Mar
  9. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, DelMar, sales*DelMar/100); //values are correct so I am leaving it this way. ahead of schedule here.
  10. break;
  11. case 2:
  12. //Calculates and Displays Store, Sales, Tax rate, and Tax for Encinitas
  13. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100);
  14. break;
  15. case 3:
  16. //Calculates and Displays Store, Sales, Tax rate, and Tax for La Jolla
  17. printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100);
  18. break;
  19. } //Storenum switch end
  20. }
  21. else
  22. {
  23. fflush(stdin);
  24. printf("\nPlease enter 1,2 or 3\n");
  25.  
  26. printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows repeat
  27. scanf("%c", &cAgain); //scan input to repeat program
  28.  
  29. if (cAgain == 'y' || cAgain == 'Y')//repeats calculator
  30. {
  31. goto YES; //back to menu
  32. }
  33. else if (cAgain == 'n' || cAgain == 'N')//sends to end
  34.  
  35. return 0;
  36. }
This is the error I see when I compile.
'if (storenum >=1 && if storenum <=3) { switch (storenum) } { case 1: printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, 7.25, sales*7.25/100)'
aborting compile
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
0

Re: Simple IF Loop

Why do you need an if statement in the first place? Just use switch-case (using default).
Last edited by Hiroshe; Aug 4th, 2009 at 3:18 pm.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 4th, 2009
0

Re: Simple IF Loop

Yeah - look at Aia's post. I too think that you want an "and", not an "or" in your logic code.
Reputation Points: 23
Solved Threads: 1
Light Poster
PetuniaRose is offline Offline
25 posts
since Jun 2009

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: Function acting weird
Next Thread in C Forum Timeline: Need help for run time speaker indicator





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


Follow us on Twitter


© 2011 DaniWeb® LLC