scanf函数验证坐着等待另一个输入。为什么? [英] scanf validation sits and waits for another input. Why?

查看:126
本文介绍了scanf函数验证坐着等待另一个输入。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的这个样本锻炼,一切正常,我想它,但有一个问题我不明白。

在提供输入:如果我连续无效的项目似乎一切工作的伟大。但是,如果我在第二个问题的情况下,第一个问题,或1,2-的情况下,进入不同的数1,2,3,程序只是坐在那里,直到一个新的输入给出。如果另一个无效条目制成,它可以追溯到错误无效项消息,如果输入的适当数量,一切都相处得移动。

我不明白为什么它停止等待第二个输入...有人吗?

谢谢你们。

 的#include<&stdio.h中GT;静态INT调用getInt(为const char *提示)
{
    int值;
    的printf(%S,提示);
    而(scanf函数(%d个,&安培;!值)= 1)
    {
      的printf(您输入无效\\ nGive它的另一个尝试:%S,提示);
      的getchar();
      scanf函数(%d个,&安培;值);
    }
    返回值;
}诠释主(){INT wood_type,table_size,table_price;的printf(请输入);
wood_type =调用getInt(1松树,2橡木,和3桃花心木:);的printf(请输入);
table_size =调用getInt(1大,2小:);的printf(\\ n);开关(wood_type){
   情况1:
        table_price =(table_size == 1)? 135:100;
        的printf(为您的新表的成本是:$%I,table_price);
        打破;    案例2:
        table_price =(table_size == 1)? 260:225;
        的printf(为您的新表的成本是:$%I,table_price);
        打破;    案例3:
        table_price =(table_size == 1)? 345:310;
        的printf(为您的新表的成本是:$%I,table_price);
        打破;    默认:
        table_price = 0;
        的printf(为您的新表的成本是:$%I,table_price);
        打破;}
}


解决方案

您最有可能需要刷新您的输入缓冲区(尤其是与多个 scanf函数在函数调用)。 scanf函数,换行的'\\ n'后保留在输入缓冲区。 fflush 确实不会做到这一点,所以你需要做手工。一个简单的做...而循环的作品。试试看:

编辑:

 静态INT调用getInt(为const char *提示)
{
    int值;
    INT℃;    而(的printf(提示)及和放大器; scanf函数(%d个,&安培;!值)= 1)
    {
        做{C =的getchar(); }而(C ='\\ n'和;!和C = EOF!); //刷新输入
        的printf(无效的入口,再试试......);
    }    返回值;
}

的空行你,如果你输入的没有 scanf函数的正常行为。它正在等待输入(某些输入)。如果你想你的日常立即的情况下 [进入] 键pressed,那么你需要使用另一个程序中读取标准输入像(函数getline 与fgets )。 函数getline 是pferred因为它返回的字符数读(你可以测试)$ P $。然后,您可以使用的atoi (在<文件stdlib.h> )将字符串值转换为整数。这会给你你需要的灵活性。

例如:

  INT newgetInt(字符*提示)
{
    字符*线= NULL; / *指向与函数getline()使用* /
    ssiz​​e_t供读= 0; / *字符数读* /
    为size_t N = 0; / *字符的NUMER阅读,0没有限制* /
    静态INT NUM = 0; / *号的结果* /    而(的printf(\\ n%S,提示)及及(读=函数getline(安培;线,&安培;!N,标准输入))= - 1)
    {
        如果((NUM =的atoi(线)))
            打破;
        其他
            的printf(无效的输入,再试一次... \\ n);
    }    返回NUM;
}

I was working on this sample exercise, and everything works as I would like it to, but there is one behavior I don't understand.

When providing input: if I make consecutive invalid entries everything seems to work great. But if I enter a number different from 1,2,3 in the case of the first question, or 1,2 in the case of the second question, the program just sits there until a new input is given. If another invalid entry is made, it goes back to the error "invalid entry" message, and if an appropriate number is entered, everything moves along fine.

I do not understand why it stops to wait for a second input...anyone?

Thanks guys.

#include <stdio.h>

static int getInt(const char *prompt)
{
    int value;
    printf("%s",prompt);
    while (scanf("%d", &value) !=1)
    {
      printf("Your entry is invalid.\nGive it another try: %s", prompt);
      getchar();
      scanf("%d", &value);
    }
    return value;
}

int main() {

int wood_type, table_size, table_price;

printf("Please enter " );
wood_type = getInt("1 for Pine, 2 for Oak, and 3 for Mahogany: ");

printf("Please enter ");
table_size = getInt("1 for large, 2 for small: ");

printf("\n");

switch (wood_type) {
   case 1:
        table_price = (table_size == 1)? 135:100;
        printf("The cost of for your new table is: $%i", table_price);
        break;

    case 2:
        table_price = (table_size == 1)? 260:225;
        printf("The cost of for your new table is: $%i", table_price);
        break;

    case 3:
        table_price = (table_size == 1)? 345:310;
        printf("The cost of for your new table is: $%i", table_price);
        break;

    default:
        table_price = 0;
        printf("The cost of for your new table is: $%i", table_price);
        break;

}
}

解决方案

You most likely need to flush your input buffer (especially with multiple scanf calls in a function). After scanf, a newline '\n' remains in the input buffer. fflush does NOT do this, so you need to do it manually. A simple do...while loop works. Give it a try:

edit:

static int getInt(const char *prompt)
{
    int value;
    int c;

    while (printf (prompt) && scanf("%d", &value) != 1)
    {
        do { c = getchar(); } while ( c != '\n' && c != EOF );              // flush input
        printf ("Invalid Entry, Try Again...");
    }

    return value;
}

The blank line you get if you enter nothing is the normal behavior of scanf. It is waiting for input (some input). If you want your routine to immediately prompt again in the case the [Enter] key is pressed, then you need to use another routine to read stdin like (getline or fgets). getline is preferred as it returns the number of characters read (which you can test). You can then use atoi (in <stdlib.h>) to convert the string value to an integer. This will give you the flexibility you need.

example:

int newgetInt (char *prompt)
{
    char *line = NULL;      /* pointer to use with getline ()       */
    ssize_t read = 0;       /* number of characters read            */
    size_t n = 0;           /* numer of chars to read, 0 no limit   */
    static int num = 0;     /* number result                        */

    while (printf ("\n %s ", prompt) && (read = getline (&line, &n, stdin)) != -1)
    {
        if ((num = atoi (line)))
            break;
        else
            printf ("Invalid Input, Try Again...\n");
    }

    return num;
}

这篇关于scanf函数验证坐着等待另一个输入。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆