C编程int验证


int main() {
int choice;
printf("n----------Welcome to Coffee Shop----------");
printf("nnt1. Login   ");
printf("nt2. Sign Up ");
printf("nnPlease enter 1 or 2: ");
scanf(" %d", &choice);
system("cls||clear");

//put while loop to check
switch (choice) {
case 1:
system("cls||clear");
login();
break;
case 2:
system("cls||clear");
user_signup();
break;
default:
printf("nInvalid Numbern");
main();
break;
}
return 0;
}

如果输入不是整型,我如何让用户重新键入他们的输入?我试图使用isdigit(),但它不会工作,我可以知道什么是解决方案吗?

scanf返回成功匹配的输入项数。所以你可以建立一个循环,直到你得到一个项目。

一个非常简单的方法是:

while(1)
{
int res = scanf(" %d", &choice);
if (res == 1) break;  // Done... the input was an int
if (res == EOF) exit(1); // Error so exit
getchar();  // Remove an discard the first character in stdin
// as it is not part of an int
}
printf("Now choice is an int with value %dn", choice);

也就是说,我建议你看看fgetssscanf。它们通常是比scanf更好的方法

scanf(" %d", &choice);无法处理转换后的输入超出int范围的情况。这会导致未定义行为

Drop usingscanf().

使用fgets()将用户输入的读入字符串,然后验证。
对于str2subrange(),请参见为什么stdlib.h中没有strtoi

bool validate_int(const char *s, int *value) {
const int base = 10;
char *endptr;
errno = 0;
long lvalue = str2subrange(s, &endptr, base, INT_MIN, INT_MAX);
// For OP's case, could instead use 
long lvalue = str2subrange(s, &endptr, base, 1, 2);
if (value) {
*value = (int) lvalue;
}
if (s == endptr) {
return false; // No conversion
}
if (errno == ERANGE) {
return false; // Out of range
}
// Skip trailing white-space
while (isspace(((unsigned char* )endptr)[0])) {
endptr++;
}
if (*endptr != '') {
return false; // Trailing junk
}
return true;
}

我认为最好将其作为字符串接收并检查是否所有字符都是数字。如果所有字符都是数字,它们会将其转换为int

这样的

int main() {
int choice;
char chars[100];
printf("n----------Welcome to Coffee Shop----------");
printf("nnt1. Login   ");
printf("nt2. Sign Up ");
printf("nnPlease enter 1 or 2: ");
scanf(" %s", chars);
choice = stringToInteger(chars);
system("cls||clear");
//put while loop to check
switch (choice) {
case 1:
system("cls||clear");
login();
break;
case 2:
system("cls||clear");
user_signup();
break;
default:
printf("nInvalid Numbern");
main();
break;
}
return 0;
}
int stringToInteger(char* chars)
{
int i = 0, number = 0;
while(chars[i] >= '0' && chars[i] <= '9')
{
number = chars[i++] - '0' + number * 10;
}
return number;
}

scanf()函数返回已成功转换和分配的字段数。在scanf(" %d", &choice);行,你可以这样做:

do {
printf("nnPlease enter 1 or 2: ");
int result=scanf(" %d", &choice);
if (result==1) {
break;
}
printf("nInvalid Numbern");
} while (1);

应该使用循环来代替对main()的递归调用。对于单字符输入,也考虑getchar()而不是scanf。例如,下面的代码可以满足您的要求。

#include <stdio.h>
#include <stdlib.h>
int main() {
int valid_choice = 0;
while (!valid_choice) {
printf("n----------Welcome to Coffee Shop----------");
printf("nnt1. Login   ");
printf("nt2. Sign Up ");
printf("nnPlease enter 1 or 2: ");
char choice = getchar();
system("cls||clear");

//put while loop to check
switch (choice) {
case '1':
valid_choice = 1;
system("cls||clear");
login();
break;
case '2':
valid_choice = 1;
system("cls||clear");
user_signup();
break;
default:
printf("nInvalid Numbern");
break;
}
}
return 0;
}

最新更新