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?
 
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);                                                  
}
0
 
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
0
 
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Raj_KauCommented:
try this


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


Raj
0
 
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.
0
 
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
0
 
pdhazAuthor Commented:
raisor,

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

I move the do{ statement prior to the scanf method
0
 
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
0
 
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
0
 
Juan OcasioApplication DeveloperCommented:
Oh, yeah, and don't forget about the internet.  THere are tons of tutorials out there for C...
0
 
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
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.