###前提・実現したいこと
ボストン、ロンドン、パリ、東京行きのチケットを売るというプログラムを書きました。
one-wayの価格が
ボストン 350$
ロンドン 600$
パリ 700$
東京 800$
になります。
Round tripの場合はone-wayの1.6を掛けた価格、学割は20%引きという設定です。もしユーザーが上記4地点以外の場所を入力した場合は"No tickets are Available to:(入力された地点)"というメッセージが表示されます。
ユーザーがDoneと入力すると最後に売れたチケットの数と合計金額が表示されます。
###発生している問題・エラーメッセージ
一回目に4地点以外の目的地を入力するとnot availableと表示されるのですが、二回目以降に入力すると表示されなくなってしまいます。
どなたかこの設定が何回目の入力でも反映するように直していただけないでしょうか。よろしくお願いいたします。
************************************************************** Enter the information for each ticket. When no more tickets to enter, enter 'Done' for destination. ************************************************************** Enter the destination city: Tokyo Round trip? [Y/N]y Student discount? [Y/N]n Enter the destination city: Hawaii Enter the destination city: Mexico Enter the destination city:
###該当のソースコード
#include<string> using namespace std; /*For destination and price*/ struct DESTINATION_CITY { double price; string name; }; /*array stracture, 4 for available cities*/ struct DESTINATION_CITY dest[4]; /*variable declaration*/ class travel { int i; int ticket =0; char discount; char roundtrip; double total = 0.0; /*Initialization*/ public: travel() { dest[0].name = "Boston"; dest[0].price = 350.00; dest[1].name = "London"; dest[1].price = 600.00; dest[2].name = "Paris"; dest[2].price = 700.00; dest[3].name = "Tokyo"; dest[3].price = 800.00; } void askDestination() { string name; int flag = 0; cout << "**************************************************************\n"; cout << "Enter the information for each ticket.\n"; cout << "When no more tickets to enter, enter 'Done' for destination.\n"; cout << "**************************************************************\n"; do { cout << "Enter the destination city:\n"; cin >> name; if (name == "Done" || name == "done") break; /*Gets out the loop*/ else { for (i = 0; i<4; i++) { if (name == dest[i].name) { flag = 1; /*When the destinations are above four cities*/ cout << "Round trip? [Y/N]"; cin >> roundtrip; if (roundtrip == 'y') total = total + (1.6*dest[i].price); else total = total + dest[i].price; cout << "Student discount? [Y/N]"; cin >> discount; if (discount == 'y') total = total - (dest[i].price*.20); ticket = ticket + 1; break; } } } if (flag == 0) /*When the destination entered was not above four cities*/ cout << "No tickets are Available to:" << name << "\n"; } while (name != "Done" || name == "done"); } void display() { cout << "-------------------------------------------------------------\n"; cout << ticket << " Tickets sold for a total sum of $" << total << "\n"; cout << "-------------------------------------------------------------\n"; } }; int main() { travel t; t.askDestination(); t.display(); return 0; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/26 22:25
2017/03/26 22:31
2017/03/26 22:45