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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:51:10+00:00 2026-06-15T01:51:10+00:00

I consistently run into this problem where I’m trying to validate user input but

  • 0

I consistently run into this problem where I’m trying to validate user input but I find myself using tons of ignores() throughout my program. Since it is a school program I’m only limited to just iostream, cctype, and student.h libraries. The problem is I need to make sure the user does not try to enter alpha characters into an integer field which I thought I covered with if(!(cin >> val)) and then I use cin.ignore(numeric_limits<streamsize>::max(), '\n'); to ignore anything extra (for instance if they try to enter a decimal into an integer field). Something is not working correctly though as I am either unable to input into name field or I get the remaining decimal as the name (if I enter 123.6, the name will be .6). Does someone know a better way to validate integers than using mass ignores?

Main

#include <iostream>
using namespace std;
#include "student.h"
//**************************************************************************
bool validInt(int&);
//**************************************************************************
int main()
{
    Student student;
    bool validInput;

    cout << "You have Chosen to Add a New Student." << endl;
    cout << "---------------------------------------" << endl;

    // Student ID Validation
    do
    {
        cout << "Enter Student ID (ex. 123): ";
        validInput = validInt(student.id);
    }while(!validInput);

    cout << "Enter Student Name (ex. John Doe): ";
    cin.getline(student.name, 50);

    cout << "Enter Student City and State (ex. St. Louis, Missouri): ";
    cin.getline(student.citystate, 50);

    cout << "\n\nStudent ID: " << student.id << endl;
    cout << "Student Name: " << student.name << endl;
    cout << "Student City / State: " << student.citystate << endl;

    return 0;
}
//**************************************************************************
bool validInt(int& val)
{
    bool valid = true;
    if(!(cin >> val))
    {
        cout << "\nERROR: Please enter a Positive Whole Number" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        valid = false;
    }
    return valid;
}

Student Header

#ifndef STUDENT_H
#define STUDENT_H
//**************************************************************************
struct Student
{
    int id;
    char name[50];
    char citystate[50];

    friend ostream& operator<<(ostream& out, const Student& data);

    bool operator == (const Student &rhs) const;
    bool operator != (const Student &rhs) const;
    bool operator < (const Student &rhs) const;
    bool operator > (const Student &rhs) const;
    bool operator <= (const Student &rhs) const;
    bool operator >= (const Student &rhs) const;
};
//**************************************************************************
ostream& operator << (ostream& out, const Student& data)
{
    out << data.id << " " << data.name << endl;
    return out;
}
//**************************************************************************
bool Student::operator == (const Student &rhs) const
{
    return (this->id == rhs.id);
}
//**************************************************************************
bool Student::operator != (const Student &rhs) const
{
    return (this->id != rhs.id);
}
//**************************************************************************
bool Student::operator < (const Student &rhs) const
{
    return (this->id < rhs.id);
}
//**************************************************************************
bool Student::operator > (const Student &rhs) const
{
    return (this->id > rhs.id);
}
//**************************************************************************
bool Student::operator <= (const Student &rhs) const
{
    return (this->id <= rhs.id);
}
//**************************************************************************
bool Student::operator >= (const Student &rhs) const
{
    return (this->id >= rhs.id);
}
#endif
  • 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-15T01:51:11+00:00Added an answer on June 15, 2026 at 1:51 am

    Usually you would use std::getline(std::string&,std::istream&) for line orientated input and parse the number with an istringstream. However, since you aren’t allowed to use neither std::string nor std::stringstream you need to parse the integer yourself.

    But first the explanation for your errors.

    Explanation

    Something is not working correctly though as I am either unable to input into name field

    When you enter a correct integer the newline token \n is not extracted from the string, that’s why cin.getline(student.name, 50); won’t extract anything: the next character in std::cin is \n, the line ends and the std::getline fills student.name with the extractet line, which is empty.

    or I get the remaining decimal as the name (if I enter 123.6, the name will be .6).

    Again, this is the same: you only extract the integer, but you don’t extract the rest of the line.

    Temporary solution

    Ignore the rest of the line in your validInt:

    bool validInt(int& val)
    {
        bool valid = true;
        if(!(cin >> val))
        {
            cout << "\nERROR: Please enter a Positive Whole Number" << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            valid = false;
        }
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        return valid;
    }
    

    Correct solution

    The correct solution is a little bit more elaborate, since you would have to write something that extracts single characters, checks whether they are digits (or a minus or plus), saves those as integer and ignores the rest of the line. Don’t worry, this is possible with only <iostream>

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

Sidebar

Related Questions

I encounter this issue pretty consistently when trying to merge a branch back into
I have run into another problem in relation to a site I am trying
I've run into a problem and I'm not sure if this is just normal
I'm trying to make a fluid grid layout and I've run into a problem
In my iOS project (using xcode only), I continue to run into a problem
So I've run into a wall trying to crack why this WordPress install hogs
I ran into this problem while developing in Objective-C for iOS, but this should
I've run into an early problem with developing for android. I've made my own
I just started using gApps scripts and I have run into two problems with
Today we had a customer run into a problem with a Windows service 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.