• 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.
  • Posts

    • Let me guess, just big hyped cut scenes for what is really just more run around in the same old static urban zone machine gun vs. sniper game play.
    • I am not seeing a huge difference there.
    • This is what I came here to say even if just for a dedicated Windows PC gaming box. Gaming is the only use case have to use Windows these days.
    • The Outer Worlds 2 gets an October release date, reveals companion details by Pulasthi Ariyasinghe After Avowed, Obsidian Entertainment's next grand RPG was already revealed to be releasing in 2025, but during the Xbox Games Showcase today, it finally received a firm release date: October 29. Check out the new story trailer above. While the original was set in Halcyon, this time, the story is set in Arcadia, a brand-new setting that's described as a lawless frontier. Also cut off from Earth, this land is pressured by human conflicts as well as mysterious space-time rifts. "You’ve been sent in as an Earth Directorate agent, but how you handle the mission – who you help, hinder, or exploit – is entirely your call," says Obsidian today. "Build your character with expanded traits, flaws, and backgrounds that shape every decision and unlock new ways to fight, sneak, talk, or blow things up." The RPG will let players get involved with three clashing factions this time: The Protectorate, Auntie’s Choice, and The Order of the Ascendant. Each of them is attempting to advance their own version of humanity's optimal future. Each of these factions has tout own hub areas, followers, audio design, and propaganda elements, with the war between the trio changing all of these as the story progresses depending on player choices. "RPG systems have been reimagined for this sequel to give players more freedom and more flavor," adds the company. "For example, traits and flaws dynamically evolve based on how you play. Steal enough and you’ll be offered Kleptomaniac, which boosts loot sales but risks auto-theft when you so much as glance at an item." Obsidian also detailed the companions players will be able to meet and ally with in their journeys: Niles: Another Earth Directorate recruit torn between duty and defection. Inez: A former experiment from Auntie’s Choice with a grafted combat edge and a moral core. Aza: A chaos-loving Rift worshipper with a taste for violence and room to grow – maybe. Marisol: A stoic killer from the Order of the Ascendant with calculations to settle. Tristen: A walking tank and judge from the Protectorate, looking to dispense justice – or redefine it. Valerie: A floating, chirping support unit with unexpected upgrades and untapped potential. The Outer Worlds 2 is releasing on October 29, 2025 across PC, Xbox Series X|S, and PlayStation 5. As usual for an Xbox game, it will be available to Game Pass subscribers on day one for no extra cost. Don't forget that Obsidian also has Grounded 2 releasing next month into early access too.
    • I love space RPGs, and this one will no doubt scratch that itch. Im still modding the crap out of Starfiled. Can't wait.
  • Recent Achievements

    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      472
    2. 2
      +FloatingFatMan
      265
    3. 3
      ATLien_0
      234
    4. 4
      snowy owl
      224
    5. 5
      Edouard
      174
  • Tell a friend

    Love Neowin? Tell a friend!