【发布时间】:2020-05-04 16:16:22
【问题描述】:
#include <iostream>
using namespace std;
class movie {
private:
string rating;
public:
string title;
string director;
movie(string aTitle, string aDirector, string aRatings) {
title = aTitle;
director = aDirector;
setrating(aRatings);
};
void setrating(string aRating) {
// this loop checks to see if the rating entered is valid
do
if (aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "18" || aRating == "NR") {
rating = aRating;
}
else {
cout << "rating must be, G, PG, PG-13, 18 or NR \n";
cout << "enter the rating: ";
cin >> aRating;
}
while (aRating != "G" || aRating != "PG" || aRating != "PG-13" || aRating != "18" || aRating != "NR");
}
string getrating() {
return rating;
}
};
int main()
{
movie avengers("The Avengers", "Joss Whedon", "PG-13");
avengers.setrating("dog");
cout << avengers.title << "\n";
cout << avengers.director << "\n";
cout << avengers.getrating() << "\n";
}
我似乎无法让程序退出 while 循环,一旦我输入正确的值,循环就会停止,但程序的其余部分不会继续。如果正确的值已经存在,程序将不显示任何内容。
如果输入的评分不正确,程序应该要求用户输入正确的评分,程序将循环直到变量 = 值之一。然后程序应该显示标题、导演和评级(这最后一部分目前不工作)。
【问题讨论】:
-
听起来你可能需要学习如何使用调试器来单步调试你的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs 和 Debugging Guide
-
我建议通过引用传递比处理器寄存器更大的大型结构,或者如果您不更改参数,则通过
const引用传递。这可以防止编译器复制对象;这也将使您的代码更有效率。
标签: c++ function class visual-studio-2019 do-while