繁体   English   中英

在 C 中打印整数的错误值

[英]Wrong value on print an integer in C

所以这就是问题所在:

在c中编写一个程序,读取盘子的数量和盘子的成本,眼镜的数量和眼镜的成本以及客户支付的总成本。该程序还需要显示变化。

这是我的代码:

#include <stdio.h>
    int main(){
        float change,total_money;
        float glasses,plates;
        float cost_plates,cost_glasses,total_cost;
        printf("how many glasses you bought: ");//only intenger
        scanf("%f",&glasses);
            while  (glasses<=-1){
                printf("Wrong value type again!\n");
                printf("how many glasses you bought: ");
                scanf("%f",&glasses);
            }   
        printf("how much each glass cost: ");
        scanf("%f",&cost_glasses);
            while  (cost_glasses<=0) {
                    if(cost_glasses==0)
                {
                    printf("Free? ok!\n");
                }
                break;
                printf("Wrong value type again!\n");
                printf("how much each glass cost: ");
                scanf("%f",&cost_glasses);
            }   
        printf("How many plates you bought: "); //only integer
        scanf("%f",&plates);
         while  (plates<=-1) {
            printf("Wrong value type again!\n");
            printf("How many plates you bought: ");
            scanf("%f",&plates);
        }
        printf("how much each plate cost: ");
        scanf("%f",&cost_plates);
                while  (cost_plates<=0) {
                    if(cost_plates==0)
                {
                printf("Free ok!\n");
                }
                break;
                printf("Wrong value type again!\n");
                printf("how much each plate cost: ");
                scanf("%f",&cost_plates);
            }
        total_cost= (cost_glasses * glasses) + (cost_plates * plates);
        printf("how much money you gave:");
        scanf("%f",&total_money);
        while(total_money<=-1) {
            printf("Wrong value type again!\n");
            printf("how much money you gave:");
            scanf("%f",&total_money);
        }
        while(total_money<total_cost) {
            printf("Thief!!! Type again!\n");
            printf("how much money you gave:");
            scanf("%f",&total_money);
        }
        printf("You paid %.2f $ total, the plates and the glasses were costumed %.2f $ so your change was %.2f $",total_money,total_cost,total_money-total_cost);
        return 0;
    }

我知道这是一个错误的代码,但我的问题是(查看代码的注释)当用户键入任何其他期望整数时我想打印错误。

老实说,我没有完全理解你的问题。 如果用户没有回答“您购买了多少眼镜(或盘子)” ,那么他/她如何以及在哪里确定他/她购买的盘子的数量?

我确定一件事。 break语句后不执行任何语句和命令。 也许你的意思是

while  (cost_glasses<=0){
        if(cost_glasses==0)
                {
                printf("Free? ok!\n");
                break;
                }
                printf("Wrong value type again!\n");
                printf("how much each glass cost: ");
                scanf("%f",&cost_glasses);
            }

更新

要过滤掉板数的负值或十进制值,请使用

while  (glasses<=-1 || int(glasses)!=glasses){
                printf("Wrong value type again!\n");
                printf("how many glasses you bought: ");
                scanf("%f",&glasses);
            }  

代替

while  (glasses<=-1){
                printf("Wrong value type again!\n");
                printf("how many glasses you bought: ");
                scanf("%f",&glasses);
            }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM