1 Replies - 15560 Views - Last Post: 29 April 2009 - 09:51 PM Rate Topic: -----

#1 cmw51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-April 09

do while loop and switch case

Posted 29 April 2009 - 09:34 PM

Here is the assignment description:
Write a program that accepts temperatures from the user, and also whether the temperature is in Fahrenheit,
Celsius or Kelvin (use the following screen shots as a guide). Keep track of the total temperature in Kelvin
and report the total after each user entry as shown below. When the user decides to stop entering information,
display the message "Goodbye!" as shown. You will need to know the following conversions:
[K] = ([F] + 459.67) 5D 9 [K] = [C] + 273.15.

I cannot figure this out for the life of me. Please help me with the code, not just a tip on what the function or whatever is called to fix it, I am very lost. Thanks.
#include <iostream>
#include <string>
using namespace std;
int main()
{
	double temperature;
	int m,cont;
	int counter=0,temp,measurement;

	do
	{
		counter=counter+temperature;
		if (temperature="")
			cout<<"Please enter a temperature: ";
			cin>>temperature;
		if (m="")
			cout<<"(F)ahrenheit, (C)elsius, or (K)elvin? ";
			cin>>m;
		cout<<"Your current total temperature is "<<temperature<< "degrees Kelvin."<<endl;
		if (cont="")
			cout<<"Do you want to continue (Y/N)?";
			getline(cin, cont);
		
		if ((temp=='F')||(temp=='C')||(temp=='K'))
			m=temp;
		
		
		else cout<<"Please enter a temperature: ";
	}while ((cont!='n')||(cont!='N');
	cout<<"Goodbye!";
	switch(measurement)
	{
	case 'F':
	case 'f':
			temperature=(temperature+459.67)*(5/9);
			break;
	case 'C':
	case 'c':
			temperature=temperature+273.15;
			break;
	case 'K':
	case 'k':
			temperature=temperature;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: do while loop and switch case

#2 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: do while loop and switch case

Posted 29 April 2009 - 09:51 PM

You can rewrite your codes in this way...

int main()
  {  
     do
	{
	  
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Get in Kelvin " << endl;
	  cout << "2. Get in Celsius" << endl;
	  cout << "3. Get in Fahrenheit" << endl;
	  cout << endl;
	  cin >> option;
      //Creat each function that will compute the sepecified temperature and 
     // display the result. In this way, it is easier. 
      
	  switch (option)
	    {
	      case 1 : Degrees_Kelvin(); break;
	      case 2 : Degrees_Celcius(); break;
	      case 3 : Degress_Fahrenheit(); break;
	      
	    }
	}
     while (option != 0);
   
    return 0;
  }



In the function Degrees_Kelvin, you can make a counter, so you can keep track on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1