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

The Archive Base Latest Questions

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

I have a Win32 console program that I wrote and it works fine. The

  • 0

I have a Win32 console program that I wrote and it works fine. The program takes input from the user and performs some calculations and displays the output – standard stuff. For fun, I am trying to get the program to work on my Fedora box but I am running into an issue with clearing cin when the user inputs something that does not match my variable type. Here is the code in question:

void CParameter::setPrincipal() {
double principal = 0.0;

cout << endl << "Please enter the loan principal: ";
cin >> principal;

while(principal <= 0)
{
    if (cin.fail())
    {
          cin.clear();
          cin.ignore(INT_MAX, '\n');
    }
    else
    {
        cout << endl << "Plese enter a number greater than zero. Please try again." << endl;
        cin >> principal;
    }
}

m_Parameter = principal;

}

This code works in Windows. For example, if the user tries to enter a char data type (versus double) then the program informs the user of the error, resets cin, and allows the user another opportunity to enter a valid value.

When I move this code to Fedora, it compiles fine. When I run the program and enter an invalid data type, the while loop never breaks to allow the user to change the input.

My questions are; how do I clear cin when invalid data is inputted in the Fedora environment? Also, how should I write this code so it will work in both environments (Windows & Linux)?

Thanks in advance for your 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-14T15:13:14+00:00Added an answer on May 14, 2026 at 3:13 pm

    I think it’s a bad idea to use formatted input to read user responses. I’d use getline – something like this:

    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    template <typename T>
    bool Read( T & t, istream & is ) {
        string s;
        if ( ! getline( is, s ) ) {
            return false;
        }
        else {
            istringstream ss( s );
            return ss >> t;
        }
    }    
    
    int main() {
        while(1) {
            double d;
            if ( ! Read( d, cin ) ) {
                cout << "invalid\n";
            }
            else {
                cout << "You entered " << d << "\n";
            }
        }
    }
    

    which works on Windows – I don’t have my Linux box switched on at the moment.

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

Sidebar

Related Questions

I have a Win32 C++ program that validates user input and updates the UI
I'm writing a (Win32 Console) program that wraps another process; it takes parameters as
I have an existing C++ win32 console app. This application contains a main program
I have a console-mode program that uses SQLite3 to maintain a database file. It
I have the windows console program code below that should show up NT Authoroty\Network
I have a c++ console app that has been doing just fine and upon
I have a Win32 program that runs on a loop. I would like to
I have a C#/.NET program that can run both as a console application and
I have 2 files, A.cpp and B.cpp, in a Win32 console application. Both 2
I have the win32 API CommandLineToArgvW which returns a LPWSTR* and warns me that

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.