Solved

# switch structure

Posted on 2006-04-06
226 Views
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:
scanf("i%",idummy);

}
}

i appreciate any help given

thanks
0
Question by:pdhaz

LVL 15

Expert Comment

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 14

Expert Comment

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

try this

default:
{
break;
}

Raj
0

Author Comment

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

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

raisor,

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

LVL 14

Accepted Solution

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:
scanf("i%",idummy);

}
}while  ( myFlag == 0);
}
0

LVL 14

Expert Comment

PS:

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

Author Comment

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 14

Expert Comment

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.

jocasio
0

LVL 14

Expert Comment

Oh, yeah, and don't forget about the internet.  THere are tons of tutorials out there for C...
0

LVL 14

Expert Comment

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

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

### Suggested Solutions

This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…