Tony Hibbert
Tony Hibbert

Reputation: 101

Nested Switch statement - returning to beginning of switch

I am writing a piece of code to convert between Celsius and Fahrenheit using switch statements as you can see in the code below.

#include <stdio.h>
#include <iostream>

using namespace std;

float celsius;
float fahrenheit;

void convertF()
{
    celsius = ((fahrenheit - 32) * 5) / 9;
}

void convertC()
{
    fahrenheit = (((celsius * 9) / 5) + 32);
}

int main()
{
    unsigned short choice;

    cout << "Welcome to the temperature converter.\n";
    cout << "Please press '1' for celsius to fahrenheit conversion" << endl;
    cout << "Or, type '2' for fahrenheit to celsius conversion." << endl;
    cout << "To exit, press 0" << endl;
    cin >> choice;

    switch(choice) 
    {
        case 0:
          return 0;
          break;

        case 1:
          cout << "Please enter temperature in celsius:";

          cin >> celsius;
          convertC();

          cout << "\n";
          cout << "Computing...\n\n";
          cout << "The temperature in Fahrenheit is " << fahrenheit << ".\n";
          cout << "Press any key to terminate the program." << endl;
          cout << endl;
          break;

        case 2:
          cout << "Please enter temperature in fahrenheit:";

          cin >> fahrenheit;
          convertF();

          cout << "\n";
          cout << "Computing...\n\n";
          cout << "The temperature in celsius is " << celsius << ".\n";
          cout << "Press any key to terminate the program." << endl;
          cout << endl;
          break;    

        default:
          cout << "That is not an option!" << endl;
          cout << "Please close the program and try again." << endl;
          break;
    }

    return 0; 
}

If the user selects 1 or 2, the program performs the conversion. You then press any key to end the program.

However I would like to offer them the chance of another conversion after the first one, so they can exit the program or perform another conversion. I have tried to put another switch statement within case 1 and case 2 but it didn't work.

What is the best way to solve this? I am thinking if there is an alternative to using the switch statement completely?

Upvotes: 0

Views: 1802

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

All you need is to enclose the part of the program with the switch statement in some kind of loop.

The program can look something like the following

#include <iostream>

using namespace std;

float convertF( float fahrenheit )
{
    return ((fahrenheit - 32) * 5) / 9;
}

float convertC( float celsius )
{
    return (((celsius * 9) / 5) + 32);
}

int main()
{
    cout << "Welcome to the temperature converter.\n";

    while ( true )
    {
        cout << "\nPlease press '1' for celsius to fahrenheit conversion" << endl;
        cout << "Or, type '2' for fahrenheit to celsius conversion." << endl;
        cout << "To exit, press 0" << endl;

        unsigned short choice;

        if ( !( cin >> choice ) || ( choice == 0 ) ) break;

        switch( choice )
        {
            float celsius;
            float fahrenheit;

        case 1:
            cout << "Please enter temperature in celsius:";

            cin >> celsius;

            cout << "\n";
            cout << "Computing...\n\n";
            cout << "The temperature in Fahrenheit is " << convertC( celsius ) << ".\n";
            break;

        case 2:
            cout << "Please enter temperature in fahrenheit:";

            cin >> fahrenheit;

            cout << "\n";
            cout << "Computing...\n\n";
            cout << "The temperature in celsius is " << convertF( fahrenheit ) << ".\n";
            break;    

        default:
            cout << "That is not an option!" << endl;
            break;

        }
    }

    return 0;
}

For more readability you could introduce an enumeration like

enum { CELSIUS = 1, FAHRENHEIT = 2 };

and use its enumerators as case labels. For example

switch( choice )
{
    float celsius;
    float fahrenheit;

case CELSIUS:
    //...

case FAHRENHEIT:
    //...

default:
    //...
}

Upvotes: 1

xinaiz
xinaiz

Reputation: 7788

You can use switch statement within a loop:

do
{
    cin >> choice;
    switch(choice)
    {
        //cases
    }
}
while (choice != 0);

or

choice = /* not 0 */;
while(choice != 0)
{
    cin >> choice;
    switch(choice)
    {
    //cases
    }
}

or

for(unsigned int choice = /* not 0 */; choice != 0; )
{
    cin >> choice;
    switch(choice)
    {
        //cases
    }
}

Upvotes: 1

4386427
4386427

Reputation: 44274

Just change your code like this:

    cout << "Welcome to the temperature converter.\n";
    while(1)   // <----- Add this 
    {          // <----- Add this 
        cout << "Please press '1' for celsius to fahrenheit conversion" << endl;
        //...
        // All your code down to
        //...
       cout << "Please close the program and try again." << endl;
       break;
       }

    }  // <----- Add this 

return 0;

Upvotes: -1

Nicholas Smith
Nicholas Smith

Reputation: 11754

A do-while loop is probably the simplest way to solve it. You want to check while(choice != 0) to keep running the loop until the user enters 0 as a choice, which means you'll want to have the input section also within the do-while loop, as you want to get further input, and also have the switch statement within the do-while. When choice is entered as 0 the loop will then end.

This shows some examples of do-while loops: http://www.cplusplus.com/doc/tutorial/control/

Upvotes: 0

user3739684
user3739684

Reputation:

You could use a while loop like this:

unsigned int choice;
do
{
    cin >> choice;
    // switch with only case 1 and 2
}
while(choice != 0);
return 0;

I don't know what you mean by an alternative to using the switch statement completely.

Upvotes: 0

Related Questions