473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

5
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.

Expand|Select|Wrap|Line Numbers
  1. char inputTemp  = ' ';
  2.  
  3. cout << "\nEnter unit of temperature that you want converted: ";
  4. cin >> inputTemp;
  5. inputTemp = toupper(inputTemp);
  6.  
  7.  
  8. if (inputTemp != 'F' && inputTemp != 'C' && inputTemp != 'K') 
  9.     {
  10.     cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
  11.     return 0;
  12.     }    
  13.  
  14. cout << "\nEnter how many units in " << inputTemp << " : ";
  15. 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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::string;
  9.  
  10. int main()
  11. {
  12.  
  13. double degrees            = 0.0;
  14. double convertedTemp    = 0.0;
  15. char convertTemp        = ' ';
  16. char inputTemp            = ' ';
  17. char count                = ' ';
  18.  
  19. do
  20. {
  21.  
  22. cout << "\nEnter unit of temperature that you want converted: ";
  23. cin >> inputTemp;
  24. inputTemp = toupper(inputTemp);
  25.  
  26. if (inputTemp != 'F' && inputTemp != 'C' && inputTemp != 'K') 
  27.     {
  28.     cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
  29.     return 0;
  30.     }    
  31.  
  32. cout << "\nEnter how many units in " << inputTemp << " : ";
  33. cin >> degrees;
  34.  
  35. cout << "\nWhich unit of temperature would you like to convert " << inputTemp << " to: "; 
  36. cin >> convertTemp;
  37. convertTemp = toupper(convertTemp);
  38.  
  39. if (convertTemp != 'F' && convertTemp != 'C' && convertTemp != 'K')
  40.     {
  41.     cout << "\nYou have entered an invalid unit of temperature!" << endl << endl;
  42.     return 0;
  43.     }
  44.  
  45.  
  46. if (inputTemp == 'F' && convertTemp == 'C')
  47.     {
  48.     convertedTemp = (5.0)/9.0 * (degrees - 32) ;
  49.     cout << "\n\n" << degrees << " in degrees Fahrenheit is " << convertedTemp << " in degrees Celsius. " << endl << endl;
  50.     }
  51.  
  52.     else if (inputTemp == 'F' && convertTemp == 'K')
  53.         {
  54.         convertedTemp = (5.0)/9.0 * (degrees - 32) + 273.15 ;
  55.         cout << "\n\n" << degrees << " in degrees Fahrenheit is " << convertedTemp << " in degrees Kelvin. " << endl << endl;
  56.         }
  57.  
  58.     else if (inputTemp == 'C' && convertTemp == 'F')
  59.         {
  60.         convertedTemp = (9.0)/5.0 * degrees + 32 ;
  61.         cout << "\n\n" << degrees << " in degrees Celsius is " << convertedTemp << " in degrees Fharenheit. " << endl << endl;
  62.         }
  63.  
  64.     else if (inputTemp == 'C' && convertTemp == 'K')
  65.         {
  66.         convertedTemp = degrees + 273.15 ;
  67.         cout << "\n\n" << degrees << " in degrees Celsius is " << convertedTemp << " in degrees Kelvin. " << endl << endl;
  68.         }
  69.  
  70.     else if (inputTemp == 'K' && convertTemp == 'C')
  71.         {
  72.         convertedTemp = degrees - 273.15 ;
  73.         cout << "\n\n" << degrees << " in degrees Kelvin is " << convertedTemp << " in degrees Celsius. " << endl << endl;
  74.         }
  75.  
  76.     else if (inputTemp == 'K' && convertTemp == 'F')
  77.         {
  78.         convertedTemp = ((degrees-273.15)*1.8)+32 ;
  79.         cout << "\n\n" << degrees << " in degrees Kelvin is " << convertedTemp << " in degrees Fahrenheit. " << endl << endl;
  80.         }
  81.  
  82. cout << "Would you like to Convert another unit of Temperature? (Y/N): ";
  83. cin >> count;
  84. count = toupper(count);
  85.  
  86. }while (count == 'Y');
  87.  
  88. return 0;    
  89. }

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
Oct 30 '06 #1
5 6005
Ganon11
3,652 Expert 2GB
OK, you have described the program nicely...but what problems are you having?
Oct 30 '06 #2
sjsn
5
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.
Oct 30 '06 #3
Banfa
9,065 Expert Mod 8TB
convertTempwhich 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.
Oct 30 '06 #4
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.
Oct 30 '06 #5
sjsn
5
Thank you very much for your replies!! I will try both and see how it it works out.


Thank you !

sjsn
Oct 31 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
7
by: Bernardo | last post by:
Hi There is any easy way to convert a 1Char string to a char variable string str="A" char ch=str.. Thanks for your help
3
by: keith | last post by:
In managed C++, there is variable String *s. The variable got value from a C# assembly. Then I need to convert it into char *c in order to call an external function in a dll that accepts parameter...
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
6
by: compboy | last post by:
Can anyone help me about this. I have been trying all the ways I knew and I could find but just didnt work. I have tried: using itoa but it says that it doesnt have that function. and...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
2
by: sangyarts | last post by:
problem in C++ -variable of type std::string called str egl std:string str; // declaration str+ ""; // initialized to this.........nothing.
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.