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

  • Home
  • SEARCH
  • 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 860539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:49:58+00:00 2026-05-15T08:49:58+00:00

My input file is: 2 5 <– extra space at the end 4 <–extra

  • 0

My input file is:

2 5 <-- extra space at the end
4 <--extra space at the end

int main(){
    ifstream input("input.txt");
    istream& in = input;
    string line1;

    while( getline(in,line1)){
        istringstream number1(line1);

        while(number1.good()){
            number1 >> temp1;
            cout<<temp1<<endl;
        }
}
input.close();
}

The problem is with the extra space at the end of the line my output is:
2
5
5
4
4
which is not what i want.. but if i remove the extra space it would work:
2
5
4

why is this happening? and how can i fix it so that even with extra spaces it reads the correct input?
Any help would be appreciated. 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-15T08:49:58+00:00Added an answer on May 15, 2026 at 8:49 am

    The problem is with the while (number1.good()) loop. The number1 fail state will not be set until after the number1 >> temp1 extraction fails, but you don’t test the fail state until the next time the loop condition is tested, which is after you print out the result of that extraction. You should change the inner loop to:

    while (number1 >> temp1)
    {
        std::cout << temp1 << std::endl;
    }
    

    This will extract the value, then test whether the extraction succeeded and will break out of the loop if the extraction fails, which is the behavior you want.

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

Sidebar

Related Questions

My raw input file text file contains a string: Caf&eacute (Should be Café) The
My input file is a large txt file with concatenated texts I got from
For input file separate by space/tab like: 1 2 3 4 5 6 7
I need an input file stream which would have a bidirectional iterator/adapter. Unfortunately std::ifstream
Input file text.txt: foo() { } buz() { } Awk script.awk : BEGIN {
I changed the input file button style with jquery, but i need space between
I have this input file (space is the separator for the two elements in
I have a input file Main.xml <?xml version=1.0 encoding=utf-8 ?> <Employees> <Employee> <id name=id>1</id>
This works as expected: INPUT FILE src.txt: ffmpeg -i uno.3gp ffmpeg -i dos.3gp ffmpeg
Input file: GET /static_register_ad_request_1_2037_0_0_0_1_1_4_8335086462.gif?pa=99439_50491&country=US&state_fips_code=US_CA&city_name=Los%2BAngeles&dpId=2&dmkNm=apple&dmlNm=iPod%2Btouch&osNm=iPhone%2BOS&osvNm=5.1.1&bNm=Safari&bvNm=null&spNm=SBC%2BInternet%2BServices&kv=0_0&sessionId=0A80187E0138A0AE42E4DE3F783E7A08&sdk_version=4.0.5.6%20&domain=805AOEtUaMu&ad_catalog=99439_50491&make=APPLE&width=320&height=460&slot_type=PREROLL&model=iPod%20touch%205.1.1&iabcat=artsandentertainment&iabsubcat=music&age=113&gender=2&zip=92869 HTTP/1.1 Output file: domain sdk_version 805AOEtUaMu 4.0.5.6%20 I could use

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.