switch structure

i am using a switch structure within my new program, and (thank god) everything works, my output and calculations. the help i need is when a person inputs a number other then the selections needed i have a defaulted to "try selection again" which works but that where it stops. what i am trying to do is when a person has an invalid input it read that staement then it goes back and lets them renter the number needed. below is a look at my code:

#include <stdio.h>
main()
{
int iResponse = 0;
float  g, h, i;                                             
float  a, b, c;                                      
float  d, e, f;                                   
int idummy;                                 

printf("\n1\tDelmar\n");                                
printf("2\tEncinitas\n");
printf("3\tLaJolla\n");
printf("\nPlease select a store (1-3): ");
scanf("%d", &iResponse);

switch (iResponse) {                      
                                    
case 1:                                                
printf("\nYou selected Delamr\n");                                      
printf("\nEnter purchase amount: $");  
scanf("%f", &g);
h=g*.0725;                                            
i=g + h;                                                        
printf("\nThe sales tax is: $%.2f\n",h);               
printf("\nThe total price with tax is: $%.2f",i);       
break;                                                  
case 2:
printf("\nYou selected Encintas\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &a);
b=a*.075;                                    
c=a + b;                                                
printf("\nThe sales tax is: $%.2f\n",b);            
printf("\nThe total price with tax is: $%.2f\n",c);       
break;                                                            
case 3:
printf("\nYou selected LaJolla\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &d);
e=d*.0775;                                           
f=d + e;                                            
printf("\nThe sales tax is: $%.2f\n",e);                   
printf("\nThe total price with tax is: $%.2f\n",f);       
break;                                                  

default:                                                
    printf("\nTry your selection again\n");        
scanf("i%",idummy);                       
                                                         
}                                                      
}                                                        
 

i appreciate any help given

thanks
pdhazAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ralf KlattEmployee in Civil ServiceCommented:
Hi,

You might think about using regex as a "validation-help-function" ... it could save time and space -> looking at the lines of code you've already generated ... ;-)

You may want to fit this code to your needs:

#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

int main(){
        char pattern[]="^..[A-Z]{2} [0-9]{4} ([0-9A-z]{6})$";
        char match[]="TEST 1234 MARKUS";
        size_t regex_nmatch=2;
        regex_t regex;
        regmatch_t regex_pmatch[regex_nmatch];

        printf("checking: %s %s\n",match, pattern);

        if (regcomp(&regex,pattern,REG_EXTENDED)==0&&regexec(&regex,match,2,regex_pmatch,0)==0){
                printf("%s\n",&match[regex_pmatch[1].rm_so]);

        }
        regfree(&regex);
}



Best regards,
Raisor
Juan OcasioApplication DeveloperCommented:
You could also use a flag and set it to false and the do a do while loop checking to see if the flag gets switched.  You would add the flag to each case:

#include <stdio.h>
main()
{
int iResponse = 0;
float  g, h, i;                                        
float  a, b, c;                                  
float  d, e, f;                              
int idummy;
int myFlag = 0;                          

printf("\n1\tDelmar\n");                                
printf("2\tEncinitas\n");
printf("3\tLaJolla\n");
printf("\nPlease select a store (1-3): ");
scanf("%d", &iResponse);
do{
switch (iResponse) {                      
                             
case 1:                                                
printf("\nYou selected Delamr\n");                                      
printf("\nEnter purchase amount: $");  
scanf("%f", &g);
h=g*.0725;                                            
i=g + h;                                                    
printf("\nThe sales tax is: $%.2f\n",h);              
printf("\nThe total price with tax is: $%.2f",i);  
 myFlag == 1;  
break;                                                  
case 2:
printf("\nYou selected Encintas\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &a);
b=a*.075;                              
c=a + b;                                                
printf("\nThe sales tax is: $%.2f\n",b);          
printf("\nThe total price with tax is: $%.2f\n",c);  
 myFlag == 1;    
break;                                                            
case 3:
printf("\nYou selected LaJolla\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &d);
e=d*.0775;                                    
f=d + e;                                          
printf("\nThe sales tax is: $%.2f\n",e);                
printf("\nThe total price with tax is: $%.2f\n",f);
 myFlag ==1;      
break;                                                  

default:                                                
    printf("\nTry your selection again\n");        
scanf("i%",idummy);                      
                                                         
}  
}while  ( myFlag == 0);                                                  
}

Doing this will ensure it's at least done once and if any of the cases are executed the flag will be set to 1 thereby stepping out of the do..while loop.
hth

jocasio
Raj_KauCommented:
try this


default:                
{                                
    printf("\nTry your selection again\n");        
 break;
}


Raj
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

pdhazAuthor Commented:
thanks for the suggestions, i will be able to give those a try when i return home from worh this evening.

to raisor, i am not savy doing this would it be possible for you to provide an example please as to how that regex statement works.

thanks

pdhaz.
pdhazAuthor Commented:
to jocasio,
 
i have just tried what you had suggested it works great when i input a number for a store and it allows me to enter different amounts for the same store but when i input a number that is not on the selection the prgram goes hay-wire. the statement "try your selection again" goes into a continous cycle on the screen and does not stop unless i exit out of the program

pdhaz
pdhazAuthor Commented:
raisor,

i have just tried your suggestion but i get an error that the variable regex "not found" regex
Juan OcasioApplication DeveloperCommented:
pdhaz:

Try this:
#include <stdio.h>
main()
{
int iResponse = 0;
float  g, h, i;                                        
float  a, b, c;                                  
float  d, e, f;                              
int idummy;
int myFlag = 0;                          

printf("\n1\tDelmar\n");                                
printf("2\tEncinitas\n");
printf("3\tLaJolla\n");
printf("\nPlease select a store (1-3): ");

do{
scanf("%d", &iResponse);
switch (iResponse) {                      
                             
case 1:                                                
printf("\nYou selected Delamr\n");                                      
printf("\nEnter purchase amount: $");  
scanf("%f", &g);
h=g*.0725;                                            
i=g + h;                                                    
printf("\nThe sales tax is: $%.2f\n",h);              
printf("\nThe total price with tax is: $%.2f",i);  
 myFlag == 1;  
break;                                                  
case 2:
printf("\nYou selected Encintas\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &a);
b=a*.075;                              
c=a + b;                                                
printf("\nThe sales tax is: $%.2f\n",b);          
printf("\nThe total price with tax is: $%.2f\n",c);  
 myFlag == 1;    
break;                                                            
case 3:
printf("\nYou selected LaJolla\n");
printf("\nEnter purchase amount: $");  
scanf("%f", &d);
e=d*.0775;                                    
f=d + e;                                          
printf("\nThe sales tax is: $%.2f\n",e);                
printf("\nThe total price with tax is: $%.2f\n",f);
 myFlag ==1;      
break;                                                  

default:                                                
    printf("\nTry your selection again\n");        
scanf("i%",idummy);                      
                                                         
}  
}while  ( myFlag == 0);                                                  
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Juan OcasioApplication DeveloperCommented:
PS:

I move the do{ statement prior to the scanf method
pdhazAuthor Commented:
to jocasio123,

that worked great, i was able to input a number and come with the desired result. i appreciate all that you have done.
question though, where would a person who is just starting out find a simple yet good book/reference where they could use to tell about stuff like the "my flag" statement. the book they gave us to use for school (i went through it a number of times) do not even refer to the stuff that you or rasior had suggested.

but again thanks for the help

pdhaz
Juan OcasioApplication DeveloperCommented:
A great book series is the deitel and deitel How to Program.  You can get this in C, C++, and everything under the sun (JAVA). Also for the very basics, SAMMS Has some decent books (The teach yourself xxx in 24 hours/days).  Start small and make sure you go over the examples over and over.

Glad it worked!

jocasio
Juan OcasioApplication DeveloperCommented:
Oh, yeah, and don't forget about the internet.  THere are tons of tutorials out there for C...
Juan OcasioApplication DeveloperCommented:
One other thing, myFlag was just and integer variable, and all you are doing is initializing it to a value (in this case 0) and then testing this value in the do...while loop.  Notice that we did a do..while loop, this ensured that the loop was done at least once and then the test  ( myFlag == 0) was performed.

jocasio
pdhazAuthor Commented:
cool beans!!


again thanks for the help and i will look into those books. but i do not think there is to many sites i have not visited while on line for this, this really was the only place i got soem clarity on things


pdhaz
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.