gpt4 book ai didi

c - 如何检查多表C程序中输入的整数?

转载 作者:行者123 更新时间:2023-11-30 19:05:26 25 4
gpt4 key购买 nike

我有一个 C 程序,当我输入一个整数时,可以打印多个表格。我还想检查整数,然后如果输入错误的类型,请注意错误,例如“1a”将显示“错误输入!”或“-5”将显示“输入的数字必须大于0”。请有人帮助我。感谢您的阅读!这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
int n;
printf ("Enter an integer: ");
scanf ("%d", &n);
check_input (n);
multible (n);
}

int clean_stdin ()
{
while (getchar () != 'n') {
}
return 1;
}

void check_input (int n)
{
char c;
do {
printf ("Enter an integer: ");
} while (((scanf ("%d%c", &n, &c) != 2 || c != 'n') && clean_stdin ()));

printf ("done, number is %d", n);
return 0;
}

int multible (int n)
{
for (int i = 1; i <= 10; ++i) {
printf ("%d * %d = %d \n", n, i, n * i);
}
return 0;
}

最佳答案

温和地说,您的代码是困惑的。您查看c != 'n'你应该在哪里检查 c != '\n''n'是 ASCII 字符 110 , '\n'换行字符(ASCII 10)。

接下来,您的返回类型为 clean_stdin应该是void -- 您什么也不返回,您的返回类型为 check_input应该可以让您指示检查成功/失败。您不使用argcargv ,所以您的声明为 main()应该是int main (void) 。您不使用 stdlib.h 中声明的任何函数,因此无需包含该 header 。 main()中看不到您的任何功能因为它们是在 main() 之后声明的并且上面没有指定函数原型(prototype) main() .

让我们看看如何才能完成您想要做的事情。首先,我们来清理一下clean_stdin ,所以它做了它想要做的事情,例如

void clean_stdin ()
{
int c = getchar();

while (c != '\n' && c != EOF)
c = getchar();
}

接下来,如果您使用 scanf (或任何该函数系列),您必须检查返回并通过生成手册EOF来处理用户取消输入的情况。 ,您必须解决匹配输入失败的情况(例如,返回值小于指定的转换次数),最后,您识别并处理良好的输入情况,使用clean_stdin旨在删除 stdin 中保留的任何字符,这样他们就不会在您下次通话时咬您。

由于您的意图是获取整数输入,因此不需要获取它,然后在单独的函数中检查它,只需创建一个填充整数值(其地址作为参数传递)并返回一个函数1指示失败0表示成功,例如

int get_int_input (int *n)
{
*n = 0;

for (;;) {
int rtn, tmp;

printf ("Enter an integer: ");

rtn = scanf ("%d", &tmp);

if (rtn == EOF) { /* handle user canceled input */
fputs ("(user canceled input).\n", stderr);
return 1;
}
if (rtn == 1) { /* good input, set *n = tmp, clean, return */
*n = tmp;
clean_stdin();
return 0;
}
/* handle matching or input failure */
fputs ("error: invalid input.\n", stderr);
clean_stdin();
}
}

您的multible函数将按原样工作,但您的循环限制与通常看到的限制相反,例如i = 0; i < 10 ,那里有一个调整:

int multible (int n)
{
for (int i = 0; i < 10; ++i) {
printf ("%d * %d = %d \n", n, i + 1, n * (i + 1));
}
return 0;
}

然后剩下的就是获取所需的输入并调用 multible ,例如

int main (void)
{
int n;

if (get_int_input (&n))
return 1;

multible (n);

return 0;
}

接下来,始终在启用警告的情况下进行编译,并且不要接受代码,直到在没有警告的情况下干净地编译。要启用警告,请添加 -Wall -Wextra给您gccclang编译字符串。 (添加-pedantic以获得几个额外的警告)。对于 clang ,您可以使用 -Weverything 。对于 VS(windoze 上的 cl.exe),添加 /Wall (或 /W3 以限制一些无关的警告)。阅读并理解每个警告。他们将识别任何问题以及问题发生的确切线路。您可以像从大多数教程中一样,通过简单地聆听编译器告诉您的内容来了解​​尽可能多的编码知识。

总而言之,您可以执行以下操作:

#include <stdio.h>

void clean_stdin ()
{
int c = getchar();

while (c != '\n' && c != EOF)
c = getchar();
}

int get_int_input (int *n)
{
*n = 0;

for (;;) {
int rtn, tmp;

printf ("Enter an integer: ");

rtn = scanf ("%d", &tmp);

if (rtn == EOF) { /* handle user canceled input */
fputs ("(user canceled input).\n", stderr);
return 1;
}
if (rtn == 1) { /* good input, set *n = tmp, clean, return */
*n = tmp;
clean_stdin();
return 0;
}
/* handle matching or input failure */
fputs ("error: invalid input.\n", stderr);
clean_stdin();
}
}

int multible (int n)
{
for (int i = 0; i < 10; ++i) {
printf ("%d * %d = %d \n", n, i + 1, n * (i + 1));
}
return 0;
}

int main (void)
{
int n;

if (get_int_input (&n))
return 1;

multible (n);

return 0;
}

编译时启用警告

$ gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o bin/getintinput getintinput.c

示例使用/输出

考虑到错误输入和用户取消的情况:

$ ./bin/getintinput
Enter an integer: foo
error: invalid input.
Enter an integer: foo
error: invalid input.
Enter an integer: (user canceled input).

有效的输入情况:

$ ./bin/getintinput
Enter an integer: 12
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120

仔细检查一下,如果您还有其他问题,请告诉我。

关于c - 如何检查多表C程序中输入的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com