1

我开始学习 C++ 的一些基础知识,我想编写一些代码来实践我所学的知识。我想创建一个类和一些功能。它应该是开始文本游戏的标题屏幕,除了没有游戏......但是:P

每当我输入 1 开始时,它会显示“Good Work”,在我按 Enter 后它什么也不做。

朝着正确方向的任何一点都会很棒。我一直在看视频和阅读函数教程,它似乎没有涵盖我遇到的问题......

#include <iostream>
#include <string>


using namespace std;

//Function Protos
void keyError();
int userInput(int x);

//class library
class Title
{
    bool nSelect;
    int x;
public:
    void titleScreen()
    {
        while(nSelect)
        {
            cout << "Welcome to Biggs RPG!" << endl << "1. Play 2. Exit" << endl;
            userInput(x);
                if (userInput(1))
                    nSelect = 0;
                else if (userInput(2))
                {
                    cout << "Closing program..." <<endl;
                    nSelect = 0;
                }
                else
                    keyError();
         }
    }
};

int main()
{
Title displayTitle;
displayTitle.titleScreen();

cout << "Good work";
return 0;
}

void keyError()
{
cout << "Meow? Wrong input try again." << endl;
}   

int userInput(int x)
{
x = 0;
cin >> x;
return x;
}
4

3 回答 3

2

存在许多风格和技术问题。尝试从The Definitive C++ Book Guide 和 List中推荐的资源中学习。

这是一个开始……</p>

#include <iostream>
#include <string>

// "using namespace std;" is poor practice. Better to write out std::

/*  Unless you will have two title screens at the same time,
    this should probably be a namespace, not a "singleton" class. */
namespace Title
{
    int nSelect;

    void titleScreen()
    {
        do {
            // prompt for input
            std::cout << "Welcome to Biggs RPG!\n" "1. Play 2. Exit\n";

            // get ready to accept input, even if there was an error before
            if ( ! std::cin ) {
                std::cin.clear(); // tell iostream we're recovering from an error
                std::cin.ignore( 1000, '\n' ); // ignore error-causing input
            }
            // repeat if invalid input
         } while( ! std::cin >> nSelect || ! handleInput( nSelect ) );

不同之处在于你想请求输入,然后处理它。您发布的代码每次检查输入内容时都会再次要求输入

这是一个do … while循环,因此它至少执行一次,然后只要最后的条件为真就重复。如果用户给出无效输入,则! std::cin计算结果为true。然后 C++ 的策略是停止返回任何输入,直到您调用std::cin.clear(),这表明您将再次尝试。ignore然后摆脱无效的输入。然后! std::cin >> nSelect尝试读取一个数字,如果该操作成功,则调用handleInput(您必须编写)false如果输入无效,该调用应该返回。因此,如果读取数字失败,或者输入了错误的数字,则循环再次进行。

于 2013-06-01T02:40:36.340 回答
2

您应该将 userInput 的返回值与 1 或 2 进行比较,如下所示:

int userInput(void);

//class library
class Title
{
    bool nSelect;
    int x;
public:
    void titleScreen()
    {
        nSelect = true;
        while(nSelect)
        {
            cout << "Welcome to Biggs RPG!" << endl << "1. Play 2. Exit" << endl;
            x = userInput();
            if (x == 1)
                nSelect = false;
            else if (x == 2)
            {
                cout << "Closing program..." <<endl;
                nSelect = false;
            }
            else
                keyError();
         }
    }
};

并将 userInput 定义为:

int userInput(void)
{
    int x = 0;
    cin >> x;
    return x;
}
于 2013-06-01T02:36:18.373 回答
0

I sense confusion about the difference between parameters and return values. When you define a function as

int userInput(int x) {
  ...

You pass a value to the function (x) and return a value with the return statement. In your case you don't need to pass a parameter to your function; you need to return a value. You access this value by assigning it to another variable:

theResult = userInput(123);

But it doesn't matter what value you pass to the function; you might as well use

int userInput(void) {
  ...

In which case you can use

theResult = userInput();

Now just to confuse you, it is possible to pass the address of a variable as a parameter to a function. You can use that either to access data (usually a "larger" block of data like an array or struct) but it can also be used to provide a place where a return value is stored. Thus

void squareMe(int *x){
    *x*=*x;
}

Would return the square of the number pointed to in that location. You could then do

int x=4;
squareMe(&x);
cout << x;

Would print out 16 (!). This is because the function looks at the contents of the address (&x is the address of the variable x), and multiplies it by itself in- place.

I hope this explanation helps.

于 2013-06-01T02:56:48.500 回答