Click here to Skip to main content
15,120,478 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to make a simple login system in c++ and I can't find a fix for this error.
Here's my code:

#include "Login.h"
#include <windows.h>
#include <iostream>
using namespace std;


int main()
{
    srand(time(NULL));

    Login(); // Login
}

void Login()
{
    string userName;
    string userPassword;
    int loginAttempt = 0;

    while (loginAttempt < 5)
    {

        std::cout << "Username:";
        std::cin >> userName;
        std::cout << "Password";
        std::cin >> userPassword;

        if (userName == "logintest" && userPassword == "logintest")
        {
            std::cout << "\nLogged in";
        }
        else
        {
            std::cout << "\nError";
        }
    }
}


Code by: Etrik

What I have tried:

I tried literally everything I could find.
Posted
Updated 7-May-21 7:37am
v2

1 solution

You cannot us a function the compiler hasn't seem defined yet. So, you have two options. You can either put the function definition above your main function, like this:
C
#include "Login.h"
#include <windows.h>
#include <iostream>
using namespace std;

void Login();

int main()
{
    srand(time(NULL));

    Login(); // Login
}

void Login()
{
    ...
}

or move the entire Login function above main:
C++
#include "Login.h"
#include <windows.h>
#include <iostream>
using namespace std;

void Login()
{
    string userName;
    string userPassword;
    int loginAttempt = 0;

    while (loginAttempt < 5)
    {

        std::cout << "Username:";
        std::cin >> userName;
        std::cout << "Password";
        std::cin >> userPassword;

        if (userName == "logintest" && userPassword == "logintest")
        {
            std::cout << "\nLogged in";
        }
        else
        {
            std::cout << "\nError";
        }
    }
}

int main()
{
    srand(time(NULL));

    Login(); // Login
}
   
Comments
CPallini 7-May-21 15:55pm
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900