質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

while

Whileは多くの言語で使われるコントロール構造であり、特定の条件が満たされる限り一連の命令を繰り返し実行します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1520閲覧

設定が2回目以降の入力に反映されない

Shoan

総合スコア13

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

while

Whileは多くの言語で使われるコントロール構造であり、特定の条件が満たされる限り一連の命令を繰り返し実行します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2017/03/26 21:35

編集2017/03/26 21:37

###前提・実現したいこと
ボストン、ロンドン、パリ、東京行きのチケットを売るというプログラムを書きました。
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; }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

forの中でflag=1にしてそれを0に戻していない為なので、

else { flag=0;//とか for (i = 0; i<4; i++)

分かり辛かったですか、すみません。ちょっと範囲を拡大して

else { flag = 0; //こことか*********************************************** 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; } } }

投稿2017/03/26 22:07

編集2017/03/26 22:31
kyunta

総合スコア350

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Shoan

2017/03/26 22:25

すみませんどこの部分に代入したらよろしいのでしょうか;;?
kyunta

2017/03/26 22:31

回答に追加しました。
Shoan

2017/03/26 22:45

大変助かりました! ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問