c - 从用户那里获取输入 - 不起作用

标签 c function gets

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "mainl.h"

static struct prod_details pd;

char *getinput(char *inp)
{
    printf("Enter the amount of the product %d:\n",pd.no_prod+1);
    gets(inp);
    return inp;
}

void print()
{
    printf("........................\n");
    printf("No of Product is: %d\n",pd.no_prod);
    printf("Grant Total is : %.2f\n",pd.total);
    printf("........................\n");
}

int check(char *str) 
{
    int i;
    if(strlen(str) == 0)
        return 2;
    for(i=0;i<strlen(str);i++)
    {
        if(str[i] == '-')
            return 3;
        if(isalpha(str[i]) != 0)
            return 0;
    }
    return 1;
}

void calc(char *str)
{
    pd.array[pd.no_prod]=atof(str);
    pd.total=pd.total+pd.array[pd.no_prod];
    printf("Total is:%.2f\n",pd.total);
    pd.no_prod++;
}

int main()
{
    int chkflg,i=0,flag=0,cflag=0;
    char ch;
    char input[1024];
    printf("..................\n");
    printf("..CASE  RIGISTER..\n");
    printf("..................\n");
    //strcpy(input,getinput(i+1));
    //printf("%s\n",input);
    do
    {
        strcpy(input,getinput(input));
        chkflg=check(input);
        switch(chkflg)
        {
            case 0:
                printf("Please Enter Correctly...!!!\n");
                printf("You Have entered Wrongly.!!!\n");
                flag=0;
                break;
            case 1:
                calc(input);
                flag=1;
                break;
            case 2:
                printf("You didnt enter anything.!!!\n");
                flag=0;
                break;  
            case 3:
                printf("Coundnot Subtract the Amount..!!!\n");
                flag = 0;
                break;      
        }
        if(flag == 0)
        {
            printf("Do u want to continue(y/n)");
            ch=getchar();
            if(ch == 'y')
            {
                flag=1;
                //continue;
            }
            else if(ch == 'n')
            {
                printf("Thank u..!!!\n");
                break;
            }
            else
            {
                printf("You didn't Enter Properly...!!!\n");
                break;
            }
        }   
    }while(flag == 1);
    print();
    return 0;
}

这是计算账单的程序。该程序对于正确输入(例如double)效果很好。但问题是,如果我们输入错误的字符串,它会显示相应的情况,并询问是否继续。如果我们想继续,它会产生如下输出:

You Have entered Wrongly.!!!
Do u want to continue(y/n)y
Enter the Amount of the product 3:
You didn't enter anything.!!!
Do u want to continue(y/n)

它没有进一步输入。我正在使用 gdb。但我不明白为什么它无法进一步获取输入。请帮我解决这个问题。提前谢谢您。

最佳答案

当您使用getchar时,您在y之后按下的换行符会留在输入流中。然后,当您执行 gets 时,它会读取该换行符,并且您有一个空行。

解决这个问题的一种方法是使用例如scanf 在格式后面加一个空格,因为这会告诉 scanf 吃掉读取字符后的所有空格:

printf("Do you want to continue(y/n)");
scanf("%c ", &ch);

另一个解决方案是使用 fgets 读取整行,并使用例如提取答案sscanf

关于c - 从用户那里获取输入 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14380103/

相关文章:

c++ - 在 z/OS 上替换 C++ 中的 C 套接字 API

函数可以返回 C 中的其他函数吗?

ruby - 如何设置预定义输入

c - for(int i = 0... 的形式是 gnu 扩展吗?

java - 在Java中查看内存中特定位置的字节

c - 用STM32F3读取非常规ADC

来自字符串变量的 Javascript 回调不起作用,尝试了正常的解决方案

javascript - 使用 eval 函数访问数组

c - 为什么使用 fgets() 而不是 gets() 时行为不同

ruby - 使用 "gets"从另一个奇怪的错误运行 ruby​​ 脚本