使用变量和输入在 C++ 问题中创建一个简单的 crackme 程序

Creating a simple crackme program in C++ problems with variable and input

所以我正在尝试学习 C++,这样我就可以学习一些逆向工程,这就是为什么我要尝试创建这个简单的 crack me 程序来打下基础,而不是在我选择自己的道路时接受别人的项目。但是,我正在使用 CodeBlocks,因为其他 IDE 不合作并且很享受它,并且给了我一些错误和两行。下面是下面的代码。所以有以下错误:

||=== Build: Debug in SimpleProgram (compiler: GNU GCC Compiler) ===| D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In member function 'int checker::processing(int)':| D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|15|warning: no return statement in function returning non-void [-Wreturn-type]| D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In function 'int main()':| D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|22|error: 'x' was not declared in this scope| ||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

    #include <iostream>

using namespace std;

class checker{
public:
    int number;
    processing(int x){
        x = number;
        if ( number == 10 ){
            cout << "Well done!";
        } else {
            cout << "Keep trying!";
        }
    }
};

int main()
{
    checker cracking;
    cout << "Please enter in the correct number";
    cin >> cracking.processing(x);
    return 0;
}

Image of the project and error

函数始终具有 return 类型,即使您不尝试 return 任何它也将具有 void 签名。如果您的意图是输入从 main 传递的数字,并通过相同对象的 class 检查器中的函数显示它,则它看起来像这样:

#include <iostream>
using namespace std;

class checker{
public:
    int number;
    void processing(int x)
    {
        if (x==10)
            cout << "Well done!";
        else 
            cout << "Keep trying!";
    }
};
int main()
{
    checker cracking;
    cout << "Please enter in the correct number \n";
    int n;
    cin >> n;
    cracking.processing(n);
    return 0;
}

我整理了代码并添加了注释作为注释:

#include <iostream>

using namespace std;

class checker{
public:
    void setnumber(int i){  //it's a good habit to put variables in private and access them with a public function
        this->number = i;
    };
    int processing(int x){  //x is just a placeholder value for whatever you put in here. You can't use it in the rest of the program
        if ( x == 10 ){
            cout << "Well done!" << endl;
            return 1;   //this condition indicates success
        } else {
            cout << "Keep trying!" << endl; //the endline just makes it so you aren't typing on the same line as the message
            return 0;   //this condition indicates success hasn't been reached yet
        }
    }
private:
    int number;
};

int main()
{
    checker cracking;
    cracking.setnumber(10);     //the number is set to 10

    int i, l;   //i is the guess number, l is a condition for the loop
    cout << "Please enter in the correct number" << endl;
    do{     //this loop sets it up so that you can have multiple tries
        cin >> i;
        l = cracking.processing(i);
    }while(l!=1);   //and this condition (the return value of processing(), breaks the loop on success
    return 0;
}

我突然想到的主要问题是 x 的使用。

正在尝试将 x 设置为 number。在函数中,参数只是稍后将传递给参数的占位符值。然后稍后当您尝试使用 x 作为 main() 程序中的输入时。您正在调用该函数(使用它)并需要一个 int 作为输入。

别担心。每个人一开始都会感到困惑(尽管公平地说,随着你的进步,你只会发现新的东西让你感到困惑。它永远不会真正停止)。坚持下去,一切都会及时发生。