Я очистил код и включил комментарии, которые служат примечаниями:
#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.
Попытка установить от х до number
. В функциях параметры являются просто значениями-заполнителями для аргументов, которые будут переданы позже. Затем, когда вы попытались использовать x в качестве входа в программе main()
. Вы вызывали эту функцию (использовали ее) и вам нужно было ввести int в качестве ввода.
Не беспокойтесь. Вначале это сбивает с толку всех (, хотя, если быть честным, по мере вашего продвижения вы просто найдете новые вещи, с которыми можно столкнуться. Это никогда не остановится ). Продолжайте в том же духе, и все это будет иметь смысл во времени.