Q:
Hi Peter
Will you please tell me why this procedure doesn't work properly?
It's supposed to control the user's input - so that he could enter only
alphabetical characters.
It does detect the wrong character, it announces
the mistake and then it procedes to the next field. I know it's something
to do with the way i'm calling the procedure, but i cannot figure out what
exactly goes wrong...
int controle_texte(char chaine[25])
{
int i,taille;
taille=strlen(chaine); /* definit la longueur de la chaine, pour
determiner la taille exacte d'une chaine saisie */
if (taille < 2) /* < 2, car chaine de caracteres se termine par /0 */
{
err_msg("*** Entrez les donnees, s.v.p! ***");
return 0;
}
for (i=0;i<taille-1;i++)
{
if(!isalpha(chaine[i]) && (chaine[i]!=' ') && (chaine[i]!='-') &&
(chaine[i]!='\'') )
/* teste si la chaine ne contient que caracteres alphabetique */
{
err_msg("*** Erreur de saisie! ***");
return 0;
}
}
return 1;
}
/* This is how I call the procedure */
do {
printf ("\n\n\t\tNOM :");
fgets(temp,25,stdin); /* lit une chaine de caracteres */
if (controle_texte(temp)==1) /* verifie, si la saisie est bonne */
{ marqueur=true; strcpy(nom,temp);
/* on copie les caracteres verifies, un par un dans un nouveau tableau */
}
} while(!marqueur);
Looking forward for your opinion.
thanks,
Vera
A:
Where do you think the variable marqueur is set to false in case the text
is OK? I am afraid that this variable is not initialized.
if (controle_texte(temp)==1) /* verifie, si la saisie est bonne */
{ marqueur=true; strcpy(nom,temp);
/* on copie les caracteres verifies, un par un dans un nouveau tableau */
}
else marqueur = false;
should make it work.
Regards,
Peter
[ back to toc ]