Today I start to learn c++ a bit and I begins to understand with simple code, but this make confuse me
#include <iostream>
using namespace std;
class MyVar
{
public:
MyVar(){
cout << "this is constructor" << endl;
}
void accesscheckpass(string x){
checkpass(x);
}
private:
bool checkpass(string x){
if(x == "password"){
return true;
}
}
};
int main()
{
int inputpass;
cout << "please enter your password\n" << endl;
cin >> inputpass;
MyVar MyAccess;
if(MyAccess.accesscheckpass(inputpass) == true){
cout << "welcome user 1" << endl;
}else{
cout << "get lost !" << endl;
}
}
I want to validate the user when he/she entered the password, then when to section IF, when I want to compile it, compiler return the status “invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]|”, please someone repair my code and explain what i’m wrong ?
Try to repair it yourself after we explain what’s wrong. You’ll gain much more from it than by us posting your code.
Sure. The method
accesscheckpassexpects astd::stringas parameter (btw you need to#include <string>at the top of the file). You call it asbut
inputpassis declared asint inputpass;, so it’s andint, not astd::string. So you either have to declareinputpassas astringor find out how to convert anintto astring. (should be easy)Also, your method:
only returns if the condition is true. You should have an
elsebranch as well:or, better yet, return the result directly.
and your method
should return a
boolas well.