• 0

[C++] Number to String


Question

I've tried several methods and now I've gotten all confused :wacko: .

So I have a number inside a string. Let's take, for example, "200." Now, I want to convert the string into a long and divide it by 2 then store it back inside a string. I tried using ostringstream, but couldn't get it to work correctly.

Does anyone know what to do?

Link to comment
https://www.neowin.net/forum/topic/274948-c-number-to-string/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

So where in that code can it divide by 2? And also, how can I append

  Quote
stringstream s(somestring, stringstream::in | stringstream::out);

into another string? I tried

  Quote
newString += s

newString is a string from string.h.

Edited by Blackout
  • 0

You cannot use any addition operator on a string as it's not a numerical type.

You need to convert newString to a number itself, add s the convert that back to a string.

atoi is a simple function for converting a string to a number.

Here's a link to msdn, you can look it up there. data conversion routines

  • 0
  Lenny said:
You cannot use any addition operator on a string as it's not a numerical type.

You need to convert newString to a number itself, add s the convert that back to a string.

atoi is a simple function for converting a string to a number.

Here's a link to msdn, you can look it up there. data conversion routines

585338566[/snapback]

You can use + on strings, but he's trying to do it with a string and a stringstream. It should be newString += someOtherString;

And as for those types of data conversion, they aren't in the standard, whereas the way I posted uses more standard methods and more probably safer too.

  • 0
  kjordan2001 said:
Hmm, guess I should have read more closely...

Converting it is still the same, just replace int with long and to convert it back after you divide:

stringstream ss;

string str;

ss << somenumber;

ss >> str;

585338552[/snapback]

Well I was trying to append, not copy the stringstream over to the string.

  Lenny said:
You cannot use any addition operator on a string as it's not a numerical type.

You need to convert newString to a number itself, add s the convert that back to a string.

atoi is a simple function for converting a string to a number.

Here's a link to msdn, you can look it up there. data conversion routines

585338566[/snapback]

Is atoi a C++ standard or is it only available in VC++? Because on MSDN it says that it's part of the VC++ runtime libraries...

  • 0
  Blackout said:
Well I was trying to append, not copy the stringstream over to the string.

Is atoi a C++ standard or is it only available in VC++? Because on MSDN it says that it's part of the VC++ runtime libraries...

585338706[/snapback]

I'm not sure I get what you're trying to do, are you trying to divide a number in a string by 2 and store it back into a string and then append it? If so:

long a;

string s = "200";

stringstream ss(s,stringstream::in | stringstream::out);

ss >> a;

a = a/2.0;

stringstream ss2;

ss2 << a;

string str;

ss2 >> str;

atoi isn't part of the C++ standard, although it's included in Linux as well.

  • 0

Here's what I'm trying to do:

1) I have a string called "num." For example, "200." I have another string called "result."

2) I want to take "num" and convert it to a long so I can divide it by 2.

3) So now "num" equals "100." Now I want to take the "num" and APPEND it to "result."

  Quote
ss2 >> str;

Doesn't that code copy the string over rather than append?

My problem with appending "100" is that when I take a number and append it to a string, it converts the number by treating it as an ASCII value.

So basically, the question is: how do I append a number to a string without it being treated as an ASCII value? Because it keeps getting converted to the ASCII character equivalent of the value, but I just want it to append the value.

Edited by Blackout
  • 0

Heh, forgot to append str, here we go again:

string num = "200";

int number;

stringstream num_convert(num,stringstream::in | stringstream::out);

num_convert >> number;

number = number/2;

stringstream string_convert;

string_convert << number;

string str;

string_convert >> str;

string result = "Result is: ";

result += str;

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.