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

标签 c debugging getchar

现在我正在学习 C 编程类(class),所以我对 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;
}

会出现错误,但如果我的代码没有运行完问题部分,它运行良好。 我尝试了近一个小时的寻找解决方案和调试,但仍然没有找到解决我问题的正确方案。

最佳答案

scanf("%f",&weekmoney);

这里,当你输入一个数字,然后回车,scanf会处理这个数字,但是新的一行还在缓冲区中,会被下面的getchar<处理。您可以使用以下内容来匹配 \n

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

但是,scanf 有很多问题,请尽可能使用其他函数,例如fgets

关于C - 使用 getchar() 读取意外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17502734/

相关文章:

android - 如何维护无限 ScrollView anchor ?

getchar() putchar()相关的C程序设计(新手帮助)

c - 强制用户以特定格式编写输入

c - 如果我在 malloc 结构中声明了一个信号量并且我释放了该结构,我还需要销毁该信号量吗?

使用指针和数组正确调用函数

javascript - 在 Google Chrome 中调试时是否可以更改 javascript 变量值?

javascript - Firebug - 如何在页面加载时以 JS Debug模式启动

c - 为什么这个语句在 while 循环中打印了两次?

c - ASCII 表中不带 "abs"或 "if"的 2 个字符之间的差的绝对值

C: 使用 strtok 解析命令行输入