Error: Undefined Reference

Pages: 12
Alright, first off I am a beginner so try to go easy one me here.

I am making practice programs and I have made 3 if them and I was proud of myself. Anyhow, later I saw that I could split my program into multiple files!

Great! If I could get it to work that is.

My program is a little snippet of a website. It's where the user would log in and create a new account and such. My program however assumes their will only be one user so keep that in mind.

I can't get the variables to work right, I hope you can assume what each does, if not let me know...

Here is my Code:


Main.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include "AccountCreation.h"
#include "Variables.h"

using namespace std;



int main()
{
	int tryedtimes = 0;
	string loginuser = "";
	string loginpass = "";
	char decison = ' ';
	int usernumber = 0;
	//if usernumber = 0 prompt new account Creation
		if (usernumber == 0)
				{
					cout << "Since you are our first user, you will have to create an account" << endl;
					cout << "Just Follow the Instructions" << endl;
					AccountCreation();
				}
	//Ask User if they have logged in before
					cout << "Have You logged in before?" << endl;
					cin >> decison;
	//if yes then prompt to login, if no then prompt account creation
					if (decison =='N')
					{
						AccountCreation();
					}
					else if (decison == 'Y')
					{
						cout << "Please Enter Your User and Password: " << endl;
						cin >> loginuser;
						cin >> loginpass;
						tryedtimes = tryedtimes + 1;
						if (loginuser == user and loginpass == pass)
						{
							tryedtimes = 0;
							cout << "you logged in! ";
							goto end;
						}
					}

					cout << "Please Enter Your User and Password: " << endl;
					cin >> loginuser;
					cin >> loginpass;
					tryedtimes = tryedtimes + 1;
					//if user and pass don't match, let user keep trying until they have tried 5 times
					while (loginuser != user and loginpass != pass)
					{
						loginuser = "";
						loginpass = "";
						cout << "Please Enter Your User and Password: " << endl;
						cin >> loginuser;
						cin>> loginpass;
						tryedtimes = tryedtimes + 1;
						if (tryedtimes == 5)
						{
							//terminate program when user fails to login 5 times
							cout << "Tried to many times: Terminating....";
							goto end;
						}
					}
					//say user logged in and end program
					cout << "You logged in" << endl;
				end:
	cin.get();

	return 0;
}


AccountCreation.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Variables.h"

using namespace std;

void AccountCreation(void) //Creates new user account
{
int usernumber= 0;
 string user = " ";
 string pass= " ";
	cout << "Step 1: What is your user name?" << endl;
	cin >> user;
	cout << "Step 2: What is your password?" << endl;
	cin >> pass;
	usernumber = usernumber + 1;
}


Variables.H

1
2
3
4
5
6
7
8
9
10
11
12
13

using namespace std;

#ifndef VARIABLES_H_
#define VARIABLES_H_

//Declare variables to be compared with user input
extern string user;
extern string pass;
extern int usernumber;

#endif /* VARIABLES_H_ */


Any help on what's wrong here?
Last edited on
In "AccountCreation.cpp" include the string header. It's not considered a primitive data type.
You are declaring global extern variables but never define them anywhere. And global variables are sign of bad style.

you should return user data from AccountCreation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct UsedData
{
    std::string login;
    std::string pass;
}; 

/*----------------*/

UserInfo AccountCreation() 
{
    UserInfo user;
    std::cout << "Step 1: What is your user name?\n"l;
    std::cin >> user.login;
    std::cout << "Step 2: What is your password?\n";
    std::cin >> user.pass;
    return user;
}


using namespace std; is a sign of bad style too, but there is one place where you should never have it even if you really want: header files. Never place using namespace std in headers.

First off thank you!

MiiNiPaa I had no idea using namespace std is bad >.>

If I may ask what is wrong with using it?
(I actually didn't need it In the header I have no idea why I put it there)

And if using namespaces is bad practice... why do they have it?
-------------------------------------------------------------------------------------

Also what's this:

1
2
3
4
UserInfo AccountCreation() 
{
    UserInfo user;
}


It is definitely a function but I've never seen one declared like that.
Last edited on
TheBadCoder wrote:
If I may ask what is wrong with using it?
http://stackoverflow.com/q/1452721/1959975
TheBadCoder wrote:
And if using namespaces is bad practice... why do they have it?
In the early versions of C++, the standard library was not in the std namespace. When they moved it, they added the using namespace blah; syntax to ease transition for developers.
TheBadCoder wrote:
Also what's this:
1
2
3
4
UserInfo AccountCreation()
{
    uderInfo user;
}
It's a normal function, but it neglects to return a value so it will cause undefined behavior at runtime. Maybe you meant to have return user; or similar at the end?
Last edited on
LB wrote:
It's a normal function, but it neglects to return a value so it will cause undefined behavior at runtime.


Doesn't this do the same thing:

 
void UserInfo (void)
UserInfo user; is a variable declaration. Like int x; declares variable x with type of int, UserInfo user; declares variable user with type of UserInfo.

Ont he other hand UserInfo AccountCreation() means that function does not take any parameters and returns variable of type of UserInfo; like int foo() returns int
Last edited on
well that explains my error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct UsedData
{
    std::string login;
    std::string pass;
}; 

/*----------------*/

UserInfo AccountCreation() 
{
    UserInfo user;
    std::cout << "Step 1: What is your user name?\n"l;
    std::cin >> user.login;
    std::cout << "Step 2: What is your password?\n";
    std::cin >> user.pass;
    return user;
}


 
"UserInfo Does not name a type"

Yes. I made a mistake: it should be struct UserInfo
Last edited on
Alright one more thing then hopefully I can do my other 3

is their a way I can compare the strings pass and login in main.cpp?

I tried to do it by saying:

main.cpp (loop snippet)

 
while (loginuser != UserInfo.login and loginpass != UserInfo.pass)


however I seem to be the dumbest person on the planet and can't figure out how to get main to recognize UserInfo struct

and I also have another file
AccountCreation.H

1
2
3
4
5
#ifndef ACCOUNTCREATION_H_
#define ACCOUNTCREATION_H_
AccountCreation();
#endif /* ACCOUNTCREATION_H_ */


Since it has the struct name in front of it in the original .cpp file I have no idea how to reference it in the .h file so I can call it in my main file

its giving me:

 
expected constructor, desconstructor or type conversion


I'm sorry if I am irritating all of you. But I'm learning xD
Last edited on
however I seem to be the dumbest person on the planet and can't figure out how to get main to recognize UserInfo struct Move its declaration in AccountCreation.h file, as it is its main user (Proper way would be move it to its own header and make its users include it)
Well I have another error But I will just ask my teacher its a small error after all.

Thanks guys! :)
TheBadCoder wrote:
Well I have another error
I know why. Read this thread:
http://www.cplusplus.com/forum/beginner/142814/
No, its a logic error... and my teacher didn't help .-.

whenever my program goes to if statements like so:

 
if (loginpass = user.login and loginpass = user.pass)


after the if statements it checks to see if the user and password match the ones that belong to the class variables. However, when I try to compare them the strings In the class equal nothing.

I can't get it to compare the variables the user entered In AccountCreation function.

Any ideas?
You used assignment operator = instead of the equality comparison operator ==
no in my main I have a comparison operator.. its logic error, not a syntax error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int main()
{
	UserInfo user;
	int tryedtimes = 0;
	std::string loginuser = "";
	std::string loginpass = "";
	char decison = ' ';
	int usernumber = 0;
	//if usernumber = 0 prompt new account Creation
		if (usernumber == 0)
				{
					std::cout << "Since you are our first user, you will have to create an account" << std::endl;
					std::cout << "Just Follow the Instructions" << std::endl;
					AccountCreation();
				}
	//Ask User if they have logged in before
					std::cout << "Have You logged in before?" << std::endl;
					std::cin >> decison;
	//if yes then prompt to login, if no then prompt account creation
					if (decison =='N')
					{
						AccountCreation();
					}
					else if (decison == 'Y')
					{
						std::cout << "Please Enter Your User and Password: " << std::endl;
						std::cin >> loginuser;
						std::cin >> loginpass;
						tryedtimes = tryedtimes + 1;
						if (loginuser == user.login and loginpass == user.pass)
						{
							tryedtimes = 0;
							std::cout << "you logged in! ";
							goto end;
						}
					}

					std::cout << "Please Enter Your User and Password: " << std::endl;
					std::cin >> loginuser;
					std::cin >> loginpass;
					tryedtimes = tryedtimes + 1;
					//if user and pass don't match, let user keep trying until they have tried 5 times
					while (loginuser != AccountCreation().login and loginpass != AccountCreation().pass)
					{
						loginuser = "";
						loginpass = "";
						std::cout << "Please Enter Your User and Password: " << std::endl;
						std::cin >> loginuser;
						std::cin>> loginpass;
						tryedtimes = tryedtimes + 1;
						if (tryedtimes == 5)
						{
							//terminate program when user fails to login 5 times
							std::cout << "Tried to many times: Terminating....";
							goto end;
						}
					}
					//say user logged in and end program
					std::cout << "You logged in" << std::endl;
				end:
	std::cin.get();

	return 0;
}
Last edited on
Well, considering you have the same code in three different ways in three different places, I'd suggest getting it down to just one place.
Alright its In two places because they both compare the same variables:

main.cpp (while loop)

while (loginuser != user.login and loginpass != user.pass)

main.cpp (if statement)

if (loginuser == user.login and loginpass == user.pass)

The while loop and the if statement are supposed to compare the variables the user entered in the AccountCreation function:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "UserInfo.h"

UserInfo AccountCreation()
{
    UserInfo user;
    std::cout << "Step 1: What is your user name?\n";
    std::cin >> user.login;
    std::cout << "Step 2: What is your password?\n";
    std::cin >> user.pass;
    return user;
}


The user inputs the info from a struct:

1
2
3
4
5
6
7
8
#ifndef USERINFO_H_
#define USERINFO_H_
struct UserInfo
{
    std::string login;
    std::string pass;
};
#endif /* USERINFO_H_ */ 


When the program goes through the while and if's. It keeps asking to input the user and password because it is not comparing the variables that's entered in the AccountCreation function.

Understand it now?

if you want the whole program to copy to your compiler let me know
Last edited on
TheBadCoder wrote:
Alright its In two places because they both compare the same variables:
You must be using very large values of 2 - http://imgur.com/awsWbWm
the boxes you highlighted compare what the user actually inputted to the variables in the user database in the Account Function....

http://i.imgur.com/wZWMOxN.png

The if statement and while loop compare different variables...

See it?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "UserInfo.h"

UserInfo AccountCreation()
{
    UserInfo user;
    std::cout << "Step 1: What is your user name?\n";
    std::cin >> user.login;
    std::cout << "Step 2: What is your password?\n";
    std::cin >> user.pass;
    return user;
}
Pages: 12