5

このコードの何が問題なのかわかりません。

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


#define MAX 100
#define TRUE 1
#define FALSE 0

char sect_cat;
char customer_name[MAX];
char customer_number[MAX];      /* error handling is easier */

int prev_unit = 0;
int current_unit = 0;
int consumed = 0;
int set = FALSE;

float init_bill;
float tax;
float total_bill;


    void get_userinfo()
    {

            printf("Enter sector category: ");
            scanf("%c", &sect_cat);
        printf("Enter customer name: ");
        fflush(stdin);
        scanf("%sn", &customer_name);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter customer number: ");
            fflush(stdin);
            scanf("%s", customer_number);

            int i;
            int error;
            for (i=0, error=0; i<strlen(customer_number); i++)
            {
                if (isdigit(customer_number[i]))
                {
                }
                else
                {
                    error = 1;
                }
            }
            if (error == 0)
            {
                set = TRUE;
            }
            else
                printf("ERROR: Only numbers are allowed\n");
        }

        printf("Enter previous unit: ");
        fflush(stdin);
        scanf("%d", &prev_unit);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter current unit: ");
            fflush(stdin);
            scanf("%d", &current_unit);

            if (prev_unit > current_unit)
            {
                printf("ERROR: Current unit must be larger than previous unit\n");
            }
            else
                set = TRUE;
        }
        consumed = current_unit - prev_unit;
    }



int main()
{


/* Introduce program to users */

        printf("\nThis program computes your electric bill based on these sector categories\n\n");

    printf("\tResidential(R)\n");
    printf("\tIndustrial(I)\n");
    printf("\tCommercial(C)\n\n");

    printf("Press any key to continue...");
    fflush(stdin);
    getchar();  
#################### 編集

templatetypedef のソリューションを適用すると、プログラムは customer_name のユーザー入力を待機します。ただし、スペースを含む文字列を入力するとエラーになり、プログラムはスペースの後の単語が次のプロンプトで入力されたと見なします。

Enter sector category: r
Enter customer name: George of the Jungle
Enter customer number: ERROR: Only numbers are allowed
Enter customer number: ERROR: Only numbers are allowed
Enter customer number:
4

3 に答える 3

9

このfflush関数は、入力ストリームからデータをフラッシュしません。代わりに、出力ストリームにバッファリングされたデータを宛先にプッシュするために使用されます。これはここに文書化されています。この以前の SO questionに見られるように、使用しようとするfflush(stdin)と未定義の動作が発生するため、回避することをお勧めします。

ユーザーが文字の入力を終了したときに入力された改行文字から改行を削除したい場合は、代わりに次のことを検討してください。

scanf("%c%*c", &sect_cat);

これは、改行を に残すのではなく、食べますstdin

お役に立てれば!

于 2012-02-03T01:32:19.103 に答える
0

I think that you meant to write fflush(stdout) instead of fflush(stdin).

于 2012-02-03T01:31:53.503 に答える
-1

fflush出力ストリームで動作するはずです。こちらのドキュメントを参照してください

于 2012-02-03T01:37:25.880 に答える