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

``````#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 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;

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("\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
{
}
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;
}
}``````

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]

``````if (storenum >= 1 && storenum <= 3) {
/* do something */
}``````
commented: thanks for the tip +3

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

I hope this wasnt suppost to be your loop:

``````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:

``````while(/*condition here*/) {
/* Code here */
}
``````

Whoops! Double post. Sorry Aia, it took about 5 minute's to post (daniweb seems slow today).

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

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.

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()

commented: thanks for the effort +3

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.

``````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("\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
{
}
else if (cAgain == 'n' || cAgain == 'N')//sends to end

return 0;
}``````

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

Why do you need an if statement in the first place? Just use switch-case (using default).

Yeah - look at Aia's post. I too think that you want an "and", not an "or" in your logic code.

I caught that too PetuniaRose, I am still getting a parse error on that line though, and it seems like it should work to me.

``````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
}``````

So it's been many years since I tried to code in C, but I know the feeling of staring blankly at code - you wrote:

if (storenum >=1 && if storenum <=3)

It looks to me like you have an extra "if" in there - shouldn't it be

if (storenum >= 1 && storenum <= 3)

??

commented: Much help. +3

Brilliant suggestion Hiroshe,
I took that out altogether, did a few quick adjustments and eliminated a lot of unneeded code.
Trouble is, now I get no output! lol
No matter what I enter I end up at INVALID NUMBER.

Any suggestions?

``````if (storenum == '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.
else if (storenum == '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);
else if (storenum == '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);

else if (storenum <'1' || storenum >'3')
{
fflush(stdin);

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
{
}
else if (cAgain == 'n' || cAgain == 'N')//sends to end

return 0;
}``````

PetuniaRose, you and me both. I am no C programmer by a long shot.
You were right about the second IF, but my problem appears to be in my switch statement. If I take it out

``````{
switch (storenum) // simple switch which responds to corresponding menu choice with output
}``````

I go straight through the program executes, but goes straight to the INVALID NUMBER error no matter what I input. Leave it in and my parse error is on that line.

I didn't mean get rid of the switch-case, I ment get rid of the the if statment. Instead of having the last "else if", just use "else". Also, you have single quotes around the numbers in your if statments. That means your checking for the ascii value of 1 and 3, not the numbers 1 and 3.

commented: Much help. +3

Thanks guys. Eliminating the switch ended up being the way to go. It was quicker than playing with the whole IF mess.
I still have a little syntax stuff to work out but your help is much appreciated.
I will post final code if you even care to see it. Otherwise I will just close the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.