2017-05-14 14 views
1

私はかなり新しいC++です。ユーザーがチケットを購入するために使うことができるかなり基本的なプログラムを行うための割り当てが与えられていますが、計算に問題があります。計算が正しく行われていない

これまでのコードです。

#include <iostream> 

using namespace std; 

int main() 
{ 
double type_ticket, num_tickets, price1, price2, price3, total_price, decision; 

cout << "Welcome to the ticket kiosk."; 
cout << "\n"; 
cout << "\n"; 
cout << "1. VVIP - RM 200"; 
cout << "\n"; 
cout << "2. VIP - RM 150"; 
cout << "\n"; 
cout << "3. Normal - RM 100" << endl; 
cout << "\n"; 


do 
{ 
cout << "Please select the category of ticket you would like to purchase: "; 
cin >> type_ticket; 
cout << "\n"; 


if (type_ticket == 1) 
{ 
cout << "How many would you like: "; 
cin >> num_tickets; 
cout << "\n"; 
price1 = num_tickets * 200; 
cout << "The price is: RM " << price1 << endl; 
cout << "\n"; 
cout << "\n"; 
cout << "1. YES" << endl; 
cout << "2. NO" << endl; 
cout << "\n"; 
cout << "Would you like to continue purchasing more tickets: "; 
cin >> decision; 
cout << "\n"; 
} 


else if (type_ticket == 2) 
{ 
cout << "How many would you like: "; 
cin >> num_tickets; 
cout << "\n"; 
price2 = num_tickets * 150; 
cout << "The price is: RM " << price2 << endl; 
cout << "\n"; 
cout << "\n"; 
cout << "1. YES" << endl; 
cout << "2. NO" << endl; 
cout << "\n"; 
cout << "Would you like to continue purchasing more tickets: "; 
cin >> decision; 
cout << "\n"; 
} 


else if (type_ticket == 3) 
{ 
cout << "How many would you like: "; 
cin >> num_tickets; 
cout << "\n"; 
price3 = num_tickets * 100; 
cout << "The price is: RM " << price3 << endl; 
cout << "\n"; 
cout << "\n"; 
cout << "1. YES" << endl; 
cout << "2. NO" << endl; 
cout << "\n"; 
cout << "Would you like to continue purchasing more tickets: "; 
cin >> decision; 
cout << "\n"; 
} 


else 
{ 
cout << "You have entered an invalid input, please try again. " << endl; 
cout << "\n"; 
} 


} 
while (decision == 1); 

total_price = price1 + price2 + price3; 
cout << "The grand total is: RM " << total_price << endl; 
cout << "\n"; 
cout << "Thank you for using this service today, we hope you enjoy the show." << endl; 
cout << "\n"; 

} 

私が午前問題は、ユーザーがVVIPおよび/またはVIPからチケットを購入する際、TOTAL_PRICEための計算が正しく行われていないです。価格3が入力された場合、計算は正常に動作します。

ユーザーはvvipおよび/またはvip =計算が正しく行われていません。 ユーザーは通常のvvipおよび/またはvip =計算が正しく行われます。

ご協力いただければ幸いです。

参考までに、このコードはまだ完成していませんが、今のところこれが私の持っているものです。

+0

「右行われていない」の意味が何であるかを説明してください。入力、出力、期待出力とは何ですか? – user463035818

+0

あなたはSOのために新しい人ですから、他のユーザーの時間を感謝してください。少なくともあなたの質問にコードを適切に書式設定し、具体的な問題を説明します( '正しく動作しません'は適切な説明ではありません)。あなたの入力と出力。 – Alex

+0

私はちょうどあなたのプログラムを実行し、あなたが提供しているものからうまくいくようです。タイプ2の3枚のチケットをリクエストし、合計費用は450でした。これがどのように機能していないかについて詳しく調べてください。 –

答えて

2

あなたはの計算の前にpriceNN1, 2, 3のいずれかです)変数を初期化していないように見える:

チケットの一種類のみの場合は
total_price = price1 + price2 + price3; 

、その変数はゴミが含まれているため、結果は予測できません。あなたがで始まる必要があり

double price1 = 0; 
double price2 = 0; 
double price3 = 0; 
+0

これは本当に感謝してくれてありがとう、私は実際にこれをやろうとしましたが、二重に3回宣言することはありませんでした。ダブルtype_ticket、num_tickets、price1 = 0、price2 = 0、price3 = 0、total_price、decision;私はそれが動作しないようにそれをやっていると仮定していますか? –

+0

それは宣言ではなく、ここの鍵である初期設定です。複数行の宣言は文体的な選択ですが、ポイントは0へのデフォルトの初期化がないので、自分でそれを行う必要があります。確かにそれは –

+0

だから私は、これらの2つのコードの間に、例えば、他の部分でpriceN = 0を追加した場合 cout << "" \ n "; price3 = num_tickets * 100;それはまだ動作するのだろうか? 例: cout << "\ n"; price3 = 0; price3 = num_tickets * 100; –

関連する問題