7 Replies - 157628 Views - Last Post: 24 June 2011 - 12:51 PM Rate Topic: -----

#1 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Celsius-to-Fahrenheit conversion

Posted 02 October 2006 - 06:18 AM

I am trying to write an temperature conversion program that asks the user to choose between Celsius-to-Fahrenheit conversion or Fahrenheit-to-Celsius conversion. The user will also have the option to quit the program. (You can use 1, 2, and 3 respectively).
- If the incorrect choice is typed, display an error message and prompt the user to re-enter his/her choice.
- Once the user has chosen which type of conversion he/she wants, the program should prompt the user to enter the temperature value to convert. Finally, display the converted value.
- Depending on the choice, use the following formulas accordingly:
Formula to convert from Celsius to Fahrenheit: ((9/5)*degC)+32.
Formula to convert from Fahrenheit to Celsius: 5*(degF-32)/9
- The process must continue until the user enters the option to quit the program.
- You must create two separate functions for the temperature conversion: one for the Celsius-to-Fahrenheit conversion and another one for the Fahrenheit-to-Celsius conversion.

This is what I have so far all I need is to add a third choice selection to the program and a invalid choice if the wrong choice is entered. This Is what I have so far:

#include <iostream>
using namespace std;
int main()
{
int ftemp;
int ctemp;
int select = 0;
cout << "Please select from the following: " << 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
cout << "Valid options 1 or 2." << endl;
return 0;
}


edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 1

Replies To: Celsius-to-Fahrenheit conversion

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Celsius-to-Fahrenheit conversion

Posted 02 October 2006 - 06:48 AM

Ive made some changes to your program:
#include <iostream>
#include <stdlib>
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;
}


I just used a while loop to repeat the required part of the program. The rest is almost the same so I don't think I need to explain.

This post has been edited by Louisda16th: 02 October 2006 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Celsius-to-Fahrenheit conversion

Posted 03 October 2006 - 05:14 AM

Ok now I see what you did! I really appreciate the help! Thanks!

This post has been edited by Dark_Nexus: 03 October 2006 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_dylan hughes*


Reputation:

Re: Celsius-to-Fahrenheit conversion

Posted 30 March 2010 - 06:13 AM

i have the same sort of problem except i need to be able to go back to the menu it starts with. i have everything else written out right i just don't know how to get it back to the menu. any help is appreciated. please and thank you
Was This Post Helpful? 0

#5 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 995
  • Joined: 30-July 08

Re: Celsius-to-Fahrenheit conversion

Posted 30 March 2010 - 06:27 AM

View Postdylan hughes, on 30 March 2010 - 01:13 PM, said:

i have the same sort of problem except i need to be able to go back to the menu it starts with. i have everything else written out right i just don't know how to get it back to the menu. any help is appreciated. please and thank you

Read about while-loops.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_dylan hughes*


Reputation:

Re: Celsius-to-Fahrenheit conversion

Posted 30 March 2010 - 06:37 AM

well this is what i have so far

#include <iostream>
#include <windows.h>
using namespace std;

char F, C, M;
int Ct, Ft, T;

int main()
{
    char wait4User;
    cout << "Please enter which option you would like to use : \n";
    cout << "Fahrenheit = F \n";
    cout << "Celsuis = C \n";
    cout << "Exit = E \n";
    cin >> M;
    if (M == 'F') {
          do {
          cout << "Please enter the temperature in Fahrenheit (999 returns to menu): ";
          cin >>  T;
          if (T != 999){
          Ct = (T - 32) * 5/9;
          cout << "The tempurate in Celsius is : " << Ct << "\n";
          }
          if (T == 999){
          break;
          }
          }while (T != 999);
   }
   if (M == 'C') {
          do {
          cout << "Please enter the temperature in Celsius (999 returns to menu): ";
          cin >>  T;
          if (T != 999){
          Ft = T * 9/5 + 32;
          cout << "The tempurate in Farenheit is : " << Ft << "\n";
          }
          if (T == 999){
          break;
          }
          }while (T != 999);
   }
   cin >> wait4User;
   return 0;
}

This post has been edited by JackOfAllTrades: 30 March 2010 - 06:45 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]

Was This Post Helpful? 0

#7 cplusplus2392  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-March 10

Re: Celsius-to-Fahrenheit conversion

Posted 30 March 2010 - 07:02 AM

this is how yours should look if you want it to return to the menu


#include <iostream>
#include <windows.h>
using namespace std;

char F, C, M;
int Ct, Ft, T;

int main()
{
    char wait4User;
    do{
    cout << "Please enter which option you would like to use : \n";
    cout << "Fahrenheit = F \n";
    cout << "Celsuis = C \n";
    cout << "Exit = E \n";
    cin >> M;
    if (M == 'F' || M == 'f') {
          do {
          cout << "Please enter the temperature in Fahrenheit (999 returns to menu): ";
          cin >>  T;
          if (T != 999){
          Ct = (T - 32) * 5/9;
          cout << "The tempurate in Celsius is : " << Ct << "\n";
          }
          if (T == 999){
          break;
          }
          }while (T != 999);
   }
   if (M == 'C' || M == 'c') {
          do {
          cout << "Please enter the temperature in Celsius (999 returns to menu): ";
          cin >>  T;
          if (T != 999){
          Ft = T * 9/5 + 32;
          cout << "The tempurate in Farenheit is : " << Ft << "\n";
          }
          if (T == 999){
          break;
          }
          }while (T != 999);
   }
}while (M != 'E' || M != 'e');
   if (M == 'E' || M == 'e'){
         return 0;
         }
   cin >> wait4User;
   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 casemo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-June 11

Re: Celsius-to-Fahrenheit conversion

Posted 24 June 2011 - 12:51 PM

Here's another solution. A bit more complex, but works well. Menu driven. It looks like your problem was not looping in main( ). This program requires you press the Escape key twice to exit, otherwise, it loops through the Fahr/Celc conversion routine. It also sends a message to the user if an incorrect key was pressed at the menu. This code does not do a data validation for numeric input values.

#include <iostream>
#include <string>
#include <conio.h>

#define ESC_KEY     0x1b
#define CELSIUS     0x43
#define FAHRENHEIT  0x46

using namespace std;

int main( void )

{   
    float   temperature;
    float*  pt=&temperature;
    
    do {
        paintMenu();
        switch (get_conversion_type()) {
            case CELSIUS:
                get_temperature("Celsius:    ",pt);
                convert_temperature(FAHRENHEIT,*pt);
                break;
            case FAHRENHEIT:
                get_temperature("Fahrenheit: ",pt);
                convert_temperature(CELSIUS,*pt);
                break;
            case ESC_KEY:
                cout << endl;
                break;
                }
        }
        while (pauseProgram()!=ESC_KEY);
    return 0x00;
}

void convert_temperature(char scaleType, float tmprValue)

{
    cout << (scaleType==CELSIUS ? "Celsius:    ":"Fahrenheit: ")
         << (scaleType==CELSIUS ? ((((40+tmprValue)*5)/9)-40):
                                   (((40+tmprValue)*9)/5)-40)
         << endl;
}

void get_temperature(char *mssg, float *tval)

{
    cout << "\n\nEnter a value...\n\n" << mssg;  
    cin >> *tval;
}

char get_conversion_type(void)

{
    switch (_getch()) {
        case 'c':
        case 'C': return(CELSIUS);
        case 'f':
        case 'F': return(FAHRENHEIT);        
        case ESC_KEY: return(ESC_KEY);
        default: 
            cout << "\n\nPlease press [C], [F], or [Esc]\n";
            return 0x00;
        }
}

char pauseProgram(void)

{
    cout << "\n[Any] - Continue\n[Esc] - Exit"; 
    switch(_getch()) { 
        case ESC_KEY: return(ESC_KEY); 
             default: return(0x00); 
        }
}

void paintMenu(void)

{
    system("CLS");
	cout << "\nTemperature Scale Conversion \n\n"
	    "Select your input value type...\n\n"
            "\t  [C] - Celsius\n" 
            "\t  [F] - Fahrenheit\n\n" 
            "\t[Esc] - Exit\n\n"
            "Press a [Key] ";
}





This post has been edited by casemo: 24 June 2011 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1