0

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;
}
Houkros
  • 13
  • 2
  • Functions return 1 value, not 3. Though that value could be a class with multiple fields. And, btw, the comma operator yields the value of the last item discarding the earlier ones after evaluating them. – Avi Berger Aug 01 '22 at 21:49
  • @AviBerger, Thank you for answering, I'll make some changes to the code based on your information, I really appreciate the help <3 – Houkros Aug 01 '22 at 21:53
  • 1
    `storedInformation` doesn't store anything. Exactly what are you trying to accomplish with this function? – jkb Aug 01 '22 at 21:54
  • `auto storedInformation(/* ... */) { return std::tuple{storedID1, storedPW1, storedMail}; }` and then `auto[id1, pw1, mail] = storedInformation(/* ... */);` would be an option. – Ted Lyngmo Aug 01 '22 at 21:58
  • Hi @jkb! So I was trying to store some user information like user id, password and email, so I could access it later prompting the user to type the information stored in 'stored information' function... basically ask the user to login in a website – Houkros Aug 01 '22 at 21:58
  • You need to read some documentation on what the `,` operator does. Definitely not what you may think. Also, `return` does not magically write something into a function’s arguments. And a function’s arguments do not magically affect the function's call site … unless references or pointers are involved. – Andrej Podzimek Aug 01 '22 at 22:05
  • You may want to get out you text book and review the part about functions. If you need another book you might look [here](https://stackoverflow.com/q/388242/631266). Functions do stuff. Plain old data variables store stuff. Objects package the two together. And every time you call a function, it does its stuff all over again. – Avi Berger Aug 01 '22 at 22:07
  • @TedLyngmo, Hi! Thanks for the suggestion, I was actually trying to surprise my teacher with a simple 'ask the user to log in with the stored information' program, I really enjoyed your suggestion, but I'll have to take a look into templates... since it seems confusing for me right now... But thanks really! – Houkros Aug 01 '22 at 22:20
  • There is also a good chance you want to look into "struct" or "class" with "member". You might also want to look up "variable" and "enum". Because I thing what you try to do with your first function is to initialize a class or struct, without having declared it first. – Samuel Åslund Aug 01 '22 at 22:23
  • @AndrejPodzimek, Hi! Thanks for taking your time to answer my topic! At first I made a simple code, but as soon as I couldn't access items inside functions, I started trying some other stuff I been studying... I still gotta look into pointers tho... Anyway I'll just not try that hard and do something else for my exam. Thanks for helping, I really appreciate it <3 – Houkros Aug 01 '22 at 22:26
  • @AviBerger, I'll check it out, I just bought some online classes at Udemy, I'll probably have to buy a book too because everybody tells me that they are more reliable than online videos... Thanks for the help >. – Houkros Aug 01 '22 at 22:31
  • 2
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 01 '22 at 22:44
  • You are trying to get a new technique to work -- by implementing it in a large, complicated stretch of code, using *strings.* Set all of this aside, start from a clean slate, and write the simplest program you can imagine that does the new thing: an `if` statement that tests the value returned by a function. *No strings.* Get it working perfectly before you try to integrate it into all the other code. – Beta Aug 01 '22 at 23:02
  • Without knowing what your level of experience is, it's difficult to know exactly what to recommend. Storing information requires that it be, well, stored somewhere. That means variables and/or objects. Since it looks like you want to store three pieces of information, you'll need three storage locations, either individual variables, or a struct (or class) with three data members. If you were to use a struct/class, then in place of your function you could write something like `storedInformation.userId = userId` to store it, and later a test like `userId != storedInformation.userId`. – jkb Aug 01 '22 at 23:36
  • Thanks everyone, I really appreciate the help, I did it thanks to you all! I have a long road ahead of me, but i'm learning everyday and I'll make sure to do heavy researches on my problems. @jkb thank you for the help <3 I edited the post with the working code! – Houkros Aug 02 '22 at 06:31
  • also never ever store password, leave security to experts. – Goswin von Brederlow Aug 02 '22 at 14:10
  • @GoswinvonBrederlow, Hi! It's just for learning purpose, don't worry, I still have a long road ahead of me ~ thanks for answering! – Houkros Aug 03 '22 at 01:42

0 Answers0