2016-09-14 137 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,這可以解釋它們不被顯示。