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 6604601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:10:44+00:00 2026-05-25T19:10:44+00:00

It might sound stupid, but I was just wondering how can I parse a

  • 0

It might sound stupid, but I was just wondering how can I parse a string to an integer in C++?

This is for a school project, and the explanation sheet says :
“Only the use of iostream and string classes and system() function is permitted; The use of any other class or function is prohibited.”

I have searched around a bit, but the only suggestions I found are using classes like atoi or atof.

The input string is already checked for error before the parsing, so it will always only contain an integer.

I wouldn’t mind doing the parsing manually with conditionals, but I don’t think it would be my teacher’s (or anyone’s) preferred way.

Thanks a bunch if you can help.

  • 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-25T19:10:44+00:00Added an answer on May 25, 2026 at 7:10 pm
    #include <iostream>
    #include <string>
    
    int stringToInt(const std::string &text)
    {
        int number = 0;
    
        int powerIndex = 1;
    
        for (int i = text.length() - 1; i >= 0; i--)
        {
            number += powerIndex * (text.at(i) - '0');
    
            powerIndex *= 10;
        }
    
        return number;
    }
    
    std::string intToString (int number)
    {
        std::string text = "";
    
        int numberHolder = number;
    
        while (numberHolder)
        {
            char digit = (numberHolder % 10) + '0';
    
            text = digit + text;
    
            numberHolder /= 10;
        }
    
        return text;
    
    }
    int main ()
    {
        //Testing...
    
        int number = stringToInt("123");
    
        std::string text = intToString(456);
    
        std::cout << number << "\n" << text << "\n";
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound like a stupid question but just trying to learn something here.
This might sound stupid but how can i find the last characters index in
this might question might sounds stupid, but I could'nt figure it out. How can
This might sound like an obvious question but I can't seem to find the
Might sound like a stupid question I guess but rubyonrails.org is just an advert
This might sound stupid but I am still not clear about the Java Stack
This might sound a little stupid but I am still curious about what the
Ok, this might sound stupid and noob but right now in my js, I
This might sound stupid, but I'm seriously a newbie at Android programming. I have
This might sound stupid, but why doesn't the Java compiler warn about the expression

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.