Hey everyone
I am trying to write a program to get user's input but only accepts alphabetic characters, nothing else and I want it to ask the user to enter a valid word until they have finally entered a valid one. I have the following code for it but it does not work properly. can anyone please help me with it ?
void CheckBound (char word1[], int SIZE1)
{
int i;
int w1[SIZE4]= {0};
int found;
for (i=0;i<strlen(word1);i++)
{
w1[i]=(int)word1[i];
if (w1[i] >97 && w1[i] <122)
{
found = 1;
}
else
{
printf("Please re-enter your word: ");
fgets(word1, SIZE1, stdin);
word1[strlen(word1)-1]='\0';
ChangeCase(word1, SIZE1);
int w1[SIZE4] = {0};
}
}
return;
}
/*The first array stores and prints the first player's secret word*/
void GetUser1(char word1[], int SIZE1)
{
printf("Please enter your word: ");
fgets(word1, SIZE1, stdin);
word1[strlen(word1)-1]='\0';
ChangeCase(word1, SIZE1);
CheckBound(word1, SIZE1);
printf("%s \n", word1);
use C++ streams instead of C functions.
Use is_alpha instead of checking character codes manually. It can fail on any non-ascii codepages. <algorithm> header has all_of function which is really useful in that case: http://ideone.com/fdRIWl
Also, if you ever need to check for a specific character, you can simply write the character itself instead of its code: if(letter=='a')
instead of if(letter==97)