PDA

View Full Version : Code to reject all alphabet key in


west_dweller79
11-06-2005, 07:20 AM
Hi,

One programming problem here to ask.

I'm doing a program that only allow me to key in numbers. So my problem here is how can i write up the coding that only allow me to key in number and not alphabet.

I've done the numbers part, but not sure how can i write the code to reject any alphabet key in (Uppercase and lowercase).

Thanks in advance.
west_dweller79

Fissure
11-06-2005, 10:12 PM
Hi,

One programming problem here to ask.

I'm doing a program that only allow me to key in numbers. So my problem here is how can i write up the coding that only allow me to key in number and not alphabet.

I've done the numbers part, but not sure how can i write the code to reject any alphabet key in (Uppercase and lowercase).

Thanks in advance.
west_dweller79

Which language are you Using?

Since character inputs are really just integers displayed in a different way, you could try converting the ASCII character input into an integer:

ASCII digits start at value 48 and end at 57. 0 - 9.

in java

if(((int)the_char > 47) && ((int)the_char < 58)){
do this if the input was a digit
}
else{
do this if the input was not a digit
}

west_dweller79
11-07-2005, 03:47 PM
Sorry, i'm using C++ to write this code.

Actually i'm writing a simple game which guess a number from 0 - 100. Therefore i just want number and not alphabet.

Is there any simple way to convert alphabet into number? What are the number that represent the alphabet?

Thanks in advance
west_dweller79

oracleguy
11-07-2005, 07:41 PM
Are you doing a cin to an integer? If so you could do a check to see if the error flags on the cin have been set, which they will be if you try to cin 73b into a variable declared as a int.

I have some specific code you can take a look that illustrates this that I'll post when I get back to my computer at home later today.

west_dweller79
11-10-2005, 03:15 PM
Oracleguy, still waiting for your code here

oracleguy
11-10-2005, 09:56 PM
Oops, sorry, I read so many threads each day, some times a few can slip my mind.

Here is the code:

int number = 0;
cout << "Enter in an ammount: ";
//This while loop catches cin errors and re-prompts the user to try again
while(!(cin >> number))
{
cin.clear(); //Clear any error flags
cout << "Invalid input, please try again.\nEnter the ammount: ";
cin.ignore(cin.rdbuf()->in_avail());
}

deadimp
11-11-2005, 03:40 AM
Only problem with that code is, if the user puts in any string that is eventually converted to zero ("0",or any non-numeric string), it will prompt them for another. It probably wouldn't be a problem for this, since you need 1-100, but just in case, you can simply ask for a string, and then check/convert it yourself...
Sorry if I get some of the C string functions wrong, I've been coding in PHP for a while now.
bool is_numeric(char *str) {
const char* chrNum=".0123456789"; //Numerical characters, in decimal (base 10) format
const int chrNumCount=strlen(chrNum); //I'm not sure if this will be a true constant...
int length=strlen(str), i, a;
bool has_decimal=false; //Checking for a decimal place
for (i=0;i<length;i++) {
//There are other ways, this is just one (maybe not super-efficient)
//This can be in both
if (str[i]==chrNum[0]) {
if (has_decimal) return false; //Already has a decimal...
has_decimal=true;
}
else {
for (a=1;a<chrNumCount;a++) {
if (str[i]==chrNum[a]) return true; //Good
}
return false; //None of the characters matched up
}
}
}

//Main code...
char buffer[100];
do {
cout << "Number: ";
cin >> buffer;
} while (!is_numeric(buffer));
//Convert to integer
int number=atoi(buffer);
Yeah, it's a little lengthy, and the code could be reorganized, but you get the basic jist of it. Plus, if you are going to use some alternate methods of input (other than cin and such, probably in a game, etc), this could help some.

west_dweller79
11-12-2005, 06:28 AM
Hi Oracleguy,

Try adding in your bit to my code, but doesn't seem to work after adding in. Also the program doesn't loop back if out of range. Maybe i'm not sure what the code mean. Please help.

Below is my program

#include <iostream.h>
#include <stdlib.h>
#include "function.h"

#define MAX 100
#define MAX_TRIES 7

int main()
{
int value, input=0;
int i, less=0, more=100;

srand(time(NULL));
value = rand()%MAX+1;
cout << value <<endl;

for(i=1; i<MAX_TRIES+1; i++)
{
cout << "\nPlease enter value between " << less << " and " << more << endl;
cout<< ">>";
cin>> input;

if(0 > input || input > 100)
cout << "\nOut of range!!!";

if(!(cin >> input))
{
cin.clear(); //Clear any error flags
cout << "Invalid input, please try again.\nEnter the amount: ";
cin.ignore(cin.rdbuf()->in_avail()); }
}

if(input < value)
{
cout << "\nValue more than " << input;
if(less < input)
less = input;
}

if(input > value)
{
cout << "\nValue less than " << input;
if(more > input)
more = input;
}

if(i==MAX_TRIES)
cout << "\nValue is " << value << "\n\n";

if(input == value)
{
cout << "\nCorrect\n" << i << " attempts needed\n\n";
break;
}
}

system("PAUSE");
return 0;
}

Your code is coloured in magenta.
What does these line of code mean?

if(!(cin >> input)) // check if input = number??
{
cin.clear(); //Clear any input that is not number??
cout << "Invalid input, please try again.\nEnter the amount: ";
cin.ignore(cin.rdbuf()->in_avail()); // Not sure wat it mean
}

I've put in some comment by the side of ur code in red. Pls see if i'm right and also wat the last line of code mean?

Thanks for ur help
west_dweller79

oracleguy
11-12-2005, 08:43 PM
If you want it to re-prompt the user if they enter something invalid, you gotta use a while loop (like the code i posted) so it will actually repeat. An if statement will not.

Basically what it does is if the cin >> input returns false, it goes into the loop because the user entered something that can't be put into the data type of input. And then it repeats until it can, at which time it drops out of the loop and continues running your program.