| | |
Simple IF Loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
Everything else seems to be pretty good I think, but this is aggravating me to no end.
Thanks
c Syntax (Toggle Plain Text)
#include <stdio.h> // standard input output library #include <string.h> // basic string handling library added for cAgain option //Assigns tax value for calculations #define DelMar 7.25 #define Encinitas 7.5 #define LaJolla 7.75 char cAgain; //repeat variable for Y/N questions int displayMenu() // creates menu { int choice; printf("Tax Calculation for $125 Purchase\n\n"); printf("1. Del Mar \n"); printf("2. Encinitas \n"); printf("3. La Jolla \n"); printf("\n\nPlease Select Store for Which to Calculate [1-3]:"); scanf("%d",&choice); return choice; } int main() { YES: { // Sales value for calculations float sales = 125.00; int storenum; storenum = displayMenu(); if storenum >=1 || if storenum <=3 //While Loop to Request Input of 1,2,or 3, still working on this { switch (storenum) // simple switch which responds to corresponding menu choice with output { case 1: //Calculates and Displays Store, Sales, Tax rate, Tax for Del Mar 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. break; case 2: //Calculates and Displays Store, Sales, Tax rate, and Tax for Encinitas printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100); break; case 3: //Calculates and Displays Store, Sales, Tax rate, and Tax for La Jolla printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100); break; } //Storenum switch end } else { fflush(stdin); printf("\nPlease enter 1,2 or 3\n"); printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows repeat scanf("%c", &cAgain); //scan input to repeat program if (cAgain == 'y' || cAgain == 'Y')//repeats calculator { goto YES; //back to menu } else if (cAgain == 'n' || cAgain == 'N')//sends to end return 0; } printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat scanf("%c", &cAgain); if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator upper or lower case { goto YES; //sends user back to the begining } else if (cAgain == 'n' || cAgain == 'N')//ends program upper or lower case { printf("\n\tPlease press enter to exit\t\n"); //prompts for exit } return 0; } }
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.
I'm no ones son, unforgiven.
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]
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]
c Syntax (Toggle Plain Text)
if (storenum >= 1 && storenum <= 3) { /* do something */ }
Last edited by Aia; Aug 4th, 2009 at 2:59 pm.
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:
A while loop looks like:
Shorten your code.
What is an IF loop? I think you mean while loop.
I hope this wasnt suppost to be your loop:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
while(/*condition here*/) { /* Code here */ }
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
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
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.
I'm no ones son, unforgiven.
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()
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
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.
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
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.
C Syntax (Toggle Plain Text)
if (storenum >=1 && if storenum <=3) //While Loop to Request Input of 1,2,or 3, still working on this { switch (storenum) // simple switch which responds to corresponding menu choice with output } { case 1: //Calculates and Displays Store, Sales, Tax rate, Tax for Del Mar 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. break; case 2: //Calculates and Displays Store, Sales, Tax rate, and Tax for Encinitas printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100); break; case 3: //Calculates and Displays Store, Sales, Tax rate, and Tax for La Jolla printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100); break; } //Storenum switch end } else { fflush(stdin); printf("\nPlease enter 1,2 or 3\n"); printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows repeat scanf("%c", &cAgain); //scan input to repeat program if (cAgain == 'y' || cAgain == 'Y')//repeats calculator { goto YES; //back to menu } else if (cAgain == 'n' || cAgain == 'N')//sends to end return 0; }
'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.
I'm no ones son, unforgiven.
![]() |
Similar Threads
- simple while loop (C++)
- trying to loop my program (C++)
- need help on simple turbo C program.. thx (C)
- another simple question concerning nodes (C++)
- A simple problem (C)
- Can't get simple loop to work (C++)
- trying to make a "Do While" loop; loop (C++)
- Getting funky errors with a simple for loop, anyone care to help? (JavaScript / DHTML / AJAX)
- newcommer to c++ need help with simple program (C++)
Other Threads in the C Forum
- Previous Thread: Function acting weird
- Next Thread: Need help for run time speaker indicator
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






