• 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

    • all these "selling for great prices" articles feel like filler anymore....
    • It has the same switch to allow whitelisting or not as the regular AdBlock extension.
    • These 20 crypto phishing applications are scamming Play Store users by Hamid Ganji Google Play Store is the main venue for Android users to download applications. While Google has strict rules and policies for verifying apps, some malicious apps somehow slip through anyway. Meanwhile, when it comes to crypto wallet apps, both Google app auditors and Play Store users need to be even more cautious. Cyble Research and Intelligence Labs has identified at least 20 crypto phishing applications on the Google Play Store that impersonate legitimate and popular crypto wallet apps and try to steal users' crypto credentials. By impersonation, these malicious apps trick users into downloading them and then start to capture the user's actual login data. "What makes this campaign particularly dangerous is the use of seemingly legitimate applications, hosted under previously benign or compromised developer accounts, combined with a large-scale phishing infrastructure linked to over 50 domains. This extends the campaign's reach and lowers the likelihood of immediate detection by traditional defenses." Cyble writes. Some of these malicious apps have the same name but come with a different package name. After removing duplicate names, here's the list of 9 newly discovered crypto phishing applications on the Play Store: Pancake Swap Suite Wallet Hyperliquid Raydium BullX Crypto OpenOcean Exchange Meteora Exchange SushiSwap Harvest Finance Blog According to Cyble, these apps prompt users to enter their 12-word mnemonic phrase to access the fake crypto wallet. Also, scammers use accounts that were previously used to distribute legitimate apps to minimize the risk of getting caught by Google. These accounts are more likely to be compromised and then taken over by scammers. If you've downloaded any of these fake crypto wallet apps from the Play Store, make sure to delete them as soon as possible. In 2024, revenue from crypto scams was estimated to be around $9.9 billion. This billion-dollar crypto scam business is expected to grow massively in 2025 thanks to AI.
  • Recent Achievements

    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
    • Week One Done
      abortretryfail earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      239
    4. 4
      ATLien_0
      228
    5. 5
      Edouard
      179
  • Tell a friend

    Love Neowin? Tell a friend!