基本C++应用程序具有额外的输出,然后应该
Basic C++ Application has extra output then it should
所以我正在编写一个基本的应用程序,出于某种原因,当我运行程序时,在我的预期输出之前会弹出一堆数字。它工作正常,直到我添加"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
相关文章:
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 查找素数,然后将质数放入输出文件中
- strtoull() 的输出在转换为双精度然后又转换为 uint64_t 时会失去精度
- 打开一个文件,修改每个字符,然后执行反向操作不输出原始文件
- 如何使用 for 循环将元素加载到我的每个数组中,然后将每个数组的总和输出到一个 sum 数组中
- 更改输出文件外观(C )的方式 - 需要先阅读,然后以不同的格式写入
- 确定邻接矩阵是否具有周期,然后输出该周期
- 基本C++应用程序具有额外的输出,然后应该
- 使用户输入整数字符串,然后输出不同的字符串
- 从文本文件中的值中减去,然后输出计算 (C++)
- 如何扫描 N 个字符串(使用结构),然后反向输出该字符串列表
- 在数组中输入5个元素,检查验证,然后输出C++
- 初始化,然后单行输出
- 从文件读取(.dat)进行计算,然后输出到新文件中
- 创建列和行;询问用户想要输出的行数,然后询问显示
- 使用std::ofstream写入文件的速度比使用std::cout慢,然后将输出重定向到日志文件
- 输出的第一部分是正确的,然后我得到了一个分段错误
- 一次接受多个输入,然后一次给出输出
- 如何使用ffmpegc API对H.264视频进行编码,然后用媒体播放器打开输出