473,396 Members | 1,945 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,396 software developers and data experts.

Converting Numbers to Strings

I am very new to C++ and I am trying to figure out how to convert a number to a string (even in the simplest case such as
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main () {
  6.   int a;
  7.   a = 1;
  8.   b = 2;
  9.  
  10.   string c;
  11.   c = ?????????????????????
  12.     cout << c;
  13.  
  14.  
  15.   return 0;
  16. }
  17.  
What do I put where the question marks are??? How would I convert a and b into strings and then concatenate them??

Thanks for your help
Jul 5 '07 #1
4 1791
sicarie
4,677 Expert Mod 4TB
Have a look at this.

(And please use code tags! Thanks!)
Jul 5 '07 #2
archonmagnus
113 100+
Since it looks like you are dealing with integers, you might want to investigate the atoi function as well.

C++ "atoi" Reference Page
Jul 5 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
atoi is a deprecated function in C++.

You should use a stringstream:
Expand|Select|Wrap|Line Numbers
  1. int val = 123;
  2. string str;
  3. stringstream ss;
  4. ss << val;
  5. ss >> str;
  6.  
  7. cout << str << endl;
  8.  
Here the integer is inserted in the stringstream as the characters 1 2 3. Then the characters are extracted from the stringstream into your string object.
Jul 6 '07 #4
r035198x
13,262 8TB
atoi is a deprecated function in C++.

You should use a stringstream:
Expand|Select|Wrap|Line Numbers
  1. int val = 123;
  2. string str;
  3. stringstream ss;
  4. ss << val;
  5. ss >> str;
  6.  
  7. cout << str << endl;
  8.  
Here the integer is inserted in the stringstream as the characters 1 2 3. Then the characters are extracted from the stringstream into your string object.
You'll notice that the OP had actually included sstream!
Jul 6 '07 #5

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

Similar topics

8
by: fo | last post by:
Does anyone know if there is a free code available to convert numbers to text and/or text to numbers? Thanks
5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
7
by: sakitah | last post by:
Hello Everyone, Here's the problem: I have a string that I break down into 3 other strings as such: string1 = "001-202-1234567" and parsing it into: string2 = "001";
8
by: rouble | last post by:
Without using errno, is there a portable way to detect if a string number is not within the range 0 to 4294967295. Currently I am using strtoul. I can check the string for "-" as the first...
3
by: Max Gattringer | last post by:
I have written a little programm, which converts normal Text into Unicode Bytes - nothing special, but i tried to creat 2nd Encoder which converts strings(numbers) in a textBox (strings which i...
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
7
by: MeganTSU | last post by:
Hey yall! I am trying to get this program finished for class.... It says that you are suppposed to write a program that will display a check formatted out put (the output looks like a check). I got...
3
by: bolly | last post by:
Hi, I've been putting Python data into a sqlite3 database as tuples but when I retrieve them they come back as unicode data e.g 'u(1,2,3,4)'.How can I change it back to a tuple so I can use it as...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.