【问题标题】:Creating a simple crackme program in C++ problems with variable and input使用变量和输入在 C++ 问题中创建一个简单的crackme程序
【发布时间】:2020-07-24 20:56:36
【问题描述】:

所以我正在尝试学习 C++,这样我就可以学习一些逆向工程,这就是为什么我要尝试创建这个简单的破解程序来打好基础,而不是在选择自己的道路时采取别人的项目。但是我正在使用 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

【问题讨论】:

  • 最好将问题中的警告和错误作为文本而不是图像包含在内。
  • 注明。已经添加了上面的错误,如果它们是解释性的,你会介意告诉我还是你只是保持原样并否决我的学习问题。
  • 您使用processing() 的意图是什么?函数还是构造函数?对于一个函数,它甚至没有返回类型。
  • 作为一个函数,在编写其他简单程序之前我从来不需要返回任何东西,但这是我的第一个。那么我会返回什么?
  • 建议:首先尝试理解(不是特别是 C++)什么是变量、函数、参数、返回值……然后一切都会变得更容易。

标签: c++ reverse-engineering


【解决方案1】:

一个函数总是有一个返回类型,即使你没有尝试返回任何东西,它也会有void签名。如果您的意图是输入从main 传递的数字并通过类检查器中的函数通过相同的对象显示它,那么它的外观如下:

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

【讨论】:

  • 非常感谢@Anirban166,我明白你的所作所为和意思。以后我学会了,谢谢!
  • @Cyber​​Lable Anytime :) 也不要对这里的社区生气 - 他们只是期望对标记的语言有一定程度的了解,这里的问题有点微不足道,但我相信你会逐渐好转——祝您身体健康!
【解决方案2】:

我已经清理了代码并包含了用作注释的 cmets:

#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。在函数中,参数只是稍后将传递到的参数的占位符值。稍后,当您尝试在 main() 程序中使用 x 作为输入时。您正在调用该函数(使用它)并且需要一个 int 作为输入。

别担心。一开始每个人都会感到困惑(虽然公平地说,随着你的进步,你会发现新的东西令人困惑。它永远不会真正停止)。坚持下去,到时候一切都会变得有意义。

【讨论】:

  • 抱歉,在您的示例中我看不到清理的位置。你读过这个(github.com/isocpp/CppCoreGuidelines/blob/master/…)或这个(codejava.net/coding/…)吗?为什么你使用 std::endl 而不是 '\n' ?为什么using namespace std;?您不应该考虑使用构造函数来初始化number 并将xnumber 进行比较吗?为什么返回int 而不是bool?恐怕这只会让编译器高兴,但不会导致良好的实践......
  • @prog-fh 我清理了代码以使其按程序预期运行。我确实看到了你的观点。不,我以前没有读过这些;谢谢你的链接。如果您有任何其他建议,我很想听听。至于int 而不是bool,这更像是一种习惯的力量;早在我开始研究 C++ 之前,我就已经用 C 编程了。
猜你喜欢
  • 2016-11-12
  • 2011-12-21
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
相关资源
最近更新 更多