[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

switch structure

Posted on 2006-04-06
13
Medium Priority
?
228 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:pdhaz
13 Comments
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 16397466
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 16397554
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
 
LVL 3

Expert Comment

by:Raj_Kau
ID: 16397928
try this


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


Raj
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:pdhaz
ID: 16399152
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
 

Author Comment

by:pdhaz
ID: 16404915
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
 

Author Comment

by:pdhaz
ID: 16404956
raisor,

i have just tried your suggestion but i get an error that the variable regex "not found" regex
0
 
LVL 15

Accepted Solution

by:
Juan Ocasio earned 1000 total points
ID: 16405072
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 16405133
PS:

I move the do{ statement prior to the scanf method
0
 

Author Comment

by:pdhaz
ID: 16405462
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 16405578
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 16405590
Oh, yeah, and don't forget about the internet.  THere are tons of tutorials out there for C...
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 16405606
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
 

Author Comment

by:pdhaz
ID: 16405686
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question