Learning C++ simple username/password check

Okay, I have posted here in the past and I am still working on my c++ skills, but I'm a tad curious as to why I have to forward declare a function that I have in a separate file that I've already #included in my header to my main file. Hopefully that makes sense. Here's the code below...
1
2
3
4
5
6
#pragma once
#include <iostream>
#include <string>
#include <ctime>

using namespace std;


1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include "Header.h"

void logIn();

int main()
{
	logIn();
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include "Header.h"

const string USERNAME = "damon";
const string PASSWORD = "password";

string username, password;

void logIn()
{
	cout << "USERNAME: " << endl;
	cin >> username;
	cout << "PASSWORD: " << endl;
	cin >> password;
	if (username != USERNAME && password != PASSWORD)
	{
		cout << "Incorrect username or password, please try again..." << endl;
		logIn();
	}
	else
	{
		cout << "Signing you in now, one moment..." << endl;
	}
}
matadm wrote:
why I have to forward declare a function that I have in a separate file that I've already #included in my header to my main file.

You don't.

It doesn't look like you're including the file that contains the definition of logIn().

Which file does this definition reside in?

MyHeader.h
1
2
3
4
5
6
#include <iostream>

void SomeMethod()
{
    std::cout << "SomeMethod" << std::endl;
}


main.cpp
1
2
3
4
5
6
#include "MyHeader.h"

int main(int argc, const char* argv[])
{
    SomeMethod();
}
I'm a tad curious as to why I have to forward declare a function that I have in a separate file that I've already #included in my header

No, it does not appear that you included the forward declaration in your header.

It's not clear what's being included where since you did not identify your file names.

snippet 1: I'm presuming this is header.h. There is no forward declaration of login() in this header.

snippet 2: I'm presuming this is main.cpp. This is where you have your forward declaration for login(). Your forward declaration belongs in header.h, not here.

snippet 3: I'm presuming this is login.cpp. Since the forward declaration for login() is not in header.h, the compiler won't detect any mismatch in the function signatures.

snippet 3 line 15: You want an || condition here. If either does not match, you want to fail the login.

Also, you might want to make this a bool function since you probably want to return an indication to main that the login failed.

snippet 3 line 18: I'm not a fan of using recursion here. If the user doesn't know the correct user and and password, you're just going to recurse forever. You have no other way out of the program.
Thanks everyone, I apologize for the late response. I got that figured out too, still in my c++ learning stage!
Topic archived. No new replies allowed.