2016-09-14 141 views
-3

我已经编了几天,现在决定开展一项练习。一个计算五个项目的总数并计算税额。 在这个特别的练习中,我已经设法显示物品的名称和价格,但是销售税和总额没有被显示出来。我知道这是一个非常基本的问题,我一直在努力学习,通过反复试验熟悉我自己。就计算部分而言,任何人都可以确定我错过了什么?感谢提前。计算销售税和总计

#include <iostream> 
using namespace std; 

float tax = 0.07; 
string item1,item2,item3,item4,item5; 
float price1,price2,price3,price4,price5; 

int main() 
{ 
cout<<" please enter the name of the first item \n"; 
cin>> item1; 

cout<<" please enter the name of second item \n"; 
cin>> item2; 

cout<<" plrease enter the name of the third item \n"; 
cin>> item3; 

cout<<" please enter the name of the fourth item \n"; 
cin>> item4; 

cout<<" please enter the name of the fifth item \n"; 
cin>> item5; 

cout<<" please enter the price for the first item \n"; 
cin>> price1; 

cout<<" please enter the price for the second item \n"; 
cin>> price2; 

cout<<" please enter the price for the third item \n"; 
cin>> price3; 

cout<<" please enter the price for the fourth item \n"; 
cin>> price4; 

cout<<" please enter the price for the fifth item \n"; 
cin>> price5; 

float subtotal = 0; 
float saletax = 0; 
float grandtotal = 0; 

subtotal = price1 + price2 + price3 + price4 + price5; 
saletax = subtotal * tax; 
grandtotal = subtotal + saletax; 

cout<<"my shopping list \n"; 
cout<<"================\n"; 

cout<<item1<<"  "<<"$"<<price1 <<endl; 
cout<<item2<<"  "<<"$"<<price2 <<endl; 
cout<<item3<<"  "<<"$"<<price3 <<endl; 
cout<<item4<<"  "<<"$"<<price4 <<endl; 
cout<<item5<<"  "<<"$"<<price5 <<endl; 
+1

究竟是什么问题?你是否得到错误的输出?一个错误? – Mureinik

+3

你没有复制你所有的'main()',所以它不可能解释缺失部分的问题。 – drescherjm

+0

我没有收到任何错误。只是它没有给我总数。因此,当我得到五个物品及其价格时,价格将出现在名称旁边,但不是总计,也不是销售税。 – ReMaKe

回答

2

它没有被显示,因为你永远不会打印它。

将这些行添加到主函数的末尾,您将看到它。

cout << "Total: " << grandtotal << endl; 
cout << "Tax: " << saletax << endl; 
+0

感谢您的帮助,但是当我添加该行时,它显示“总计”和“税”,但实际上没有计算它们,但是,查看您编写这些代码的方式时,我只是添加了“cout << “小计:”“价格1 +价格2 +价格3 +价格4 +价格5;'但是非常感谢你 – ReMaKe

+0

@ReMaKe除了你知道你在代码中添加了什么,当你没有发布整个东西时,我们不可能帮助'main()',特别是当问题出现在未发布的部分时。 – drescherjm

+0

***它显示“总计”和“税”,但实际上并没有计算它们***您发布的'main()'部分计算这些! – drescherjm

2

我只看到cout陈述五个价格,而不是saletaxgrandtotal,这可以解释它们不被显示。