2017-03-13 6 views
-2

編集:私のコードにリビジョンを作成した後、それは正常に動作します。私はC++を初めて使っているので、あなたの一部が正しく見えず、エラーや倫理的でないものが見つかるかもしれないと確信しています。このプログラムで改善できることがあれば教えてください。このcin文のソリューションを見つけることができません

#include <iostream> 
#include <string> 
#include <cstdlib> 

using namespace std; 

int main() { 

// CUSTOMER TYPE 
string customertype; 
string classtype; 
string difficultytype; 

// CLASS TYPE 
string ballet; 
string salsa; 
string bollywood; 

// DIFFICULTY TYPE 
float beginner = 0; 
float intermediate = 0; 
float advanced = 0; 

// CUSTOMER TYPE INPUT 
cout << "Enter Customer Type: "; 
cin >> customertype; 

if (customertype == "concession") { 

} else if (customertype == "child") { 

} else if (customertype == "adult") { 

} else 
cout << "\n\tInvalid Choice" << "\n" << endl; 

// CLASS TYPE INPUT 
cout << "Enter Class Type: "; 
cin >> classtype; 

if (classtype == "ballet") { 

} else if (classtype == "salsa") { 

} else if (classtype == "bollywood") { 

} else 
cout << "\n\tInvalid Choice" << "\n" << endl; 

// DIFFICULTY TYPE INPUT 
cout << "Enter Difficulty Level: "; 
cin >> difficultytype; 

if (difficultytype == "beginner") { 

} else if (difficultytype == "intermediate") { 

} else if (difficultytype == "advanced") { 

} else 
cout << "\n\tInvalid Choice" << "\n" << endl; 

// CALCULATION 
float totalprice = 0; 

if ((customertype == "concession") && (difficultytype == "beginner" && "intermediate" && "advanced")) { 
cout << "\n\tTotal Price: 2.50" << "\n" << endl; 

} else if ((customertype == "child") && (difficultytype == "beginner")) { 
cout << "\n\tTotal Price: 2.50" << "\n" << endl; 

} else if ((customertype == "child") && (difficultytype == "intermediate")) { cout << "\n\tTotal Price: 3.50" << "\n" << endl; 

} else if ((customertype == "child") && (difficultytype == "advanced")) { 
cout << "\n\tTotal Price: 4.00" << "\n" << endl; 

} else if ((customertype == "adult") && (difficultytype == "beginner")) { 
cout << "\n\tTotal Price: 4.00" << "\n" << endl; 

} else if ((customertype == "adult") && (difficultytype == "intermediate")) { cout << "\n\tTotal Price: 5.00" << "\n" << endl; 

} else if ((customertype == "adult") && (difficultytype == "advanced")) { 
cout << "\n\tTotal Price: 5.50" << "\n" << endl; 

} else 
cout << "\tInvalid Choice" "\n" << "\n" << endl; 

cout << "\n\tCustomer Type: " << customertype << "\n" << endl; 
cout << "\n\tClass Type: " << classtype << "\n" << endl; 
cout << "\n\tDifficulty Type: " << difficultytype << "\n" << endl; 

system("pause"); 
return 0; 
} 
+4

あなたは[良いC++の本](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)を使うことができるような音がします。 'cin >>譲歩、子供、大人;'あなたが思っていることはしません。 – NathanOliver

+0

@ NathanOliver私はそれが譲歩、子供、大人のどちらかとしてのインプットを取るという印象を受けていますか?私が間違っているなら、私を修正してください。 – FutureProgrammer

+0

いいえ、カンマ演算子です。この場合、 '' cin >> concession''だけが何かをするものです。 '、子、大人;'は基本的に無視されます。参照:https://en.wikipedia.org/wiki/Comma_o​​perator – NathanOliver

答えて

0
cin >> beginner, intermediate, advanced; 

カンマを使用すると、彼らが何をしたいのかを正確に行いません。この式では、cin >> beginnerの入力のみが使用され、その他の2つの変数は完全に無視されます。単一のステートメントで複数の入力を受け入れるように

一つの方法は、それらを連結することである:

cin >> beginner; 
cin >> intermediate; 
cin >> advanced; 

EDIT:

cin >> beginner >> intermediate >> advanced; 

また、あなたは複数行にCIN文を持つことができます。あなたのコメントに基づいて、あなたは次のようなものを探します:

string choice; 
cout << "Enter your choice here: "; 
cin >> choice // no need to use getline() as you only need one word as input 

if (choice == "beginner) 
    // do stuff 
else if (choice == "intermediate") 
    // do stuff 
else if (choice == "advanced") 
    // do stuff 
else 
    cout << "Invalid choice"; 

3つのオプションのいずれかを選択するようにユーザーに依頼する他のすべてのインスタンスに対して、これを複製する必要があります。

+0

このプログラムでは間違っている3つの入力をユーザーから聞いてきます。私はプログラムが3つのタイプの入力に基づいて答えを定義するようにしたい。例えば、ユーザが「子供」を入力すると、各難易度オプションは、1.50だけ低減されたコストを有する。 – FutureProgrammer

+0

@FutureProgrammerプログラムの入力はいくつありますか? –

+0

なぜ、あなたのcin文で3つの変数を使用していますか? –

関連する問題