gpt4 book ai didi

c++ - 为什么 int plus float 显示奇​​怪的数字?

转载 作者:行者123 更新时间:2023-11-28 01:25:02 27 4
gpt4 key购买 nike

我是 C++ 的新手,我正在努力完成我的第一个作业。我不需要决定,但我想知道我做错了什么?这是我的代码:

#include <iostream>
using namespace std;

int main(){

int chTenders;
int frOrders;
int macCh;
int drinks;
int sauces;
float tip;
float subtotal;
subtotal = chTenders + frOrders + macCh + drinks + sauces + tip;

cout << "How many chicken tenders would you like?\n";
cin >> chTenders;
cout << "How many orders of fries would you like?\n";
cin >> frOrders;
cout << "How many orders of mac and cheese would you like?\n";
cin >> macCh;
cout << "How many drinks would you like?\n";
cin >> drinks;
cout << "How many sauces would you like?\n";
cin >> sauces;
cout << "How much would you like to tip?\n";
cin >> tip;

cout << "====Slim's Order====\n";
cout << "Subtotal $" << subtotal << endl;


return 0;
}

当我编译并运行它时,它给了我 $2.60929e+08 而不是 6.0:

How many chicken tenders would you like?
1
How many orders of fries would you like?
1
How many orders of mac and cheese would you like?
1
How many drinks would you like?
1
How many sauces would you like?
1
How much would you like to tip?
1
====Slim's Order====
Subtotal $2.60929e+08

最佳答案

我认为你的计算顺序刚刚好。 subtotal在您将输入值接收到加数( chTendersfrOrders 等)之前计算。移动 subtotal=...行到 cin << tip 之后声明。

关于c++ - 为什么 int plus float 显示奇​​怪的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54273046/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com