• 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

    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
    • I think you've hit the nail on the head! I logged out after disabling Cookie AutoDelete, relaunched Firefox, and returned to the site, and no pop-ups. Before claiming victory, I'm going to leave this extension deactivated, and I'll let you know the results in a few days' time. If it's this extension that's the problem, I'll uninstall it, as well as report the problem I've encountered. Thank You!  
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      330
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!