CaptainnnnHINDSIGHT
CaptainnnnHINDSIGHT

Reputation: 1

When I try to compile this C++ program, it gives me an error

This is a program in which someone inputs a password and gets three tries to guess the password. When I compile it, I get multiple errors, one of which includes line 13, where it basically says that it cannot find a similar function included in the Password_Program class.

#include <iostream>
using namespace std;

class Password_Program
{
private:
     string password;
public:
     Password_Program();
     void login();
     string passAttempt;
};

Password_Program::Password_Program()
{
     cin >> password;
}
Password_Program::login()
{
     for (int i = 0; i < 3; i++)
     {
          cin >> passAttempt;
          if (passAttempt == password)
          {
               cout << "Success!" << endl;
               break;
          }
          else if (i >= 3) { cout << "Try again later" << endl;
          break; }
          else { cout << "Sorry, try again" << endl; }
     }
}

int main() {
     Password_Program myPassword;
     myPassword.login;
     return 0;
}

Upvotes: 0

Views: 90

Answers (3)

Iram
Iram

Reputation: 276

2 things are there

  1. You did not added the void when defining the class function login outside the class.
  2. Second that when you are calling login function you are missing ()

Here is the right code.

#include <iostream>
using namespace std;

class Password_Program
{
private:
     string password;
public:
     Password_Program();
     void login();
     string passAttempt;
};

Password_Program::Password_Program()
{
     cin >> password;
}
void Password_Program::login()
{
     for (int i = 0; i < 3; i++)
     {
          cin >> passAttempt;
          if (passAttempt == password)
          {
               cout << "Success!" << endl;
               break;
          }
          else if (i >= 3) { cout << "Try again later" << endl;
          break; }
          else { cout << "Sorry, try again" << endl; }
     }
}

int main() {
     Password_Program myPassword;
     myPassword.login();
     return 0;
}

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

You forgot the return type in the definition of Password_Program::login().

Change

Password_Program::login() {....}

to

void Password_Program::login() {....}

Upvotes: 2

Julian
Julian

Reputation: 1736

Your definition of the login() method is missing a return type. It should be:

void Password_Program::login()
{
    ...
}

You've also forgotten brackets when calling it:

myPassword.login();

Upvotes: 5

Related Questions