我正在学习 C++
。作为一个家庭作业,我开始尝试分支......但我并没有完全了解它...这是我试图执行的代码(如果我犯了很大的错误,请耐心等待我。 。)
#include <iostream>
using namespace std;
int main () {
int age;
char * yes;
char * no;
bool Rated = Rated ? yes : no;
int ticketPrice = 5;
int discountPrice = ticketPrice - (ticketPrice * 0.1);
int matineePrice = (ticketPrice * 0.5);
int hour = 8 <= hour <= 24;
cout << "Is the movie R_rated? \n";
cin >> yes or no;
cout << "How old are you?";
cin >> age;
if (age < 0 or age >100) {
cout << "Not a valid age!";
}
else if ((age <= 13) and (Rated = yes)) {
cout << "You must be accompanied by a Janitor";
}
else if (((age > 13) and ((Rated = yes) or (Rated = no)))
or ((age <=13) and (Rated = yes))) {
cout << "What time do you want the ticket for?";
cin >> hour;
if (hour < 8 or hour > 24) {
cout << "Not a valid hour!";
}
else if (hour < 18) {
if (age <= 5) {
cout << "The entrance is free";
}
else if (age >= 55) {
cout << "Matinee Ticket price is "<<
matineePrice;
}
else if (5 < age < 55) {
cout << "Matinee ticket price is " << matineePrice;
}
}
else if (hour >= 18) {
if (age <= 5) {
cout << "The entrance is free";
}
else if (5 < age <= 55) {
cout << "Ticket price is " << ticketPrice;
}
else if (age > 55) {
cout << "You're eligibile for a 10% "
"discount \n";
cout << "Ticket price is " << discountPrice;
}
}
}
}
输出(我回答没有, 67 , 20 )我应该得到discountedPrice
代替ticketPrice
值...
Is the movie R_rated?
no
How old are you?67
What time do you want the ticket for?20
Ticket price is 5
任何建议,链接或教程帮助都会非常感激......
答案 0 :(得分:3)
您的代码存在很多问题。我建议你读一本关于C ++的好书并从中学习。如果你已经在使用一本书,那很可能是不好的。
以下是一些事情:
char*
不适合用于字符串。您应该使用std::string
类。
围绕Rated
的整个代码与C ++几乎没有相似之处。
=
是赋值运算符。它不能用于比较事物的平等;这就是==
的用途。
答案 1 :(得分:2)
首先,摆脱没有意义的yes
和no
,并将输入读入字符串变量:
string Rated;
cin >> Rated;
然后使用它,请记住使用==
而不是=
进行比较:
if (Rated == "yes") {/*whatever*/}
或者,使用布尔变量:
string yes_or_no;
cin >> yes_or_no;
bool Rated = yes_or_no == "yes";
if (Rated) {/*whatever*/}
另外,这个:
8 <= hour <= 24
不符合您的想法。您需要进行两次单独的比较:
8 <= hour and hour <= 24
虽然,在这种情况下,你根本不需要它 - 用这个来初始化hour
是没有意义的。您正在阅读hour
的值并稍后检查其范围,并且无需在此处初始化。
可能有更多问题,但这应该让你开始。我希望在我超过100岁的时候我仍然可以去看电影。
答案 2 :(得分:2)
以下代码已修复。我试图准确解释代码的作用。
// Declare the input/output streams, including standard streams like std::cin and std::cout.
#include <iostream>
// Declare the std::string class - it's C++, we should not use C strings!
#include <string>
// Instead of using the entire std namespace, we'll only use the things that come up often.
// This saves some typing, but is safe. Otherwise, who knows what name may clash with something
// in the vast std namespace.
using std::cin;
using std::cout;
using std::endl;
int main() {
bool ok; // Whether the most recent answer to a question is valid
bool rated; // Whether the movie is R-rated
int age; // Customer's age
int hour; // Hour of the showing
const int ticketPrice = 5;
const int discountPrice = ticketPrice * (1.0 - 0.9);
const int matineePrice = ticketPrice * 0.5;
// Gather Inputs
do {
std::string answer; // Holds the answer to the yes/no question
cout << "Is the movie R-rated (y/n)? ";
cin >> answer;
if (answer.length() > 0) {
// If the answer is not empty, we can uppercase the first letter.
// This way we don't have to check for lowercase answers.
answer[0] = toupper(answer[0]);
}
// The answer is valid when it's non-empty and when it begins with either Y/y or N/n
ok = answer.length() > 0 and (answer[0] == 'Y' or answer [0] == 'N');
if (not ok) {
cout << "That's not a valid answer." << endl;
} else {
// The answer is valid, so we can set the rated variable.
rated = answer[0] == 'Y';
}
} while (not ok); // Repeat the question while the answer is invalid
do {
cout << "How old are you? ";
cin >> age;
// The answer is valid when it's between 0 and 150, inclusive.
ok = age >= 0 and age <= 150;
if (not ok) {
cout << "That's not a valid age!" << endl;
}
} while (not ok);
do {
cout << "What hour do you want the ticket for? ";
cin >> hour;
// The hour 0 is mapped to 24.
if (hour == 0) hour = 24;
// The answer is valid when it's between 8 and 24, inclusive.
ok = hour >= 8 and hour <= 24;
if (not ok) {
cout << "That's not a valid hour!";
}
} while (not ok);
// Output the Messages
if (rated and age <= 13) {
cout << "You must be accompanied by a Janitor" << endl;
}
if (age <= 5) {
cout << "The entrance is free" << endl;
}
else if (hour < 18) {
cout << "Matinee ticket price is " << matineePrice << endl;
}
else {
if (age <= 55) {
cout << "Ticket price is " << ticketPrice << endl;
}
else {
cout << "You're eligibile for a 10% discount." << endl;
cout << "Ticket price is " << discountPrice << endl;
}
}
}
答案 3 :(得分:0)
首先:
char * yes;
char * no;
// ...
cin >> yes or no;
没有任何编码意义。
答案 4 :(得分:0)
通常,“是”和“否”不是c ++中的关键字。 “真实”和“虚假”都是。
好吧,有些事情: 1) cin&gt;&gt;是或否;
应该是:
cin >> Rated;
因为变量名不能包含空格,因为你编写它时编译器会写“cin应该把东西放进去,但我无法弄清楚该怎么做'或'和'不'。 “
2)
else if ((age <= 13) and (Rated = yes))
永远不会奏效。我建议重写将结果存储在字符串(std :: string)中,然后根据该值设置。
std::string rated_str;
cin >>rated_str;
if(rated_str == "yes") {
rated = true;
} else {
rated = false;
}
然后在if if use:
中if(rated)
或 if(rating == true)
3)在完全声明变量之前,你不能引用变量:
bool Rated = Rated ? yes : no;
答案 5 :(得分:0)
行:
bool Rated = Rated ? yes : no;
cin >> yes or no;
以及所有
(Rated = yes)
(Rated = no)
毫无意义。
首先,如果Rated
是bool
,那么您只能为其分配true
或false
(不是0
或{{的所有内容1}}以及编译器接受的内容将转换为NULL
)。
最佳质量代码应使用枚举类型进行评级。
阅读时,您应该阅读一些变量并检查其类型。 小修补程序:
true
答案 6 :(得分:0)
对于折扣票使用: double discountPrice = 0,9 * ticketPrice;
您将输入小时并测试它们,因此您只能写: int hour;
答案 7 :(得分:0)
else if
声明中犯了错误。else if
条件检查程序。or
关键字...实际上这是您在此遇到的真正问题,您只需要替换 {{1} or
。 and
这将为您提供折扣价
else if (5 < age <= 55) {
cout << "Ticket price is " << ticketPrice;
}