C程序的外壳开关工作不正常

C程序的外壳开关工作不正常,c,C,我用C语言编写了一个程序,使用switch语句计算并显示上周售出的产品1到5的总零售价值。如果用户输入的数字不是1到5,则系统会提示用户:“不存在此类产品。请输入有效的产品编号。”但是,如果用户输入两位数字,如77,则系统会显示此提示两次。如果输入两位数,如36,则显示一次。代码如下: #include "stdafx.h" #include <stdio.h> #include <iostream> #include <math.h> unsigned i

我用C语言编写了一个程序,使用switch语句计算并显示上周售出的产品1到5的总零售价值。如果用户输入的数字不是1到5,则系统会提示用户:“不存在此类产品。请输入有效的产品编号。”但是,如果用户输入两位数字,如77,则系统会显示此提示两次。如果输入两位数,如36,则显示一次。代码如下:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <math.h>

unsigned int switchfn(); // prototype function for the switch statement

    // initialize global variables
    unsigned int productOne = 0;
    unsigned int productTwo = 0;
    unsigned int productThree = 0;
    unsigned int productFour = 0;
    unsigned int productFive = 0;

// program execution
int main( void )
{   
    // retail price of each product
    double product1 = 2.98;
    double product2 = 4.50;
    double product3 = 9.98;
    double product4 = 4.49;
    double product5 = 6.87;

    // user enters data for each product on each day
    puts("Enter the product number");
    puts("Enter the quantity sold for Monday");
    unsigned int monday = switchfn();
    puts("Enter the the product number and quantity sold for Tuesday:");
    unsigned int tuesday = switchfn();
    puts("Enter the the product number and quantity sold for Wednesday:");
    unsigned int wednesday = switchfn();
    puts("Enter the the product number and quantity sold for Thursday:");
    unsigned int thursday = switchfn();
    puts("Enter the the product number and quantity sold for Friday:");
    unsigned int friday = switchfn();
    puts("Enter the the product number and quantity sold for Saturday:");
    unsigned int saturday = switchfn();
    puts("Enter the the product number and quantity sold for Sunday:");
    unsigned int sunday = switchfn();

    // display total amount of each product sold last week
    puts("\nThe total amount of each product sold is:");
    printf("Product 1: %u\n", productOne);
    printf("Product 2: %u\n", productTwo);
    printf("Product 3: %u\n", productThree);
    printf("Product 4: %u\n", productFour);
    printf("Product 5: %u\n\n", productFive);

    // display total retail cost for each product sold
    puts("\nThe total dollar amount for each product sold is:");
    printf("Product 1: %.2f\n", productOne * product1);
    printf("Product 2: %.2f\n", productTwo * product2);
    printf("Product 3: %.2f\n", productThree * product3);
    printf("Product 4: %.2f\n", productFour * product4);
    printf("Product 5: %.2f\n\n", productFive * product5);

    system("pause");
    return 0;
} // end of program

// switch function 
unsigned int switchfn()
    {
    int productNumber; // product 1 through 5

    // only while product 1 through 5 is being entered.
    while ((productNumber = getchar()) != EOF)  {

        switch (productNumber)  {

        case '1':
            ++productOne;
            break;

        case '2':
            ++productTwo;
            break;

        case '3':
            ++productThree;
            break;

        case '4':
            ++productFour;
            break;

        case '5':
            ++productFive;
            break;

        case '\n':
        case '\t':
        case ' ':
            break;

        default:
            printf("%s", "No such product exists.");
            puts("  Please enter a valid product number.");
            break;
        }
    }
    return 0;
    }
#包括“stdafx.h”
#包括
#包括
#包括
无符号整数开关fn();//switch语句的原型函数
//初始化全局变量
无符号整数productOne=0;
无符号int-productTwo=0;
无符号整数三=0;
无符号int-productFour=0;
无符号整数=0;
//程序执行
内部主(空)
{   
//每种产品的零售价格
双产品1=2.98;
双产品2=4.50;
双产品3=9.98;
双产品4=4.49;
双产品5=6.87;
//用户每天输入每个产品的数据
投入(“输入产品编号”);
看跌期权(“输入周一卖出的数量”);
unsigned int monday=switchfn();
看跌期权(“输入周二的产品编号和销售数量:”);
unsigned int周二=switchfn();
看跌期权(“输入周三的产品编号和销售数量:”);
无符号整数=switchfn();
看跌期权(“输入周四的产品编号和销售数量:”);
无符号整数星期四=switchfn();
卖出(“输入星期五的产品编号和销售数量:”);
无符号整数friday=switchfn();
卖出(“输入周六的产品编号和销售数量:”);
unsigned int saturday=switchfn();
看跌期权(“输入产品编号和周日售出的数量:”);
unsigned int sunday=switchfn();
//显示上周销售的每种产品的总金额
卖出(“\n每种产品的销售总额为:”);
printf(“产品1:%u\n”,productOne);
printf(“产品2:%u\n”,产品二);
printf(“产品3:%u\n”,产品三);
printf(“产品4:%u\n”,产品4);
printf(“产品5:%u\n\n”,产品5);
//显示销售的每种产品的总零售成本
卖出(“\n每种售出产品的总金额为:”);
printf(“产品1:%.2f\n”,productOne*product1);
printf(“产品2:%.2f\n”,产品2*product2);
printf(“产品3:%.2f\n”,产品三*product3);
printf(“产品4:%.2f\n”,产品4*product4);
printf(“产品5:%.2f\n\n”,产品5*product5);
系统(“暂停”);
返回0;
}//节目结束
//开关功能
无符号整数开关fn()
{
int productNumber;//产品1到5
//仅当输入产品1至5时。
而((productNumber=getchar())!=EOF){
开关(产品编号){
案例“1”:
++产品一;
打破
案例“2”:
++产品二;
打破
案例“3”:
++产品三;
打破
案例“4”:
++产品四;
打破
案例“5”:
++产品五;
打破
案例“\n”:
案例'\t':
案例“”:
打破
违约:
printf(“%s”,“不存在此类产品”);
puts(“请输入有效的产品编号”);
打破
}
}
返回0;
}
这是否意味着要知道一个两位数的数字,并且以这种方式,任何大于9的数字都是一个单数,而不是将每个数字作为输入?如果是,我将如何实施?非常感谢您的帮助

您正在使用getch()读取键输入,它一次只读取一次击键。当您输入77时,它被处理为7,7。这两个条目都是无效的,所以您会看到消息两次。输入3、6时,只有6是无效条目,因此只显示一条消息


如果要将多个字符作为一个条目进行处理,则需要更改读取方式,可以使用readln()或其他方法。

与其一次读取一个字符,不如简单地读取一行。在继续执行开关()之前,代码可以对用户输入执行各种错误检查


发布的代码正在逐位读取值,最好是通过
if(1!=scanf(“%d”,&productNumber)){//handle error}
重构代码以使用函数消除复制粘贴,readIn()也是强烈建议的似乎不是我可以使用的库函数。当我用scanf(“%d”和&productNumber)替换代码的getchar()部分时,我得到以下输出:输入产品编号输入星期一的销售量1不存在此类产品。请输入有效的产品编号^Z输入星期二的产品编号和销售数量:1不存在此类产品。请输入有效的产品编号^Z输入星期三的产品编号和销售数量:^Z输入星期四的产品编号和销售数量:2
char buf[80];
if (fgets(buf, sizeof buf, stdin)) return 0;  // Or something else to show EOF
int productNumber = atoi(buf);  // or strtol(), sscanf()

switch (productNumber)  {
    // case '1':  // Use numbers
    case 1:
        ++productOne;
        break; 
   case 2:
        ++productTwo;
        break;

   // case '\n':
   // case '\t':
   // case ' ':