C - 用getchar()读取意外字符

时间:2013-07-06 12:00:25

标签: c debugging getchar

现在我正在参加C编程课程,所以我完全是关于C的新手。我现在变得头痛,因为我的代码不能按照我认为的方式工作。

这是我的代码,显示问题:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

int main()
{
    printf("\n\nList of Paycodes:\n");
    printf("1 Manager\n");
    printf("2 Hourly worker\n");
    printf("3 Commission Worker\n");
    printf("4 Cook\n\n");

    bool cd=true;
    float weekmoney;
    char name[100];
    char code[10];
    int codeint;
    char cl;
    int cllen;
    char hw[5];
    int hwint;
    int salary=0;

    while(cd){
        printf("Enter employee name: ");
        fgets(name,100,stdin);
        name[strlen(name)-1]='\0'; // nk remove new line lepas user input
        printf("Enter employee\'s paycode: ");
        strcpy(code, "");
        fgets(code, 10, stdin);
        codeint = atoi(code);
        if(codeint > 4 || codeint <= 0){
            printf("\nPlease enter correct employee\'s paycode!\n\n");
            continue;
        }else if(codeint == 1){
            printf("%s\'s pay for this week (RM): 500.00\n\n", name);
        }else if(codeint == 2){
            printf("Enter hours work this week: ");
            fgets(hw, 5, stdin);
            hwint=atoi(hw);
            if(hwint > 12){
                hwint -= 12;
                salary += 500;
            }
            if(hwint > 0){
                for(int i=0;i < hwint;i++){
                    salary += 100;
                }
            }
            printf("%s\'s pay for this week (RM): %d\n\n", name, salary);
        }else if(codeint == 3){
            printf("Enter %s\'s this week sales (RM): ", name);
            scanf("%f",&weekmoney);
            printf("%s\'s pay for this week (RM): %.1f\n", name, (((5.7/100)*weekmoney)+250));
        }
        while(true){
            printf("Do you wish to continue? (Y = Yes, N = No): ");
            cl=getchar();
            getchar();
            if(tolower(cl) == 'y'){
                break;
            }else if(tolower(cl) == 'n'){
                cd=false;
                break;
            }else{
                printf("\nPlease enter correct value!\n\n");
                continue;
            }
        }
    printf("\n");
    }
}

以下是问题和解释。

如果我的代码贯穿本段代码

printf("Enter %s\'s this week sales (RM): ", name);
scanf("%f",&weekmoney);
printf("%s\'s pay for this week (RM): %.1f\n", name, (((5.7/100)*weekmoney)+250));

这里的代码

cl=getchar();
getchar();
if(tolower(cl) == 'y'){
    break;
}else if(tolower(cl) == 'n'){
    cd=false;
    break;
}else{
    printf("\nPlease enter correct value!\n\n");
    continue;
}

会出现错误,但如果我的代码没有运行问题部分,则效果很好。 我试图找到解决方案+调试近一个小时,但仍然没有找到解决我问题的正确解决方案。

2 个答案:

答案 0 :(得分:2)

scanf("%f",&weekmoney);

这里,当您输入一个数字,然后按ENTER键时,scanf将处理该数字,但新行仍在缓冲区中,并由以下getchar处理。您可以使用以下内容来匹配\n

scanf("%f%*[\n]", &weekmoney);

但是,scanf存在许多问题,请尽可能使用其他功能,例如fgets

答案 1 :(得分:0)

put getchar();在scanf

之后删除新的行缓冲区

所以它会像这样

printf("Enter %s\'s this week sales (RM): ", name);
scanf("%f",&weekmoney);
getchar();
printf("%s\'s pay for this week (RM): %.1f\n\n", name, (((5.7/100)*weekmoney)+250));

感谢Yu Hao对缓冲区解释中的新行。 :d