Thread: Need Help please

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Unhappy Need Help please

    The question is:
    Write a C++ program that will read temperature and degrees fehrenheit and convert it to degree celesius.
    celesius=5/9(fahrenheit-32).
    It may be very easy to other but I'm an ameature in C++.
    please help

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    No one will write full code for you...show some efforts...whereever you are stuck...post it....someone will definetly help you

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Well to begin with you are gonna need a function that would translate from C to F, and from C to F

    and then in the main function you are gonna need to ask for the degreese and run the apropriate function.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Maybe this will help, just a few days old:
    http://cboard.cprogramming.com/showthread.php?t=70535

    It is the reverse of your problem.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
      float fahr; //holds user entered degrees in Fahrenheit
      float cels; //holds our calculated degrees in Celsius
    
      cout << "Enter degrees in Fahrenheit: ";
      cin >> fahr;                          //Get user input
      cels = (((fahr-32)/9)*5);             //Equation to convert F to C
      cout << fahr<<" degrees equals "<< cels << " degrees in Celsius"<< endl;  //Output answer
      system("PAUSE");  //Pause so you can read the answer
      return 0;    
    }
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No one will write full code for you
    Evidently not. Please do not post questions asking for people to write your entire program. Come to the forums with a specific question and show that you have already tried. Equally annoying, do not encourage this behavior by GIVING people their entire program.

    Please refer to the forum rules:

    http://cboard.cprogramming.com/annou...ouncementid=51

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I'm sorry but it was such an easy program that explaining it is longer than writing it. I couldn't help it. I commented to show him how it was done.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    What if you want to go from Cels to fah?

    you can create the functions:
    Code:
    int FC(int f)
    {
    return (f - 32) * 5/9
    }
    
    int CF(int c)
    return (c * 9/5) +32
    }
    Last edited by mrafcho001; 10-08-2005 at 02:20 PM.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  9. #9
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    He didn't say he needed to go from Celsius to Fahrenheit, but yes you could have made the equation into a function.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thank you all for helping me out and i'm sorry that i had make go through all of this.
    And by the way you are right i should specifie my question.
    Anyway i only had two classes in C++ and i have no backeground on this subject.
    Again thank u all for help.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is a little code you can try:


    Code:
    #include<vector>
    #include<cstdlib>
    #include<iomanip>
    #include<iostream>
    #include<windows.h>
    
    using namespace std;
    
    class Temp
    {
    
    public:
    
    	void   menu();
    	double f_to_c(char* fehrenheit);
    	char*  get_user_input();
    	void   display_answer(char* fehrenheit, double celcius);
    	bool   again();
    
    private:
    
    	vector<char> user_input;
    };
    
    void clrscr();
    
    int main()
    {
    	Temp *MyTemp;
    	char *f_temp;
    
    	do
    	{
    		MyTemp->menu();
    
    		f_temp = MyTemp->get_user_input();
    
    		MyTemp->display_answer(f_temp, MyTemp->f_to_c(f_temp));
    
    	}while(MyTemp->again());
    
    return EXIT_SUCCESS;
    }
    
    
    void Temp::menu()
    {
    	clrscr();	
    
    	cout	<< endl << endl << endl << endl	
    			<< setw(40) << "+----------------------------+\n"
    			<< setw(40) << "| +------------------------+ |\n" 
    			<< setw(40) << "| |                        | |\n" 
    			<< setw(40) << "| | Fehrenheit  to Celcius | |\n"
    			<< setw(40) << "| |                        | |\n" 
    			<< setw(40) << "| | Temperature Converter  | |\n"
    			<< setw(40) << "| |                        | |\n"
    			<< setw(40) << "| +------------------------+ |\n"
    			<< setw(40) << "+----------------------------+\n";
    }
    
    
    
    char* Temp::get_user_input()
    {
    	char input, *f_temp;
    
    	cout	<< endl << endl
    			<< setw(20) << "Enter temperature in degrees fehrenheit :  ";
    
    	cin.get(input);
    	
    		while(input!='\n')
    		{			
    			user_input.push_back(input);
    			cin.get(input);
    		}
    
    	f_temp = new char[user_input.size()];
    
    	for(int i=0, back=user_input.size(); i<back; i++)
    
    		f_temp[i] = user_input[i];
    
    	return f_temp;
    }
    
    
    double Temp::f_to_c(char* f_temp)
    {
    	double f;
    
    	f = static_cast<double>(atof(f_temp));
    
    	return(((f-32)/9)*5);
    }
    
    
    void Temp::display_answer(char* fehrenheit, double celcius)
    {
    	cout << endl << endl; 
    
    	cout << static_cast<double>(atof(fehrenheit)) << "F equals " << celcius << " degrees celcius.";
    }
    
    
    void clrscr()
    {
        COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD dwConSize;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
        GetConsoleScreenBufferInfo(hConsole, &csbi);
    
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    
        FillConsoleOutputCharacter(hConsole, TEXT(' '),dwConSize, coordScreen, &cCharsWritten);
    
        GetConsoleScreenBufferInfo(hConsole, &csbi);
    
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    
    bool Temp::again()
    {
    	char choice;	
    
    	cout << "\n\nWould ye' like to try again?";	
    
    	cin.get(choice);
    
    	switch(toupper(choice))
    	{
    		case 'Y': return true;
    			      break;
    
    		case 'N': clrscr();
    			      return false;
    				  break;
    
    		default:  cout << '\a'; 
    			      again();
    	}
    }
    Last edited by The Brain; 10-10-2005 at 08:34 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed