首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C中打印整数时出现错误的值

在C中打印整数时出现错误的值
EN

Stack Overflow用户
提问于 2020-03-24 16:33:44
回答 1查看 162关注 0票数 0

所以这就是问题:

用c写一个程序,它读取盘子的数量和盘子的成本,玻璃杯的数量和玻璃的成本,以及客户paid.The程序也需要的总成本来显示变化。

这是我的代码:

代码语言:javascript
复制
#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;
    }

我知道这是一个糟糕的代码,但我的问题是(看看代码的注释),当用户输入除整数以外的任何其他内容时,我想打印一个错误。

EN

回答 1

Stack Overflow用户

发布于 2020-03-24 17:01:01

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

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

代码语言:javascript
复制
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);
            }

更新

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

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

而不是

代码语言:javascript
复制
while  (glasses<=-1){
                printf("Wrong value type again!\n");
                printf("how many glasses you bought: ");
                scanf("%f",&glasses);
            }  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60827408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档