Switch statement help.

Just help me get started please :)

Page 1 of 1

14 Replies - 1288 Views - Last Post: 24 November 2008 - 06:56 AM Rate Topic: -----

#1 ralphiz  Icon User is offline

  • New D.I.C Head

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

Switch statement help.

Post icon  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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#5 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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

Was This Post Helpful? 1
  • +
  • -

#9 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Switch statement help.

Posted 21 November 2008 - 11:01 PM

bool notDone = true;

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


Was This Post Helpful? 0
  • +
  • -

#11 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#13 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 ralphiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
	}
}

Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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

//ask to do another
    if(response == 'N')
         notDone = false; 
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1