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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:59:30+00:00 2026-06-04T01:59:30+00:00

As far as i know, the common way in c++ to cast a String

  • 0

As far as i know, the common way in c++ to cast a String into a int is to use sstream:

   std::string inputString = "12 34";
   std::istringstream istr(inputString);
   int i1, i2;
   istr >> i1 >> i2;

But if I want to make sure, that my code works for any input, the problem is to decide between the input of a string or 0:

   std::string inputString = "TEXT 0";
   std::istringstream istr(inputString);
   int i1, i2;
   istr >> i1 >> i2;
   cout << i1 <<"  !=  "<< i2 << endl;

I want to decide, if the user has inputed a String or a zero, in order to perform further manipulation.
Is there a clean way to decide this problem, without using lexical cast or atoi?
best gegards

  • 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-06-04T01:59:31+00:00Added an answer on June 4, 2026 at 1:59 am

    You could use an invalid default value:

    #include<sstream>
    #include<limits>
    using std::numeric_limits;
    int main(){
        std::string inputString = "TEXT 0"; 
        std::istringstream istr(inputString);
        int i1=numeric_limits<int>::min();
        int i2=numeric_limits<int>::min(); 
        std::string tmp;
        istr >> i1;
        if (i1 == numeric_limits<int>::min()) {
          // extraction failed
          i1 = 0;
          istr.clear(); // clear error flags to allow further extraction
          istr >> tmp; // consume the troublesome token
        }
        // same for i2
    }
    

    Note: The technique works if numeric_limits<int>::min() cannot be a valid input

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know, there are 3 permission levels available to use in
As far as I know there's no way for server-side application to be notified
As far as I know, \d{2,} matches 2 or more consecutive digits, but I
KnockoutJS has been really great to use so far, but I'm new to the
As far as know, I must be careful with PHP, and I think Javascript.
As far as I know, there's no public API to collect touch points/locations at
As far as I know, this isn't caused by an infinite recursion. The program
As far as I know, in C programming language, an array is stored on
As far as I know, Spring uses JDK to generate dynamic proxy for the
As far as I know, initrd acts as a block device, thus requiring a

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.