• 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

    • I always disable animations in Windows using sysdm.cpl and yes, it feels faster. I have also reduced animations in my android phone using dev options.
    • Neowin's servers (hosted in the UK).
    • MacOS isn't Linux, but Proton is modified WINE, and WINE runs on MacOS. They would just need to add something like Box86 or Rosetta support.
    • PicPick 7.4.0 by Razvan Serea PicPick is user friendly and full of features for creating your image, suitable for software developers, graphic designers and the home user. It is an all-in-one program that provides a full-featured screen capture tool, intuitive image editor, color picker, color palette, pixel ruler, protractor, crosshair and even whiteboard. It not only has everything that you need, but it loads fast, and sits quietly in the system tray until needed. This software is provided as freeware for personal use only. In this case, you are granted the right to use this program free of charge. Otherwise, you need to pay for a license for commercial use. PicPick key features: For All Windows (Fully support Windows 11, 10, 8.1, 8, 7, Vista and XP both 32-bit and 64-bit) Multi-language is supported. (MORE 28+) All functions are fully supported on a dual screen environment. No Registry, No access to System folder (you can copy these files to portable USB) Screen Capture Auto-scroll, dual monitors and sound effect are supported Various output to File, Printer, Office programs, External program Sharing to FTP, Web, E-mail, Facebook and Twitter are supported as well Full Screen Active Window Window Control Scrolling Window Region, Fixed Region FreeHand Repeat Last Capture Image Editor Intuitive User Interface Windows Ribbon style Standard drawing, shapes, arrows, lines, text, and etc. Blur, sharpen, hue, contrast, brightness, pixelate, rotate, flip, frame effect and etc. Color Picker and Color Palette various color code type (RGB, HTML, C++, Delphi) Photoshop style RGB/HSV conversion is supported. Pick and Save your favorite color! Screen Pixel Ruler Horizontal and vertical orientation various units (Pixels, Inches, Centimeters) DPI setting (72, 96, 120, 300) colorful gradient skins You don't have to install any other screen ruler softwares. Screen Magnifier Zoom 2x to 10x option Stay on top, smooth display, and sizeable window Screen Protractor Have you seen any screen protractor function in other software? Screen Crosshair For aligning objects in graphics or design applications For calculating relative coordinates on screen Some prefer to use this tool than a pixel-ruler. Whiteboard For giving a presentation or just drawing something on screen PicPick 7.4.0 changelog: Added support for saving in WebP file format Added horizontal scrolling in the Image Editor with Shift and mouse wheel Fixed control capture failing to detect specific windows Fixed focus loss during delayed active-window captures. Download: PicPick 7.4.0 | 74.9 MB (Free for personal use only) Download: Portable PicPick 7.4.0 | 73.3 MB View: PicPick Home page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Stellarium 25.2 by Razvan Serea Stellarium is a free open source planetarium for your computer. It shows a realistic sky in 3D, just like what you see with the naked eye, binoculars or a telescope. It is being used in planetarium projectors. Just set your coordinates and go. Stellarium key features: Realistic simulation of the sky, sunrise and sunset Default catalogue of over 600,000 stars Downloadable additional catalogues for up to 210 million stars Catalog data for all New General Catalogue (NGC) objects Images of almost all Messier objects and the Milky Way Artistic illustrations for all 88 modern constellations More than a dozen different cultures with their constellations Solar and lunar eclipse simulation Photorealistic landscapes (more are available on the website) Scripting support with ECMAScript (a few demo scripts are included) Extendable with plug-ins: 8 plug-ins installed by default, including: artificial satellites plug-in (updated from an on-line TLE database) ocular simulation plug-in (shows how objects look like in a given ocular) Solar System editor plug-in (imports comet and asteroid data from the MPC) telescope control plug-in (Meade LX200 and Celestron NexStar compatible) The major changes of this version: Many changes in support of sky cultures Added 2 new plugins: Mosaic Camera and Custom Nebula Textures Many improvements in Core Updates in sky cultures (include new one) [full release notes] Download: Stellarium 25.2 (64-bit) | 361.0 MB (Open Source) View: Stellarium Home Page | Other Operating Systems | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • First Post
      Johnny Mrkvička earned a badge
      First Post
    • Week One Done
      viraltui earned a badge
      Week One Done
    • One Month Later
      serfegyed earned a badge
      One Month Later
    • Dedicated
      firey earned a badge
      Dedicated
    • Dedicated
      fettermanj earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      651
    2. 2
      Michael Scrip
      224
    3. 3
      ATLien_0
      222
    4. 4
      Xenon
      146
    5. 5
      +FloatingFatMan
      142
  • Tell a friend

    Love Neowin? Tell a friend!