1

the question as the title suggests, is an error when i was executing my program in the certain part of the password function. actually it is a basic password function which was working properly in turbo c++, but in visual c++ this error arrives

void user::password()
 {
  char any_key, ch;
  string pass;
  system("CLS");        
  cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER 
            PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
  start:
  getline(cin,pass);
   if (strcmp(pass, "sha") == 0)           //this is where the error is!*
    {
       cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
       cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
       cin >> any_key;
    }
   else
    {
       cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
       goto start;
    }
  system("CLS");
  }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Shazin
  • 13
  • 4
  • Side issue, see [why goto is bad](https://stackoverflow.com/q/3517726/10957435). –  Sep 03 '19 at 16:50

2 Answers2

5

The expression in the if statement

if (strcmp(pass, "sha") == 0) 

is incorrect.

The function requires the both parameters of the type const char * while you supplied the first argument of the type std::string and there is no implicit conversion from the type std::string to the type const char *.

Use instead

if ( pass == "sha" ) 

In this case there is an implicit conversion from the type const char * (the type of the string literal after its implicit conversion from the array type) to an object of the type std::string due to the non-explicit constructor

basic_string(const charT* s, const Allocator& a = Allocator());
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Actually, a `string` doesn't get constructed, there's an `operator==` overload for `const char*` so it's as efficient as `strcmp(pass.data(), "sha");` http://www.cplusplus.com/reference/string/string/operators/ – Mirko Sep 03 '19 at 21:17
3

You can also convert the string to a const char* and perform the comparison with strcmp.

if (strcmp(pass.c_str(), "sha") == 0) 
Guillaume Adam
  • 191
  • 2
  • 10
  • If this is your solution, then it is a [duplicate](https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char). – 1201ProgramAlarm Sep 03 '19 at 20:47