基本C++应用程序具有额外的输出,然后应该

Basic C++ Application has extra output then it should

本文关键字:输出 然后 应用程序 C++ 基本      更新时间:2023-10-16

所以我正在编写一个基本的应用程序,出于某种原因,当我运行程序时,在我的预期输出之前会弹出一堆数字。它工作正常,直到我添加"std::cout"行以使输出仅为小数点后 2 位。该应用程序的一般要点是一个程序,充当商店的自助结账登记,并允许用户购买 2 件商品。是的,我知道代码可能看起来很糟糕,我对C++仍然非常陌生。

#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 个浮点数

std::cout << std::fixed;   // sets a format
std::cout << std::setprecision(2);  // sets a format

设置流输出格式。

线条

std::cout << price1;  // outputs a number
std::cout << price2;  // outputs a number
std::cout << priceofitemplustax1;  // outputs a number
std::cout << priceofitemplustax2;  // outputs a number
std::cout << total;  // outputs a number

将变量打印到流中。

只需删除可变输出行即可。不要接受这个答案 - 功劳归于曼尼66

相关文章: