使用Bool函数确定End

时间:2012-12-20 03:00:36

标签: c++ boolean

我正在尝试创建一个Play()函数(想像媒体播放器类型的游戏),首先检查以确保给定对象不在其选择的末尾(如果是,用户将是告诉倒带。)

我有一个名为IsCAtEnd()的bool函数。它应该找出C是否在最后。现在我有这样的东西:

bool AC::IsCAtEnd()
{
CAtEnd = (CurrentSelect == NumOfSelect)
if (CAtEnd)
{return true;}
else
{return false;}
}

在我的Play()函数中,我有类似的东西 -

void AC::Play()
{
int Choice;
cout << Enter Selection to Play: “;
cin >> Choice;

CurrentSelection = Choice;
If (IsCAtEnd())
{
cout << “You need to rewind first.” << endl;
}
else
{
cout << “Playing “ << CurrentSelection << endl;
}

它编译并运行但不是我喜欢它。首先,如果我输入最后一个选项(Say C有6个选项,我想播放最后一个选项),它会告诉我倒带。 相反,我希望它能够播放该选择,并且下次调用Play时告诉我倒带(回想起最后一次调用Play时,它是在最后一次选择时)。我还希望它继续提示用户倒回,直到他们这样做(所以不再调用游戏,选择不同的选择并进行选择)。

我知道我需要这样做,以便如果放入最后一个选择,CAtEnd变量在播放后会更改为true。这样,下一次它会说它需要重绕。我不知道怎么做。

2 个答案:

答案 0 :(得分:2)

我想你想先做任何选择。 然后检查它是否是最后一个。如果它是最后一个,则提示您回滚消息,否则不执行任何操作。

你可以像这样改变它

if(currentSelection <= NumberOfSelection)
{
     cout << “Playing “ << CurrentSelection << endl;
}

If (IsCAtEnd())
{
     cout << “You need to rewind first.” << endl;
}

答案 1 :(得分:0)

如果您希望程序在提示倒带信息时连续运行并使用用户输入,则可以使用while循环

    int Choice;    
    cout << Enter Selection to Play: “;
    cin >> Choice;

    while(choice != 0)
    {

         CurrentSelection = Choice;

         if(currentSelection <= NumberofSelection)
             cout << “Playing “ << CurrentSelection << endl;

        if (IsCAtEnd())
        {
            cout << “You need to rewind first.” << endl;
            cout << Enter Selection to Play: “;
            cin >> Choice;
        }
}