Hello I was wondering how can I set a char limitation in my code when I am asking for an int? When you input a char my program goes crazy. Let's put the following simple program as an example:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int userChoice;
    
    do 
    {
         cout << "Please enter a number from 1 to 10: ";
         cin >> userChoice;
    
    }
    while( userChoice < 1 || userChoice > 10);
    
    cout << "Thank you." << endl;
    
                 
    
    
   system("pause");
   return 0;
}

I will appreciate it.

Recommended Answers

All 10 Replies

Read your input as a string. Then test the characters for valid digits. If good, convert to integer. If not...

char c='\0';
	userChoice=0;
	cout << "Please enter a number from 1 to 10: ";
	while(c!=13)
	{       c='\0';
		c=getche();
		if(c>47 && c<58)
		userChoice=userChoice*10+(int)(c-'0');
	}

here I use getche to take input from use and check the assic code if its character skip this other wise put it in userChoice
use this code to restrict user to input character
may be it will help full for you.

Best Of Luck.

I don't comprehend getche.

use this simple function, :

int getInt(const char *msg){
 cout << msg ;
 int n = 0;
 while(!(cin >> n)){
    cout <<  "Input failed...try again\n";
    cin.clear();
    while(cin.get() != '\n');
 }
 return n;
}

Basically, the while(!(cin>>n)) checks the cin gets what we are expecting it to get. We are expecting it to get an int, so if a letter is entered, cin will fail and set its fail flags. Thus the reason why we did cin.clear(). That clears the flags, and resets the stream. Now all thats left is to discard the invalid input in the stream. To do that, we did while(cin.get() != '\n');. That code discards the invalid character inputted until it reads a newline character.

I don't comprehend getche.

The function getche() is a non-standard input function defined in the non-standard header "conio.h". Don't use it.

Instead, use something standard like istream::get() or similar.

EDIT:
Or do as first person suggested. Approach your problem directly instead of dancing around it.

Ok firstperson, I see what the code does and it removed my issue with the invalid character inputs, but now it messes up my number inputs as when you input it doesn't do directly to the if then statement, it lets you input again.

// This program asks the user to input a number from 1 to 100
// as it produces a random number, and it informs the user on
// how relatively close they are to the number.

// Javier Falcon

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    
    int counter = 0,                       // Variable to store number of tries
        n = 0,
        theNumber,                         // Random number varible, limit 100
        theGuess;                          // Variable for user input
    
    srand ( time(NULL) );

    theNumber = rand() % 100 + 1;
    

    
    do
    {
        cout << "I am thinking of a number from 1 to 100, please guess that number: " 
        << endl;                           // Ask for the user input
        cin >> theGuess;                          // User input 
        while(!(cin >> n))
        {    
             cout <<  "Input failed... Please try again\n";    
             cin.clear();    
             while(cin.get() != '\n'); 
        }
        counter++;                         // Increase to the variable per loop
        if (theNumber > theGuess)                    // If stat. for number being low
           cout << "Number is too low, please try again." << endl;
        else if (theNumber < theGuess)               // If stat. for number being high
           cout << "Number is too high, please try again." << endl;
        
    }
    while(theGuess != theNumber);                    // Run loop again if not == to number
    
    cout << "Congratulations, you found the number in " << counter << " tries." 
    << endl;                               // Congratulate user upon success
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

while(! (cin >> theNumber) )
And remove the cin before that line

There still seems to be a problem in the code, now that the piece is added, when you input a valid number it doesn't automatically re-initiate the loop.

So move the initialization code where you need it to be.

^ Lol I am new at this and am trying my best.. So please bear with me... I have been coding for hours now and am a little frustrated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.