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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:06:21+00:00 2026-06-14T20:06:21+00:00

I just wrote a pretty simple input validation algorithm. I thought it was pretty

  • 0

I just wrote a pretty simple input validation algorithm. I thought it was pretty good, but when I was testing it I saw something I didn’t understand. Here is the code first.

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>

using namespace std;

int main()
{
cout << "Simple Menu"
    << "\n\t\t1. Input"
    << "\n\t\t2. Exit" << endl;

string input = "";

int myNum = 0;

char myChar = {0};

do
{
    cout << "Please enter a valid menu option: ";
    getline(cin,input);

    if (!(stringstream(input) >> myNum))
    {
        cout << "Please enter a valid menu option: ";
        getline(cin,input);
    }           

}while(myNum < 1 || myNum > 5 || input.length() != 1);

cout << "\nThe menu option you entered is: " << myNum << endl << endl;

return 0;       
}

Turns out, if I put .1 in as a choice it asks me to enter a valid option, which is good. However, if I enter 2 (a valid option) immediately following entering .1, it still reacts as though I put in a bad value. Here is the console window for such an event:

Simple Menu
                1. Input
                2. Exit
Please enter a valid menu option: .1
Please enter a valid menu option: 2
Please enter a valid menu option: 2

The menu option you entered is: 2

Press any key to continue . . .

Any idea why this is?

  • 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-14T20:06:22+00:00Added an answer on June 14, 2026 at 8:06 pm

    Your current code:

    do
    {
        cout << "Please enter a valid menu option: ";    // first prompt
        getline(cin,input);                              // you enter .1
    
        if (!(stringstream(input) >> myNum))
        {
            cout << "Please enter a valid menu option: ";// second prompt
            getline(cin,input);                          // this data is discarded
        }           
    
    }while(myNum < 1 || myNum > 5 || input.length() != 1);
    

    Change to:

    do
    {
        cout << "Please enter a valid menu option: "; // first prompt
        getline(cin,input);                           // .1
    
        if (!(stringstream(input) >> myNum)) continue;// if parsing fails loop back
    } while(myNum < 1 || myNum > 5 || input.length() != 1);
    

    Simplify further to:

    myNum = -1;
    do
    {
        cout << "Please enter a valid menu option: "; // first prompt
        getline(cin,input);                           // .1
    
        stringstream(input) >> myNum;
    } while(myNum < 1 || myNum > 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok pretty simple but i dont know how... i just want to make an
Okay, I'm pretty sure I'm missing something very obvious here, but I just couldn't
My problem I'm pretty positive is simple, I must just be missing something.. just
I just wrote a very simple perl tk script to read a huge text
I am trying to write a simple script in JavaScript, just for testing purposes.
Ok so basically I wrote a not very pretty GUI that gives random simple
I'm not very familiar with CoreAnimation, so I hope I've just missed something pretty
Let's say I wrote a pretty simple class called ValueFinder that looks like this:
So, I am pretty new to web programming and I just understand the basic
Just wrote a web service and try to return columns of a sharepoint list

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.