简体   繁体   中英

C++ to check if user input is a number, not a character or a symbol

I have been trying to incorporate a check to see if the input from the user is a valid input. For example my program wants the user to guess a number between 1-1000. My program works perfectly, except when the user inputs any other character other than a number it goes CRAZY. Anyways, I want it to check and make sure that the user is inputting numbers, not something silly. So I have been going in circles trying to figure this part out. I am sure it is a easy fix, but I am new to programming and this has got me stumped. Any help would be appreciated.

#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

    bool isGuessed=true;
    while(isGuessed)
    {
        srand(time(0));
        int number=rand()%1000+1;
        int guess;
        char answer;


        cout<<"Midterm Exercise 6\n";
        cout<<"I have a number between 1 and 1000.\n";
        cout<<"Can you guess my number?\n";
        cout<<"Please type your first guess:\n\n";
        cin>>guess;


    while(guess!=number)
    {



        if(guess>number)
        {
            cout<<"\nToo high. Try again!\n\n";
            cin>>guess;
        }

        if(guess<number)
        {
            cout<<"\nToo low. Try again!\n\n";
            cin>>guess;
        }
    }
    if(guess==number)
        { 
            cout<<"\nExcellent! You have guess the number!\n";
        }
            cout<<"Would you like to play again (y or n)?\n\n";
            cin>>answer;
            cout<<"\n";

    if(answer!='y')
        {
            isGuessed=false;
            cout<<"Thanks for playing!\n\n";
            system ("PAUSE"); 
            return 0;
        } 

    }


    return 0;
}

Here's a snippet I like to keep around for using in these sorts of situations.

int validInput()
{
    int x;
    std::cin >> x;
    while(std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(std:numeric_limits<std::streamsize>::max(),'\n');
        std::cout << "Bad entry.  Enter a NUMBER: ";
        std::cin >> x;
    }
    return x;
}

Then any place you want to use cin>>guess , instead, use guess = validInput();

Since spreading around code similar to what @nhgrif suggests for every acquisition is tedious and error-prone, I usually keep around the following header:

#ifndef ACQUIREINPUT_HPP_INCLUDED
#define ACQUIREINPUT_HPP_INCLUDED

#include <iostream>
#include <limits>
#include <string>

template<typename InType> void AcquireInput(std::ostream & Os, std::istream & Is, const std::string & Prompt, const std::string & FailString, InType & Result)
{
    do
    {
        Os<<Prompt.c_str();
        if(Is.fail())
        {
            Is.clear();
            Is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        Is>>Result;
        if(Is.fail())
            Os<<FailString.c_str();
    } while(Is.fail());
}

template<typename InType> InType AcquireInput(std::ostream & Os, std::istream & Is, const std::string & Prompt, const std::string & FailString)
{
    InType temp;
    AcquireInput(Os,Is,Prompt,FailString,temp);
    return temp;
}

#endif

Usage example:

//1st overload
int AnInteger;
AcquireInput(cout,cin,"Please insert an integer: ","Invalid value.\n",AnInteger);

//2nd overload (more convenient, in this case)
int AnInteger=AcquireInput(cout,cin, "Please insert an integer: ","Invalid value.\n");

The AcquireInput function allows to read any type for which there's an operator>> available and automatically retries (cleaning up the input buffer) if the user inserts invalid data. It also prints the given prompt before asking the data and the error message in case of invalid data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM