如何防止输入是某些字符?
How to preventing an input from being certain characters?
这是我的部分代码:除了 A-Z、a-z、1-9 和 _,我不希望用户输入任何内容。我不知道我做错了什么!基本上,如果用户输入的不是那些字符,它会打印出一个错误。我已经尝试调试了几个小时,我觉得我只是让事情变得更多 "messy".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_outputfile(char *outname, int count_attempts);
FILE* CreateFile(){
char name[20];
char user_decision[1];
int count_attempts=0;
for(;;){
get_outputfile(name, count_attempts);
FILE *test;
test = fopen(name,"r");
if(test!=NULL){
fclose(test);
printf("file %s already exists, would you like to overwrite (Y/N)? ",name);
scanf("%s",user_decision);
}
if(user_decision[0]!='Y'&&user_decision[0]!='y'){
count_attempts=0;
continue;
}
else{
return fopen(name,"w");
}
}
return 0;
}
void get_outputfile(char *outname, int count_attempts){
int i;
printf("Enter the output file name:");
scanf("%s",outname);
fgetc(stdin);
for(i=0;i<strlen(outname);i++){
if(((outname[i]<'a'||outname[i]>'z')&&(outname[i]<'A'||outname[i]>'Z')&&(outname[i]<'0'||outname[i]>'9'))&&(outname[i]!='_')){
if (count_attempts>1){
printf("You have exceeded maximum attempts. Restart the program and try again.\n");
exit(0);
}
count_attempts++;
printf("file name must be _, a-z, or A-Z\nEnter the output file name:");
scanf("%s",outname);
i=0;
}
}
printf("returning..\n");
return;
}
int main(){
FILE* afile = CreateFile();
return 0;
}
中的逻辑
if(((outname[i]<'a'||outname[i]>'z')&&(outname[i]<'A'||outname[i]>'Z')&&(outname[i]<'0'||outname[i]>'9'))&&(outname[i]!='_')){
错了。
我建议创建一个函数,其中的逻辑可以简化并且更容易理解。
int isValidCharacter(char c)
{
return ( (c >= 'a' && c <= 'z') || // Takes care of lower case letters
(c >= 'A' && c <= 'A') || // Takes care of uppper case letters
(c >= '0' && c <= '9') || // Takes care of numberss
c == '_' ); // Takes care of _
}
并将其用作:
if ( !isValidCharacter(outname[i]) )
{
if (count_attempts>1){
...
这是我的部分代码:除了 A-Z、a-z、1-9 和 _,我不希望用户输入任何内容。我不知道我做错了什么!基本上,如果用户输入的不是那些字符,它会打印出一个错误。我已经尝试调试了几个小时,我觉得我只是让事情变得更多 "messy".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_outputfile(char *outname, int count_attempts);
FILE* CreateFile(){
char name[20];
char user_decision[1];
int count_attempts=0;
for(;;){
get_outputfile(name, count_attempts);
FILE *test;
test = fopen(name,"r");
if(test!=NULL){
fclose(test);
printf("file %s already exists, would you like to overwrite (Y/N)? ",name);
scanf("%s",user_decision);
}
if(user_decision[0]!='Y'&&user_decision[0]!='y'){
count_attempts=0;
continue;
}
else{
return fopen(name,"w");
}
}
return 0;
}
void get_outputfile(char *outname, int count_attempts){
int i;
printf("Enter the output file name:");
scanf("%s",outname);
fgetc(stdin);
for(i=0;i<strlen(outname);i++){
if(((outname[i]<'a'||outname[i]>'z')&&(outname[i]<'A'||outname[i]>'Z')&&(outname[i]<'0'||outname[i]>'9'))&&(outname[i]!='_')){
if (count_attempts>1){
printf("You have exceeded maximum attempts. Restart the program and try again.\n");
exit(0);
}
count_attempts++;
printf("file name must be _, a-z, or A-Z\nEnter the output file name:");
scanf("%s",outname);
i=0;
}
}
printf("returning..\n");
return;
}
int main(){
FILE* afile = CreateFile();
return 0;
}
if(((outname[i]<'a'||outname[i]>'z')&&(outname[i]<'A'||outname[i]>'Z')&&(outname[i]<'0'||outname[i]>'9'))&&(outname[i]!='_')){
错了。
我建议创建一个函数,其中的逻辑可以简化并且更容易理解。
int isValidCharacter(char c)
{
return ( (c >= 'a' && c <= 'z') || // Takes care of lower case letters
(c >= 'A' && c <= 'A') || // Takes care of uppper case letters
(c >= '0' && c <= '9') || // Takes care of numberss
c == '_' ); // Takes care of _
}
并将其用作:
if ( !isValidCharacter(outname[i]) )
{
if (count_attempts>1){
...