【问题标题】:c++ program doesn't continue on after loopc ++程序在循环后不会继续
【发布时间】: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 programsDebugging Guide
  • 我建议通过引用传递比处理器寄存器更大的大型结构,或者如果您不更改参数,则通过const 引用传递。这可以防止编译器复制对象;这也将使您的代码更有效率。

标签: c++ function class visual-studio-2019 do-while


【解决方案1】:

你的逻辑条件不正确:

while (aRating != "G" || aRating != "PG" || aRating != "PG-13" || aRating != "18" || aRating != "NR");

写出一些例子,你会发现这个条件总是成立的。 (例如,aRating 的任何值将始终为 not Gnot PG

您需要将所有||s 替换为&amp;&amp;s。

此外,您的 do-while 循环存在缺陷,因为您不会在需要时分配给 rating。相反,您可以像这样重组它:

do 
{
   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");

// now just assign
rating = aRating;

这也避免了必须两次拼写约束。

【讨论】:

    【解决方案2】:

    您需要在该行中使用&amp;&amp; 而不是||

    while (aRating != "G" || aRating != "PG" || aRating != "PG-13" || aRating != "18" || aRating != "NR");
    

    更好的替代方案是将“评级有效”的逻辑放在它自己的函数中,并在if 语句和do while 语句中使用它。

    bool isValidRating(strting const& aRating)
    {
       return (aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "18" || aRating == "NR");
    }
    
    void setrating(string aRating)
    {
       // this loop checks to see if the rating entered is valid
       do
       {
          if ( isValidRating(aRating) )
          {
    
             rating = aRating;
          }
          else
          {
             cout << "rating must be, G, PG, PG-13, 18 or NR \n";
             cout << "enter the rating: ";
             cin >> aRating;
          }
    
       } while ( !isValidRating(aRating) );
    }
    

    第二个函数可以简化为:

    void setrating(string aRating)
    {
       while  ( !isValidRating(aRating) )
       {
          cout << "rating must be, G, PG, PG-13, 18 or NR \n";
          cout << "enter the rating: ";
          cin >> aRating;
       }
    
       rating = aRating;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多