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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:37:16+00:00 2026-05-14T15:37:16+00:00

As the question says, for some reason my program is not flushing the input

  • 0

As the question says, for some reason my program is not flushing the input or using my variables in ways that I cannot identify at the moment. This is for a homework project that I’ve gone beyond what I had to do for it, now I just want the program to actually work 😛

Details to make the finding easier:

The program executes flawlessly on the first run through. All throws work, only the proper values( n > 0 ) are accepted and turned into binary.

As soon as I enter my terminate input, the program goes into a loop and only asks for the terminate again like so:

When I run this program on Netbeans on my Linux Laptop, the program crashes after I input the terminate value. On Visual C++ on Windows it goes into the loop like just described.

In the code I have tried to clear every stream and initialze every variable new as the program restarts, but to no avail. I just can’t see my mistake.

I believe the error to lie in either the main function:

int main( void )
{
vector<int> store;
int terminate = 1;

do
{
    int    num   =  0;
    string input = "";

    if( cin.fail() )
    {
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }

    cout << "Please enter a natural number." << endl;
    readLine( input, num );

    cout << "\nThank you. Number is being processed..." << endl;
    workNum( num, store );

    line;
    cout << "Go again? 0 to terminate." << endl;
    cin >> terminate // No checking yet, just want it to work!

    cin.clear();
}while( terminate );

cin.get();
return 0;
}

or in the function that reads the number:

void readLine( string &input, int &num )
{
    int buf = 1;
    stringstream ss;
    vec_sz size;

    if( ss.fail() )
 {
        ss.clear();
     ss.ignore( numeric_limits<streamsize>::max(), '\n' );
 }

    if( getline( cin, input ) )
    {
       size = input.size();
       for( int loop = 0; loop < size; ++loop )
           if( isalpha( input[loop] ) )
               throw domain_error( "Invalid Input." );

    ss << input;
    ss >> buf;

    if( buf <= 0 )
        throw domain_error( "Invalid Input." );

    num = buf;

    ss.clear();
    }
}
  • 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-14T15:37:17+00:00Added an answer on May 14, 2026 at 3:37 pm

    When you call cin >> terminate, it will read the value of terminate, but leave the newline following it in the input stream. Late when you call getline(cin, input), it will read up to the newline, which means it will get an empty string.

    You can discard all characters up to the newline by adding this after cin >> terminate:

    cin.ignore(99, '\n');
    

    Or avoid mixing operator >> and getline.

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

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Let's write the following one public class Person { private… May 14, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer I would just store them in a HashSet<ProgramViewModel>. That way,… May 14, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer I believe you can set up another relation on user… May 14, 2026 at 4:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.