Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:51:40+00:00 2026-05-19T13:51:40+00:00

I am new to C++ (coming from a C# background) and am trying to

  • 0

I am new to C++ (coming from a C# background) and am trying to learn how to convert a string to an int.

I got it working by using a stringstream and outputting it into a double, like so:

const char* inputIndex = "5+2";
double number = 0;
stringstream ss(inputIndex);
ss >> number;
// number = 5

This works great. The problem I’m having is that the strings I’m parsing start with a number, but may have other, not digit characters after the digits (e.g. “5+2”, “9-(3+2)”, etc). The stringstream parses the digits at the beginning and stops when it encounters a non-digit, like I need it to.

The problem comes when I want to know how many characters were used to parse into the number. For example, if I parse 25+2, I want to know that two characters were used to parse 25, so that I can advance the string pointer.

So far, I got it working by clearing the stringstream, inputting the parsed number back into it, and reading the length of the resulting string:

ss.str("");
ss << number;

inputIndex += ss.str().length();

While this does work, it seems really hacky to me (though that might just be because I’m coming from something like C#), and I have a feeling that might cause a memory leak because the str() creates a copy of the string.

Is there any other way to do this, or should I stick with what I have?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:51:41+00:00Added an answer on May 19, 2026 at 1:51 pm

    You can use std::stringstream::tellg() to find out the current get position in the input stream. Store this value in a variable before you extract from the stream. Then get the position again after you extract from the stream. The difference between these two values is the number of characters extracted.

    double x = 3435;
    std::stringstream ss;
    ss << x;
    
    double y;
    std::streampos pos = ss.tellg();
    ss >> y;
    std::cout << (ss.tellg() - pos) << " characters extracted" << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coming from a MS SQL Server background. Working on a new project using
I am trying to learn web development using Mono. Coming from ASP.NET background, I
I'm new to Python, coming from a C# background and I'm trying to get
I'm quite new to C# (coming from a Java background) and I'm working on
Coming from a PHP background, I'm trying to learn C++, since I find it
I am new to java (coming from c#.net background) and was trying the above
I'm trying to learn ASP.NET MVC 3 coming from a web forms background. I'm
I'm trying to understand shaders using libgdx, coming from an XNA/HLSL background. I'm trying
I'm new to C# coming from a python background. I've had a hard time
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.