gpt4 book ai didi

c++ - 基本 C++ 应用程序有额外的输出,那么它应该

转载 作者:行者123 更新时间:2023-11-28 04:57:00 24 4
gpt4 key购买 nike

所以我正在编写一个基本应用程序,出于某种原因,当我运行该程序时,在我的预期输出之前弹出了一堆数字。在我添加“std::cout”行以使输出仅为 2 位小数之前,它工作正常。该应用程序的一般要点是一个程序,它充当商店的自助结帐登记册,并让用户购买 2 件商品。是的,我知道代码可能看起来很糟糕,我对 C++ 还是 super 新手。

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

float price1;
float number1;
float price2;
float number2;
float priceofitemplustax1;
float priceofitemplustax2;
float total;

std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2;
std::cout << total;

cout << endl << "Please scan your first item." <<endl;

cin.get();

cout << endl << "How many of that item are you buying? "<<endl;

cin >> number1;

cout << endl << "How much is that item?"<<endl;

cin >> price1;

priceofitemplustax1 = (number1 * price1) * 1.0875;

cout << endl << "So you want " << number1 << " of this item? Adding tax that will be " << priceofitemplustax1 << "."<<endl;

cin.get();

cout << endl << "Please scan your second item."<<endl;

cin.get();

cout << endl << "How many of that item are you buying? "<<endl;

cin >> number2;

cout << endl << "How much is that item?"<<endl;

cin >> price2;

priceofitemplustax2 = (number2 * price2) * 1.0875;

cout << endl << "So you want " << number2 << " of this item? Adding tax that will be " << priceofitemplustax2 << "."<<endl;

cin.get();

total = priceofitemplustax1 + priceofitemplustax2;

cout << endl << "So your final total for this shopping trip including tax is " << total << "."<<endl;

cin.get();

cout << endl << "Your reciept will print below."<<endl;

cin.get();

cout << setw(14) << right << "Number of Item" << setw(10) << right << "Price" << setw(20) << "Price plus tax" << endl;

cout << setw(14) << right << number1 << setw(10) << right << price1 << setw(20) << priceofitemplustax1 << endl;

cout << setw(14) << right << number2 << setw(10) << right << price2 << setw(20) << priceofitemplustax2 << endl;

cout << endl;

cout << endl;

cout << setw(8) << right << "Total is" << setw(10) << total << price2 << endl;

cin.get();
}

最佳答案

std::cout << std::setprecision(2); 
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2; std::cout << total;

这里你写了5个 float

关于c++ - 基本 C++ 应用程序有额外的输出,那么它应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939939/

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