Need Help in my C++ Program.

+2 Munich Germany · April 26, 2015
Instead of using the if/else statement in selecting a converter. I want it to be a SWITCH statement.
Hope you can help me out guys. Very important for my assingment. 

Here is my program:

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

    int ftemp;

    int ctemp;

    int select = -1;

 

    while (select ==-1)

   {

        cout << "Please select from the following (0 to quit): " << endl;

        cout << "1) Fahrenheit-to-Celsius" << endl;

        cout << "2) Celsius-to-Fahrenheit" << endl << endl;

        cout << "Enter: ";

        cin >> select;

        if (select == 1)

        {

            cout << " Enter temperature in Fahrenheit to convert to degrees Celsius: ";

            cin >> ftemp;

            ctemp = (ftemp-32) * 5 / 9;

            cout << "Equivalent in Celsius is: " << ctemp << endl;

        }

        else if (select == 2)

        {

            cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";

            cin >> ctemp;
            ftemp = ctemp*9/5 + 32;

            cout << "Equivalent in Fahrenheit is: " << ftemp << endl;

        }

      else if (select == 0)

          exit(0);

        else

            cout << "Valid options 1 or 2." << endl;

      select = -1;

   }

 

    return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Munich Germany · April 30, 2015
Thanks by the way Sal.Al. Very useful tip. I'm new at using the c++
  • 1

C++

107,097 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator