我想我试图在我的变量中传递错误的数据?里面的新手编码器



我开始学习一些c++的基础知识,我想写一些代码来实践我所学到的。我想做一个类和一些函数。这应该是一个开始文本游戏的标题屏幕,但这里没有游戏……: P

每当我输入1开始时,它就会显示"Good Work",在我按回车后它什么也不做。

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

#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;
}

有许多文体和技术问题。尝试从《权威c++图书指南和列表》中推荐的资源中学习。

#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. Exitn";
            // 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。因此,如果读取数字失败,或者输入错误的数字,循环将再次开始。

应该将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;
}

我对参数和返回值之间的差异感到困惑。当您将函数定义为

int userInput(int x) {
  ...

传递一个值给函数(x),并且使用return语句返回一个值。在你的例子中,你不需要传递参数给你的函数;您需要返回一个值。您可以通过将该值赋值给另一个变量来访问该值:

theResult = userInput(123);

但是不管你传递给函数什么值;你不妨使用

int userInput(void) {
  ...

在这种情况下你可以使用

theResult = userInput();

现在只是让您感到困惑,可以将变量的地址作为参数传递给函数。您可以使用它来访问数据(通常是"较大"的数据块,如数组或结构体),但它也可以用于提供存储返回值的位置。因此

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

将返回指向该位置的数字的平方。然后可以输入

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

将输出16(!)。这是因为该函数会查看地址的内容(&x是变量x的地址),并在适当的位置将其与自身相乘。

最新更新