473,785 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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 6027
Ganon11
3,652 Recognized Expert Specialist
OK, you have described the program nicely...but what problems are you having?
Oct 30 '06 #2
sjsn
5 New Member
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 Recognized Expert Moderator Expert
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.
Oct 30 '06 #4
nands31
1 New Member
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 New Member
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
6194
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
3160
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
5633
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 func(char c). How to convert from String *s to char *c? thanks Keith
14
1463
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 integer that was entered. So, if 353 was entered, the output should be 11.
3
9201
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
34155
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 also
8
8437
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 = "aabbccdd"; where aa is the LSB and dd is the MSB.
2
2448
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
13544
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. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.