# Switch statement help.

Page 1 of 1

## 14 Replies - 1853 Views - Last Post: 24 November 2008 - 06:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=72975&amp;s=615aab17111d997dece95d9f924908c3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

# Switch statement help.

Posted 21 November 2008 - 07:19 PM

Hello. I have a question about switch statements for my lab assignment. Basically what this program is supposed to do it convert a metric unit value into and english unit value using #defines and switch statements. I don't fully understand switch statements that well to get me started on it so I was wondering if anybody could help me get going in the right direction.

Thank you,
-Ralph

Here is my code so far:
``` #include <stdio.h>
#include <stdlib.h>
#define QUART_GALLON 4
#define PINT_QUART 2
#define CUP_PINT 2
#define FLOZ_CUP 8
#define ML_FLOZ 29.573
#define ML_CL 10
#define CL_DL 10
#define DL_L 10

int getMetricOption (void);
int getEnglishOption (void);
void getValue (double* metricValue);

int main (void)
{
int metricOption;
int englishOption;
double metricValue;

metricOption = getMetricOption();
englishOption = getEnglishOption();
getValue (&metricValue);

return 0;
}

int getMetricOption (void)
{
int metricOption;
do
{
printf("Metric Units (Input)");
printf("\n1: Milliliter(s)");
printf("\n2: Centiliter(s)");
printf("\n3: Deciliter(s)");
printf("\n4: Liter(s)");
printf("\nEnter the number of the Metric unit: ");

scanf_s("%d", &metricOption);

if (metricOption < 1 || metricOption > 4)
printf("\nInvalid Option, must be 1 - 4 only!\n\n");
} while (metricOption < 1 || metricOption > 4);
return metricOption;
}

int getEnglishOption (void)
{
int englishOption;
do
{
printf("\nEnglish Units (Output)");
printf("\n1: Fluid Ounce(s)");
printf("\n2: Cup(s)");
printf("\n3: Pint(s)");
printf("\n4: Quart(s)");
printf("\n5: Gallon(s)");
printf("\nEnter the number of the English unit: ");

scanf_s("%d", &englishOption);

if (englishOption < 1 || englishOption > 5)
printf("\nInvalid Option, must be 1 - 5 only!\n\n");
} while (englishOption < 1 || englishOption > 5);
return englishOption;
}

void getValue (double* metricValue)
{
printf("\nEnter the value to convert: ");
scanf_s("%lf", metricValue);
return;
}

```

This post has been edited by ralphiz: 21 November 2008 - 07:20 PM

Is This A Good Question/Topic? 0

## Replies To: Switch statement help.

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 21 November 2008 - 07:40 PM

Where are the switches supposed to go? For the menu selection or for the conversion? Or both?

### #3 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 21 November 2008 - 08:01 PM

The switches are for the conversion. I haven't declared anything for the switch function yet, was trying to take it slow XD.

This post has been edited by ralphiz: 21 November 2008 - 08:02 PM

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 21 November 2008 - 08:13 PM

The idea of a switch is a control method. You give it a variable (int, char, etc...) and tell it to make decisions based on that piece of information. Example:

```int val;
cin >> val;
switch(val)
{
case 1:
//if val is 1 do stuff
break;
case 2:
//if val is 2 do stuff
break;
case 3:
//if val is 3 do stuff
break;
case 4:
//if val is  do stuff
break;
case 5:
//if val is 5 do stuff
break;
default:
//if val is none of these take action here
//also good for debugging purposes
break;
}

```

### #5 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 21 November 2008 - 08:25 PM

So can the switch statement do the calculations I plan to do as well? Or does it call another function to do so?

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 21 November 2008 - 08:32 PM

Of course. You're the programmer, the computer will do whatever you tell it to do (unlike people, hahahahahaha, I digress.....)

You'll need to be more specific in what you want/need though. If i understand your assignment correctly, the switch statement would best be suited to a menu choice determining what to do after the user picks the type of conversion and goes from there.

### #7 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 21 November 2008 - 08:57 PM

I'm probably doing this incorrectly. Does this have to go under my englishOption function?

Edit: How I'm trying to do the conversion is to convert the English value (input) to milliliters using my #defines then from there I take the milliliter value and convert it to fluid ounces then use that to convert to what the user's choice was. (Hopefully that makes sense) There's probably an easier way to the conversion but this way seems easiest and makes sense to me XD.

```switch processMetric (int metricOption, double metricValue, double* convertedValue)
{
case 1: metricValue * 1 = convertedValue
// Milliliters to Milliliters [do nothing]
break;
case 2: metricValue / ML_CL = convertedValue
// Centiliters to Milliliters
break;
case 3: (metricValue / CL_DL) / ML_CL = convertedValue
// Deciliters to Centiliters to Milliliters
break;
case 4: ((metricValue / DL_L) / CL_DL) / ML_CL = convertedValue
// Liters to Deciliters to Centiliters to Milliliters
break;
default: printf("Option not available\n");
exit (101);
//if val is none of these take action here
//also good for debugging purposes
break;
}
```

This post has been edited by ralphiz: 21 November 2008 - 09:01 PM

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 21 November 2008 - 10:10 PM

You can only switch one value at a time:

```void processMetric(int metricOption, double metricValue, double* convertedValue) //function, I'm guessing? pass in values
{
switch (metricOption)
{
case 1: metricValue * 1 = *convertedValue//derefernece your pointers or youll end up with memory addresses
// Milliliters to Milliliters [do nothing]
break;
case 2: metricValue / ML_CL = *convertedValue
// Centiliters to Milliliters
break;
case 3: (metricValue / CL_DL) / ML_CL = *convertedValue
// Deciliters to Centiliters to Milliliters
break;
case 4: ((metricValue / DL_L) / CL_DL) / ML_CL = *convertedValue
// Liters to Deciliters to Centiliters to Milliliters
break;
default: printf("Option not available\n");
exit (101);
//if val is none of these take action here
//also good for debugging purposes
break;
}
}

```

I haven't checked your other items, variables ,etc... That code snippet above is an idea. Put the switch in a function, pass parameters you'll need and do calculations as necessary.

This post has been edited by KYA: 21 November 2008 - 10:11 PM

### #9 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 21 November 2008 - 10:23 PM

Ohhhh, I see. I'll try again and see what I come up with and edit this if have trouble. Thank you

Edit: I did the switch statements and no compiler errors! Woot. Now I have to figure out how to use a switch statement to print it to the user and prompt if they want to do another conversion x____________x, any suggestions? XD

```#include <stdio.h>
#include <stdlib.h>
#define QUART_GALLON 4
#define PINT_QUART 2
#define CUP_PINT 2
#define FLOZ_CUP 8
#define ML_FLOZ 29.573
#define ML_CL 10
#define CL_DL 10
#define DL_L 10

int getMetricOption (void);
int getEnglishOption (void);
void getValue (double* metricValue);
void processMetric(int metricOption, double metricValue, double* convertedMetric);

int main (void)
{
int metricOption;
int englishOption;
double metricValue;
double convertedMetric;

metricOption = getMetricOption();
englishOption = getEnglishOption();
getValue (&metricValue);
processMetric (metricOption, metricValue, &convertedMetric);

return 0;
}

int getMetricOption (void)
{
int metricOption;
do
{
printf("Metric Units (Input)");
printf("\n1: Milliliter(s)");
printf("\n2: Centiliter(s)");
printf("\n3: Deciliter(s)");
printf("\n4: Liter(s)");
printf("\nEnter the number of the Metric unit: ");

scanf_s("%d", &metricOption);

if (metricOption < 1 || metricOption > 4)
printf("\nInvalid Option, must be 1 - 4 only!\n\n");
} while (metricOption < 1 || metricOption > 4);
return metricOption;
}

int getEnglishOption (void)
{
int englishOption;
do
{
printf("\nEnglish Units (Output)");
printf("\n1: Fluid Ounce(s)");
printf("\n2: Cup(s)");
printf("\n3: Pint(s)");
printf("\n4: Quart(s)");
printf("\n5: Gallon(s)");
printf("\nEnter the number of the English unit: ");

scanf_s("%d", &englishOption);

if (englishOption < 1 || englishOption > 5)
printf("\nInvalid Option, must be 1 - 5 only!\n\n");
} while (englishOption < 1 || englishOption > 5);
return englishOption;
}

void getValue (double* metricValue)
{
printf("\nEnter the value to convert: ");
scanf_s("%lf", metricValue);
return;
}

void processMetric (int metricOption, double metricValue, double* convertedMetric)
{
switch (metricOption)
{
case 1: *convertedMetric = metricValue * 1;
// Milliliters to Milliliters [do nothing]
break;
case 2: *convertedMetric = metricValue / ML_CL;
// Centiliters to Milliliters
break;
case 3: *convertedMetric = (metricValue / CL_DL) / ML_CL;
// Deciliters to Centiliters to Milliliters
break;
case 4: *convertedMetric = ((metricValue / DL_L) / CL_DL) / ML_CL;
// Liters to Deciliters to Centiliters to Milliliters
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}

void processEnglish (int englishOption, double convertedMetric, double* convertedEnglish)
{
switch (englishOption)
{
case 1: *convertedEnglish = convertedMetric / ML_FLOZ;
// Metric Milliliters to English Fluid Ounces
break;
case 2: *convertedEnglish = (convertedMetric / ML_FLOZ) * FLOZ_CUP;
// Metric Milliliters to Engligh Fluid Ounces to Cups
break;
case 3: *convertedEnglish = ((convertedMetric / ML_FLOZ) * FLOZ_CUP) * CUP_PINT;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints
break;
case 4: *convertedEnglish = (((convertedMetric / ML_FLOZ) * FLOZ_CUP) * CUP_PINT) * PINT_QUART;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints to Quarts
break;
case 5: *convertedEnglish = ((((convertedMetric / ML_FLOZ) * FLOZ_CUP) * CUP_PINT) * PINT_QUART) * QUART_GALLON;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints to Quarts to Gallons
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}
```

This post has been edited by ralphiz: 21 November 2008 - 10:56 PM

### #10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 21 November 2008 - 11:01 PM

```bool notDone = true;

while(notDone)
{
//do conversions
//if so:
notDone = false;
}

```

### #11 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 21 November 2008 - 11:37 PM

I'm stumped again, lol. Do I have to do ANOTHER switch statement for my print function?

Last step:
6. In the function that prints the metric value and its equivalent English value, be sure to print the appropriate unit name. Use switch statements to print the name of the unit chosen when printing the values, for example, "1.000 quart(s) = 0.946 liter(s)".

This post has been edited by ralphiz: 21 November 2008 - 11:38 PM

### #12 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 22 November 2008 - 08:43 AM

Base don the description, that sounds like the way to go. switch your choice variable if you still have it laying around somewhere.

### #13 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 22 November 2008 - 03:15 PM

Wait so I'm confused. How would I do a print function that would print the metric value that was inputted and its equivalent english value? x_x

### #14 ralphiz

Reputation: 0
• Posts: 11
• Joined: 21-November 08

## Re: Switch statement help.

Posted 23 November 2008 - 11:39 PM

How can I prompt the user if they want to do another conversion? Example: "Do you want to do another conversion: Press Y for yes, N for no." Then starts the process over again (asking metric, english and value). Here's my code:

```#include <stdio.h>
#include <stdlib.h>
#define QUART_GALLON 4
#define PINT_QUART 2
#define CUP_PINT 2
#define FLOZ_CUP 8
#define ML_FLOZ 29.573
#define ML_CL 10
#define CL_DL 10
#define DL_L 10

int getMetricOption (void);
int getEnglishOption (void);
void getValue (double* metricValue);
void processMetric (int metricOption, double metricValue, double* convertedMetric);
void processEnglish (int englishOption, double convertedMetric, double* convertedEnglish);
void printMetric (int metricOption, double metricValue);
void printEnglish (int englishOption, double convertedEnglish);

int main (void)
{
int metricOption;
int englishOption;
double metricValue;
double convertedMetric;
double convertedEnglish;

metricOption = getMetricOption();
englishOption = getEnglishOption();
getValue (&metricValue);
processMetric (metricOption, metricValue, &convertedMetric);
processEnglish (englishOption, convertedMetric, &convertedEnglish);
printMetric (metricOption, metricValue);
printEnglish (englishOption, convertedEnglish);
system("pause");
return 0;
}

int getMetricOption (void)
{
int metricOption;
do
{
printf("Metric Units (Input)");
printf("\n1 - Milliliter(s)");
printf("\n2 - Centiliter(s)");
printf("\n3 - Deciliter(s)");
printf("\n4 - Liter(s)");
printf("\nEnter the number of the Metric unit: ");

scanf_s("%d", &metricOption);

if (metricOption < 1 || metricOption > 4)
printf("Invalid Option, must be 1 - 4 only!\n\n");
} while (metricOption < 1 || metricOption > 4);
return metricOption;
}

int getEnglishOption (void)
{
int englishOption;
do
{
printf("\nEnglish Units (Output)");
printf("\n1 - Fluid Ounce(s)");
printf("\n2 - Cup(s)");
printf("\n3 - Pint(s)");
printf("\n4 - Quart(s)");
printf("\n5 - Gallon(s)");
printf("\nEnter the number of the English unit: ");

scanf_s("%d", &englishOption);

if (englishOption < 1 || englishOption > 5)
printf("Invalid Option, must be 1 - 5 only!\n\n");
} while (englishOption < 1 || englishOption > 5);
return englishOption;
}

void getValue (double* metricValue)
{
printf("Enter the value to convert: ");
scanf_s("%lf", metricValue);
return;
}

void processMetric (int metricOption, double metricValue, double* convertedMetric)
{
switch (metricOption)
{
case 1: *convertedMetric = metricValue * 1;
// Milliliters to Milliliters [do nothing]
break;
case 2: *convertedMetric = metricValue * ML_CL;
// Centiliters to Milliliters
break;
case 3: *convertedMetric = (metricValue * CL_DL) * ML_CL;
// Deciliters to Centiliters to Milliliters
break;
case 4: *convertedMetric = ((metricValue * DL_L) * CL_DL) * ML_CL;
// Liters to Deciliters to Centiliters to Milliliters
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}

void processEnglish (int englishOption, double convertedMetric, double* convertedEnglish)
{
switch (englishOption)
{
case 1: *convertedEnglish = convertedMetric / ML_FLOZ;
// Metric Milliliters to English Fluid Ounces
break;
case 2: *convertedEnglish = convertedMetric / ML_FLOZ / FLOZ_CUP;
// Metric Milliliters to Engligh Fluid Ounces to Cups
break;
case 3: *convertedEnglish = convertedMetric / ML_FLOZ / FLOZ_CUP / CUP_PINT;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints
break;
case 4: *convertedEnglish = convertedMetric / ML_FLOZ / FLOZ_CUP / CUP_PINT / PINT_QUART;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints to Quarts
break;
case 5: *convertedEnglish = convertedMetric / ML_FLOZ / FLOZ_CUP / CUP_PINT / PINT_QUART / QUART_GALLON;
// Metric Milliliters to Engligh Fluid Ounces to Cups to Pints to Quarts to Gallons
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}

void printMetric (int metricOption, double metricValue)
{
switch (metricOption)
{
case 1: printf("%.3lf milliliter(s) =", metricValue);
break;
case 2: printf("%.3lf centiliter(s) =", metricValue);
break;
case 3: printf("%.3lf deciliter(s) =", metricValue);
break;
case 4: printf("%.3lf liter(s) =", metricValue);
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}

void printEnglish (int englishOption, double convertedEnglish)
{
switch (englishOption)
{
case 1: printf(" %.3lf fluid ounce(s)\n", convertedEnglish);
break;
case 2: printf(" %.3lf cup(s)\n", convertedEnglish);
break;
case 3: printf(" %.3lf pint(s)\n", convertedEnglish);
break;
case 4: printf(" %.3lf quart(s)\n", convertedEnglish);
break;
case 5: printf(" %.3lf gallon(s)\n", convertedEnglish);
break;
default: printf("Option not available!\n");
exit (101);
break;
}
}
```

### #15 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Switch statement help.

Posted 24 November 2008 - 06:56 AM

Set up a loop

```bool notDone = true;

while(notDone)
{
//conversions