How to convert char's variable to string? using 101 C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sjsn
    New Member
    • Oct 2006
    • 5

    How to convert char's variable to string? using 101 C++

    Basic C++, just started this course in school... So I'm very limited.

    What I'm at doing is, below I have a variable named inputTemp, which is the product of a char, F = Fahrenheit, C = Celsius and K = Kelvin... I want to be specific when asking the user for more information, such as degrees. So when I ask for degrees in F... it wont sound so kinda funny.
    I know I can write "Enter unit of temp that you want converted using (F)ahrenheit, (C)elsius, and (K)elvin" , just to specificfy which characters the program is looking for...but assume the user already knows about F, C and K.

    Code:
    char inputTemp  = ' ';
    
    cout << "\nEnter unit of temperature that you want converted: ";
    cin >> inputTemp;
    inputTemp = toupper(inputTemp);
    
    
    if (inputTemp != 'F' && inputTemp != 'C' && inputTemp != 'K') 
    	{
    	cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
    	return 0;
    	}	
    
    cout << "\nEnter how many units in " << inputTemp << " : ";
    cin >> degrees;
    Basically I want that cout statement to say if the user enters f:
    "Enter how many units in Fahrenheit: "

    or enters 'c'
    "Enter how many units in Celsius: "


    All of my code is below:

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main()
    {
    
    double degrees			= 0.0;
    double convertedTemp	= 0.0;
    char convertTemp		= ' ';
    char inputTemp			= ' ';
    char count				= ' ';
    
    do
    {
    
    cout << "\nEnter unit of temperature that you want converted: ";
    cin >> inputTemp;
    inputTemp = toupper(inputTemp);
    
    if (inputTemp != 'F' && inputTemp != 'C' && inputTemp != 'K') 
    	{
    	cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
    	return 0;
    	}	
    
    cout << "\nEnter how many units in " << inputTemp << " : ";
    cin >> degrees;
    	
    cout << "\nWhich unit of temperature would you like to convert " << inputTemp << " to: "; 
    cin >> convertTemp;
    convertTemp = toupper(convertTemp);
    	
    if (convertTemp != 'F' && convertTemp != 'C' && convertTemp != 'K')
    	{
    	cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
    	return 0;
    	}
    		
    
    if (inputTemp == 'F' && convertTemp == 'C')
    	{
    	convertedTemp = (5.0)/9.0 * (degrees - 32) ;
    	cout << "\n\n" << degrees << " in degrees Fahrenheit is " << convertedTemp << " in degrees Celsius. " << endl << endl;
    	}
    
    	else if (inputTemp == 'F' && convertTemp == 'K')
    		{
    		convertedTemp = (5.0)/9.0 * (degrees - 32) + 273.15 ;
    		cout << "\n\n" << degrees << " in degrees Fahrenheit is " << convertedTemp << " in degrees Kelvin. " << endl << endl;
    		}
    
    	else if (inputTemp == 'C' && convertTemp == 'F')
    		{
    		convertedTemp = (9.0)/5.0 * degrees + 32 ;
    		cout << "\n\n" << degrees << " in degrees Celsius is " << convertedTemp << " in degrees Fharenheit. " << endl << endl;
    		}
    
    	else if (inputTemp == 'C' && convertTemp == 'K')
    		{
    		convertedTemp = degrees + 273.15 ;
    		cout << "\n\n" << degrees << " in degrees Celsius is " << convertedTemp << " in degrees Kelvin. " << endl << endl;
    		}
    
    	else if (inputTemp == 'K' && convertTemp == 'C')
    		{
    		convertedTemp = degrees - 273.15 ;
    		cout << "\n\n" << degrees << " in degrees Kelvin is " << convertedTemp << " in degrees Celsius. " << endl << endl;
    		}
    
    	else if (inputTemp == 'K' && convertTemp == 'F')
    		{
    		convertedTemp = ((degrees-273.15)*1.8)+32 ;
    		cout << "\n\n" << degrees << " in degrees Kelvin is " << convertedTemp << " in degrees Fahrenheit. " << endl << endl;
    		}
    
    cout << "Would you like to Convert another unit of Temperature? (Y/N): ";
    cin >> count;
    count = toupper(count);
    
    }while (count == 'Y');
    
    return 0;	
    }

    As you can see my output statement that shows the calculations I've written out Fahrenheit because I know that statement's conclusion is looking for the converted Fahrenheits degrees...




    Any suggestions to this problem?

    Thanks for reading my rant, I hope its clear and un-noobish... If you do understand my question but don't have an answer, but could de-noobify it some, could you help as far as my "clearly stated question"?


    Thank you,
    sjsn
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    OK, you have described the program nicely...but what problems are you having?

    Comment

    • sjsn
      New Member
      • Oct 2006
      • 5

      #3
      Really isn't a problem... I'm looking for a command to convert the letter F to Fahrenheit, C to Celsius and so on. Using the variable that receives the input from the user...


      In this case, inputTemp and convertTemp are both variables that require one Character from the user. I want to be able to use the variable inputTemp, have it recognize F,C,K and spell out the whole word when I make reference to it...

      more of a "How to" problem...

      Thanks for the reply, I hope I'm more clear on my question.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        convertTempwhic h contains 'C', 'F' or 'K' is a character, just use a switch statement to then initialise a string variable with the full name of the input unit.

        You could also then handle 'c', 'f' and 'k' easily too.

        Comment

        • nands31
          New Member
          • Oct 2006
          • 1

          #5
          You can try using pointers as well. the char arrays' first location is basically what you want. and then you can use the switch statement. let me know if you want more details on this.

          Hope this helps.

          Comment

          • sjsn
            New Member
            • Oct 2006
            • 5

            #6
            Thank you very much for your replies!! I will try both and see how it it works out.


            Thank you !

            sjsn

            Comment

            Working...