无法找到解决方案

时间:2017-03-13 11:47:54

标签: c++ visual-studio procedural-programming

编辑:在对我的代码进行修改后,它可以正常工作。我是C ++的新手,所以我确信它对你们中的某些人来说并不合适,而且你们可能会发现错误或不符合“道德”的事情。如果在此计划中有任何我可以改进的地方,请告诉我。

#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; 
}

1 个答案:

答案 0 :(得分:0)

cin >> beginner, intermediate, advanced;

cin语句中的逗号并不完全符合您的要求。在此表达式中,仅采用cin >> beginner的输入,而完全忽略其他2个变量。

在单个语句中接受多个输入的一种方法是链接它们:

cin >> beginner >> intermediate >> advanced;

或者,您可以在多行上使用cin语句:

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

编辑:根据您的评论,您可以寻找类似的内容:

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个选项之一的所有其他实例中复制此内容。