hope you are doing fine! I have an exam next week and I've been stuck in a part of my program... I'm trying to use an item from a function in an 'if-statement' but couldn't find a way to do it... I've been doing some research and some says I should use pointers, could someone explain me how to do it?
// This is where the user information is stored
std::string storedInformation(std::string storedID1 = "admin", std::string storedPW1 = "123", std::string storedMail = "admin@admin") {
return storedID1, storedPW1, storedMail;
}
So, the part above, at first I made it a const char, but I had trouble trying to make an if-statement comparison when checking if the user has entered valid credentials... I did make it compile and run using std::string instead of const char, but has some bugs...
// Login phase, where I try to get user info from another function,
// I think probably with pointers or maybe I should have made a class?
void loginPhase(std::string userID, std::string userPW) {
std::string sid1, spw, sml;
storedInformation(sid1, spw, sml);
// Testing to see if it prints all three components, but it only prints the last 'admin@admin'
std::cout << storedInformation() << std::endl;
// Here I try to access storedInformation() items while using an 'if statement'
// I've been doing some research, but I couldn't find a way to do it...
do
{
std::cout << " Type your Original Game Identity > " << std::flush;
std::cin >> userID;
std::cout << " Type your Original Game Code > " << std::flush;
std::cin >> userPW;
if (userID != storedInformation(sid1) && userPW != storedInformation(spw))
{
std::cout << "Invalid credentials, please type it again." << std::endl;
std::cout << std::endl;
}
} while (userID != storedInformation(sid1) && userPW != storedInformation(spw));
std::cout << "Credentials accepted" << std::endl;
std::cout << std::endl;
}
In this part of the code, doesn't matter if I input the right credentials, It doesn't accept it... Probably because the program still can't access storedInformation(storedID1) and storedInformation(storedPW1)...
And just for testing purpose, I tried to print the current values stored in the function 'storedInformation(storedID1, storedPW1, storedMail)', but it only prints the last item which is 'admin@admin'...
I'll post the whole code below, hope I didn't confuse you too much :(
#include <iostream>
#include <limits>
void presentationPhase() { // Just header and titles
std::cout << " |||| WEBSITE SIMULATOR |||| " << std::endl;
std::cout << std::endl;
}
// Still gonna add a switch to choose what menu the user wants
void menuPhase(std::string menuEntry1, std::string menuEntry2, std::string menuEntry3, std::string menuEntry4, std::string menuEntry5) {
std::cout << " |||| MAIN MENU |||| " << std::endl;
std::cout << std::endl;
std::cout << menuEntry1 << std::endl;
std::cout << std::endl;
std::cout << menuEntry2 << std::endl;
std::cout << std::endl;
std::cout << menuEntry3 << std::endl;
std::cout << std::endl;
std::cout << menuEntry4 << std::endl;
std::cout << std::endl;
std::cout << menuEntry5 << std::endl;
std::cout << std::endl;
}
// This is where the user information is stored
std::string storedInformation(std::string storedID1 = "admin", std::string storedPW1 = "123", std::string storedMail = "admin@admin") {
return storedID1, storedPW1, storedMail;
}
// Just a login message
void loginPresentation() {
std::cout << " |||| ||||||||||||||||| |||| " << std::endl;
std::cout << " |||| LOG IN NOW AND GO |||| " << std::endl;
std::cout << " |||| ||||||||||||||||| |||| " << std::endl;
std::cout << std::endl;
}
// Login phase, where I try to get user info from another function,
// I think probably with pointers or maybe I should have made a class?
void loginPhase(std::string userID, std::string userPW) {
std::string sid1, spw, sml;
storedInformation(sid1, spw, sml);
// Testing to see if it prints all three components, but it only prints the last 'admin@admin'
std::cout << storedInformation() << std::endl;
// Here I try to access storedInformation() items while using an 'if statement'
// I've been doing some research, but I couldn't find a way to do it...
do
{
std::cout << " Type your Original Game Identity > " << std::flush;
std::cin >> userID;
std::cout << " Type your Original Game Code > " << std::flush;
std::cin >> userPW;
if (userID != storedInformation(sid1) && userPW != storedInformation(spw))
{
std::cout << "Invalid credentials, please type it again." << std::endl;
std::cout << std::endl;
}
} while (userID != storedInformation() && userPW != storedInformation());
std::cout << "Credentials accepted" << std::endl;
std::cout << std::endl;
}
int main() {
presentationPhase(); // Title stuff...
menuPhase("Login", "Register", "Download", "Forum", "Support"); // Eventually I will try to read the main menu titles from a server file
loginPresentation();
loginPhase("default", "default");
}
EDIT: I made it work with Classes as instructed by the amazing ppl in the comment section <3
I'll post the edited code incase anyone else that got stuck like me needs it. Lets keep studying!
UserData.h
class Usersinfo {
public:
const std::string userID = "admin";
const std::string userPW = "123";
void loginIteration();
};
UserData.cpp
void Usersinfo::loginIteration() {
std::cout << "User Login Interface" << std::endl;
std::string uid;
std::string upw;
Usersinfo uData;
bool isLoginValid = false;
do
{
std::cout << "Login > " << std::flush;
std::cin >> uid;
std::cout << "Password > " << std::flush;
std::cin >> upw;
if (uid != uData.userID || upw != uData.userPW)
{
std::cout << "incorrect input! Please, type again..." << std::endl;
}
if (uid == uData.userID && upw == uData.userPW)
{
isLoginValid = true;
}
} while (!isLoginValid);
std::cout << "Login accepted!" << std::endl;
}