C 在do的第二个循环上显示错误消息

C 在do的第二个循环上显示错误消息,c,C,如果我在C中有一个while或do while循环,是否有一些(本机)方法可以让第二个循环发生一些事情 我要求获得投入;我有这个: int size; do { printf("Size of tower (0 <= x <= 23): "); scanf("%i", &size); } while (size > 23 || size < 0); int大小; 做{ printf(“塔的大小(0我想你想要这样的东西: int s

如果我在C中有一个while或do while循环,是否有一些(本机)方法可以让第二个循环发生一些事情

我要求获得投入;我有这个:

int size;
do {
        printf("Size of tower (0 <= x <= 23): ");
        scanf("%i", &size);
} while (size > 23 || size < 0);
int大小;
做{

printf(“塔的大小(0我想你想要这样的东西:

int size = -1;
int MAX_TRIES = 10;
while (MAX_TRIES--)
{
  printf("Size of tower (0 <= x < 23): ");
  if (scanf("%i", &size) != 1)
  {
    printf("Read error!!\n");
    break;
  }

  if (size >= 0 && size < 23)
  {
    break;
  }

  printf("Error: You entered '%d' which is not in the range 0 <= x < 23\n", size);
}
int size=-1;
int MAX_尝试=10;
而(MAX_尝试--)
{

printf(“塔的大小(0我想你想要这样的东西:

int size = -1;
int MAX_TRIES = 10;
while (MAX_TRIES--)
{
  printf("Size of tower (0 <= x < 23): ");
  if (scanf("%i", &size) != 1)
  {
    printf("Read error!!\n");
    break;
  }

  if (size >= 0 && size < 23)
  {
    break;
  }

  printf("Error: You entered '%d' which is not in the range 0 <= x < 23\n", size);
}
int size=-1;
int MAX_尝试=10;
而(MAX_尝试--)
{

printf(“塔的大小(0我的首选技术包括复制输入,但会简化循环:

printf("Size of tower (0 <= x <= 23): ");      // Try to get good input once.
scanf("%i", &size);

while (size < 0 || 23 < size ) {               // While the user is wrong:
    printf("Invalid size\n");           
    printf("Size of tower (0 <= x <= 23): ");  // Try, try again.
    scanf("%i", &size);
} 

printf(“塔的大小(0我的首选技术包括复制输入,但会简化循环:

printf("Size of tower (0 <= x <= 23): ");      // Try to get good input once.
scanf("%i", &size);

while (size < 0 || 23 < size ) {               // While the user is wrong:
    printf("Invalid size\n");           
    printf("Size of tower (0 <= x <= 23): ");  // Try, try again.
    scanf("%i", &size);
} 

printf(“塔的大小(0转换和错误报告可以移动到一个函数中以简化调用代码。输入由
fgets
获取,值在函数中由
strtol
解析。此函数返回成功或失败。其他值通过指针返回给调用方

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

int get_int_range ( char *line, char **next, char *delim, int *value, int min, int max);//prototype

int main( int argc, char *argv[])
{
    char line[100] = {'\0'};//input buffer
    int valid = 0;
    int size = 0;

    do {
        printf ( "Size of tower (0 <= x <= 23) or quit\n");
        fgets ( line, sizeof ( line), stdin);//read a line
        if ( strcmp ( line, "quit\n") == 0) {
            valid = 0;
            break;// if quit is entered, exit loop
        }
        valid = get_int_range ( line, NULL, "\n", &size, 0, 23);// call to parse a value
    } while ( !valid);// on failure, keep looping the above

    if ( valid) {
        printf ( "Size of tower is %d\n", size);
    }

    return 0;
}

//inputs
// char *line : pointer to text to be parsed
// char **next : pointer to pointer to allow modification of caller's pointer
// char *term : pointer to characters to be considered terminators
// int *value : pointer to int to allow modification of caller's int
// int min : minimum value of range
// int max : maximum value of range
// returns : 0 failure or 1 success
int get_int_range ( char *line, char **next, char *delim, int *value, int min, int max)
{
    long int input = 0;
    char *end = NULL;//will point to end of parsed value

    if ( line == NULL) {
        printf ( "no text to parse\n");
        return 0;
    }
    if ( value == NULL) {
        printf ( "unable to save parsed value\n");
        return 0;
    }
    errno = 0;
    input = strtol ( line, &end, 10);//get the integer from the line. end will point to the end of the parsed value
    if ( ( errno == ERANGE && ( input == LONG_MAX || input == LONG_MIN))
    || ( errno != 0 && input == 0)){// parsing error from strtol
        perror ( "input");
        return 0;
    }
    if ( end == line) {// nothing was parsed. no digits
        line[strcspn ( line, "\n")] = '\0';//remove newline
        printf ( "input [%s] MUST be a number\n", line);
        return 0;// return failure
    }
    // *end is the character that end points to
    if ( *end != '\0' && !( delim && strchr ( delim, *end))) {// is *end '\0'? is *end in the set of term characters?
        line[strcspn ( line, "\n")] = '\0';//remove newline
        printf ( "problem with input terminator: [%s] \n", line);
        return 0;
    }
    if ( input < min || input > max) {// parsed value is outside of range
        printf ( "input out of range %d to %d\n", min, max);
        return 0;
    }

    if ( next != NULL) {// if next is NULL, caller did not want pointer to end of parsed value
        *next = end;// *next allows modification to caller's pointer
    }
    *value = input;// *value allows modification to callers int
    return 1;// success
}
#包括
#包括
#包括
#包括
#包括
int get_int_range(char*line,char**next,char*delim,int*value,intmin,intmax);//原型
int main(int argc,char*argv[])
{
字符行[100]={'\0'};//输入缓冲区
int valid=0;
int size=0;
做{

printf(“塔的大小(0转换和错误报告可以移动到一个函数中以简化调用代码。输入由
fgets
获取,值在函数中由
strtol
解析。此函数返回成功或失败。其他值通过指针返回给调用方

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

int get_int_range ( char *line, char **next, char *delim, int *value, int min, int max);//prototype

int main( int argc, char *argv[])
{
    char line[100] = {'\0'};//input buffer
    int valid = 0;
    int size = 0;

    do {
        printf ( "Size of tower (0 <= x <= 23) or quit\n");
        fgets ( line, sizeof ( line), stdin);//read a line
        if ( strcmp ( line, "quit\n") == 0) {
            valid = 0;
            break;// if quit is entered, exit loop
        }
        valid = get_int_range ( line, NULL, "\n", &size, 0, 23);// call to parse a value
    } while ( !valid);// on failure, keep looping the above

    if ( valid) {
        printf ( "Size of tower is %d\n", size);
    }

    return 0;
}

//inputs
// char *line : pointer to text to be parsed
// char **next : pointer to pointer to allow modification of caller's pointer
// char *term : pointer to characters to be considered terminators
// int *value : pointer to int to allow modification of caller's int
// int min : minimum value of range
// int max : maximum value of range
// returns : 0 failure or 1 success
int get_int_range ( char *line, char **next, char *delim, int *value, int min, int max)
{
    long int input = 0;
    char *end = NULL;//will point to end of parsed value

    if ( line == NULL) {
        printf ( "no text to parse\n");
        return 0;
    }
    if ( value == NULL) {
        printf ( "unable to save parsed value\n");
        return 0;
    }
    errno = 0;
    input = strtol ( line, &end, 10);//get the integer from the line. end will point to the end of the parsed value
    if ( ( errno == ERANGE && ( input == LONG_MAX || input == LONG_MIN))
    || ( errno != 0 && input == 0)){// parsing error from strtol
        perror ( "input");
        return 0;
    }
    if ( end == line) {// nothing was parsed. no digits
        line[strcspn ( line, "\n")] = '\0';//remove newline
        printf ( "input [%s] MUST be a number\n", line);
        return 0;// return failure
    }
    // *end is the character that end points to
    if ( *end != '\0' && !( delim && strchr ( delim, *end))) {// is *end '\0'? is *end in the set of term characters?
        line[strcspn ( line, "\n")] = '\0';//remove newline
        printf ( "problem with input terminator: [%s] \n", line);
        return 0;
    }
    if ( input < min || input > max) {// parsed value is outside of range
        printf ( "input out of range %d to %d\n", min, max);
        return 0;
    }

    if ( next != NULL) {// if next is NULL, caller did not want pointer to end of parsed value
        *next = end;// *next allows modification to caller's pointer
    }
    *value = input;// *value allows modification to callers int
    return 1;// success
}
#包括
#包括
#包括
#包括
#包括
int get_int_range(char*line,char**next,char*delim,int*value,intmin,intmax);//原型
int main(int argc,char*argv[])
{
字符行[100]={'\0'};//输入缓冲区
int valid=0;
int size=0;
做{

printf(“塔的尺寸(0有人检查来自
scanf
的返回值吗?顺便说一句
size>=23
-->
size>23
@Quelklef-当您不记录时,您如何检查来自
scanf
的返回值!@Quelklef如果
scanf
返回0您的
大小测试是未定义的行为,因为
大小
是唯一的初始化。请遵循给您的建议:始终需要测试
scanf
的返回值,否则您的工作将成为问题的牺牲品。
scanf(“%i”,&size)
如果在流中遇到任何数据或某些硬错误,可以返回EOF;如果要读取整数,则可以返回1(该整数的值为
size
);如果没有可读取的整数,它可以返回0(例如,如果您编写了
a
,而不是
1
),在这种情况下,
size
不会改变,并且
a
仍在输入中等待下一次迭代读取,这将失败,因此您将生成一条错误消息,并且…您必须检查输入函数的返回值:
if(scanf(“%i”,&size)!=1)…过程错误…
。有人检查来自
scanf
的返回值吗?顺便说一句
size>=23
-->
size>23
@Quelklef-如果
scanf
返回0,您的
大小测试是未定义的行为,因为
size
未初始化。请遵循您收到的建议:始终需要测试
scanf
的返回值,否则您的工作将成为scanf的牺牲品。
scanf(“%i”,&size)
如果在流中没有遇到数据或某些硬错误,则可以返回EOF;如果要读取一个整数,则可以返回1(该整数的值为
size
);如果没有要读取的整数,则可以返回0(例如,如果您编写了
a
而不是
1
),在这种情况下,
size
不会改变,并且
a
仍在输入中等待下一次迭代读取,这将失败,因此您将生成一条错误消息,并且…您必须检查输入函数的返回值:
if(scanf(“%i”,&size)!=1)…处理错误…
。为什么不检查来自
scanf
的返回值?如果不是1,则吞掉错误输入。您的代码将进入无限循环这是湿代码。我知道这是可以做到的,但我想避免这种情况。为什么不检查来自
scanf
的返回值?如果不是1,则吞掉错误输入。您的代码将l进入无限循环虽然这是湿代码。我知道这是可以做到的,但我想避免这种情况。次要的挑剔:你没有声明
scanf\u ret
。你可以直接测试返回值:
if(scanf(“%I”,&size)!=1)break;
。应该避免无限输入循环。如果多次尝试后结果不正确,则可能不会正确。使用:
enum{MAX\u trys=10};int trys;for(trys=0;i
。错误消息可能比
error!!
-可能
printf(“您输入的%d不在0@JonathanLeffler范围内,谢谢您的建议。我已经纠正了您提到的问题。次要挑剔:您没有声明
scanf\u ret
。您可以直接测试返回值:
if(scanf(“%I”,&size)!=1)break;
。应该避免无限输入循环。如果多次尝试后结果不正确,则可能不会正确。使用:
enum{MAX\u trys=10};int trys;for(trys=0;i
。错误消息可能比
错误更有用!!
-可能
printf(“您输入的%d不在0@JonathanLeffler范围内,谢谢您的建议。I