Simple IF Loop

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

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Simple IF Loop

 
0
  #1
Aug 4th, 2009
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
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Simple IF Loop

 
1
  #2
Aug 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #3
Aug 4th, 2009
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.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #4
Aug 4th, 2009
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.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #5
Aug 4th, 2009
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
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #6
Aug 4th, 2009
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Simple IF Loop

 
1
  #7
Aug 4th, 2009
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()
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #8
Aug 4th, 2009
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
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Simple IF Loop

 
0
  #9
Aug 4th, 2009
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.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 25
Reputation: PetuniaRose is an unknown quantity at this point 
Solved Threads: 1
PetuniaRose PetuniaRose is offline Offline
Light Poster

Re: Simple IF Loop

 
0
  #10
Aug 4th, 2009
Yeah - look at Aia's post. I too think that you want an "and", not an "or" in your logic code.
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