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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:10:10+00:00 2026-05-16T18:10:10+00:00

void get_english_input() { string input = ; stringstream my_string(input); int ft; double in; while(true)

  • 0
void get_english_input() {
    string input = " ";
    stringstream my_string(input);
    int ft;
    double in;

    while(true) {    
        cout << "Enter an integer value of feet." << endl;
        getline(cin, input);
        my_string << input;
        if(my_string >> ft)
            break;
        cout << "Invalid input! Please try again." << endl;
    }
    cout << "you entered " << ft << " as the int value for feet." << endl;
    /*while(true) {
        cout << "Enter a double value of inches." << endl;
        getline(cin, input);
        my_string << input;
            break;
    cout << "Invalid input! Please try again." << endl;
    }
    cout << "we are done entering english input" << endl;
    cout << "feet = " << ft << endl;
    cout << "inches = " << in << endl;*/
}

This code is supposed to test if the input is an integer by trying to put the contents of my_string into ft. If I enter a letter instead of an integer I get the error message “Invalid input! Please try again,” which is what is supposed to happen. The problem is, after I get that message once, I will get it for every input after that, even if the next input is valid.


Someone suggested that I should use std::cin.clear(); to clear the error-flags. I tried putting it before the getline() and it did not change the problem. Was I using this incorrectly?

  • 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-16T18:10:11+00:00Added an answer on May 16, 2026 at 6:10 pm

    You could reset the error state of my_string:

    my_string.clear();
    my_string.ignore( /* big number of choice */ );
    

    But I think it would be easier here just to reinitialize it every time:

    while(true) {    
        cout << "Enter an integer value of feet." << endl;
        getline(cin, input);
        stringstream my_string(input);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void getSelectedTime(final String json){ Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int
why doesn't the element get swapped public static void SwapArray(int[,] arr) { for (int
[DllImport(Project1.dll)] unsafe static extern void CallFu(string OpenFile, string SaveFile); This is dll on C++
i have situation like this: class IData { virtual void get() = 0; virtual
How can this test fail? [TestMethod] public void Get_Code() { var expected = new
I would like to have declaration like this: void Date::get_days_name(const Date& = this) which
I get the StackOverflow error running this particular code: litera.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable
I can get the current selected row in this way: private void DataGridView1_CellContentClick(object sender,
I get a compile time error on countList method. public static void countList( List<?
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,

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.