Mohd Shahril
Mohd Shahril

Reputation: 2327

C - reading unexpected character with getchar()

Now I'm taking C programming course, so I'm totally newbies about C. I'm now getting headache because my code doesn't work the way I thought it should.

Here is my code that shows problem:

#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");
    }
}

Here is the problem and explanation.

If my code running through this section of code

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));

This code here

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;
}

Will get an error, but if my code doesn't run through the problem section, it works well. I've tried to find solution + debug for nearly an hour but still haven't found the correct solution to solve my problem.

Upvotes: 0

Views: 433

Answers (2)

Mohd Shahril
Mohd Shahril

Reputation: 2327

Put getchar(); remove the new line buffer after scanf

so it will be like this

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));

Thanks Yu Hao for new line in buffer explanation. :D

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122383

scanf("%f",&weekmoney);

Here, when you input a number, and press ENTER, scanf will process the number, but the new line is still in the buffer, and will be processed by the following getchar. You can use the following to match the \n.

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

However, scanf has many problems, use other function like fgets when possible.

Upvotes: 2

Related Questions