Won't Convert

This is a discussion on Won't Convert within the C++ Programming forums, part of the General Programming Boards category; This code will convert Celsius to Fahrenheit, but it will not convert Fahrenheit to Celsius can any one help me ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Taylors Falls, Minnesota
    Posts
    5

    Won't Convert

    This code will convert Celsius to Fahrenheit, but it will not convert Fahrenheit to Celsius can any one help me its my first program really on my own. its a fairly simple Program but im 15yr old so gotta start somewhere

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double far;
    	double cel;
    	int choice;
    	do{
    		cout <<"1. Celsius to Fahrenheit.\n"<<"2. Fahrenheit to Celsius\n"<<"3. Exit\n"<<"Your choice ( 1-3 ): ";
    		cin >>choice;
    		switch (choice){
    			case 1:
    				cout<<"Please Enter Degrees in Celsius: ";
    					cin>>cel;
    					far = ((9/5) * cel +32;
    						cout<<"Degrees in Fahrenheit is: "<<far<<"\n";
    						break;
    			case 2:
    				cout<<"Please Enter Degrees in Fahrenheit: ";
    					cin>>far;
    					cel = (5/9) * (far-32);
    						cout<<"Degrees in Celsius is: "<<cel<<"\n";
    					break;
    			case 3:
    				cout<<"Good bye\n";
    				exit;
    				break;
    			default:
    				cout<<"Please Enter a Valid choice 1-3. Thank You.";
    				break;
    		}
    	}while(choice !=3);
    		system("Pause");
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    On both of your conversions, use floats (floating point values) instead of integers:

    cel = (5/9)....

    is 0 in integer division. Use 5.0/9.0 and vice versa for the other conversion.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Taylors Falls, Minnesota
    Posts
    5
    When i made the variables into floats instead of doubles it said something about failure to convert double to float possible loss of data but it works fine with doubles thank you

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes you are correct. Floats and doubles are both floating point. I wasn't paying attention to your current set of types.

    You're welcome.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Reading this makes me feel all sentimental. A temperature converter was my very first C program and the teacher decided not to tell us about integers. So of course I made the same mistake.

    Now that I think about it, I think I started swearing about the same time I started programming.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,852
    Quote Originally Posted by baitman18 View Post
    When i made the variables into floats instead of doubles it said something about failure to convert double to float possible loss of data but it works fine with doubles thank you
    That's because things like 9.0/5.0 will be computed as a value of type double, and conversion of that back to type float implies a reduction of precision. If you want things to work with float variables, make these 9.0f/5.0f.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21