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...
#include <iostream>
#include <string>
#include "AccountCreation.h"
#include "Variables.h"
usingnamespace 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();
}
elseif (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"
usingnamespace 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
usingnamespace std;
#ifndef VARIABLES_H_
#define VARIABLES_H_
//Declare variables to be compared with user input
extern string user;
extern string pass;
externint usernumber;
#endif /* VARIABLES_H_ */
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;
}
usingnamespace 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 usingnamespace std in headers.
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 usingnamespace 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?
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
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
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)
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.
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();
}
elseif (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;
}
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;
}
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
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;
}