-4

I'm trying to make this program where one function takes in the password you write and in the checkPassword function all I do is check if the password matches the correct word. If so I print "Correct". How exactly do I implement this by breaking it down into different function, like how do I get the checkPassword function to take what I wrote in loadPassword? How do I make it run in the main function?

void loadPassword() {
    std::string password;
    std::cout << "Enter password: \n";
    std::cin >> password;
}
std::string checkPassword(std::string password) {
    std::string correct;
    correct == "right";
    if (password == correct) {
        std::cout << "correct";
    }
}

int main() {
    loadPassword();
    checkPassword(//HOW DO I MAKE IT TAKE WHAT I WROTE IN loadPassword);
}
  • 3
    Make `loadPassword` return std::string?? – MFisherKDX May 11 '19 at 06:42
  • 1
    Return it from `loadPassword`? `std::string loadPassword();` and then `checkPassword(loadPassword())`. BTW, you're declaring `checkPassword` as returning a string yet don't return anything. – bipll May 11 '19 at 06:43
  • `correct == "right";` -- Are you sure you want to do this? – PaulMcKenzie May 11 '19 at 06:50
  • If `loadPassword` should *return* something, what do you need to change in that function? And should `checkPassword` really return a string? – Some programmer dude May 11 '19 at 07:00
  • You definitely should read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about the basics. Learning c++ by trial and error and asking trivia questions at Stack Overflow won't work. – πάντα ῥεῖ May 11 '19 at 07:31

1 Answers1

1

Well either you make loadPassword() returns a std::string and you pass it to checkPassword(std::string password) or you make password as a global variable which is something I highly advise you against (best practices...)

So your best solution would be:

std::string loadPassword() {
    std::string password;
    std::cout << "Enter password: \n";
    std::cin >> password;
    return password;
}
bool checkPassword(std::string password) {
    std::string correct;
    correct = "right"; //why ==? == returns a boolean.
    if (password == correct) {
        return true;
    }
    return false;
}

int main() {
    std::string passInput = loadPassword();
    if (checkPassword(passInput)) std::cout << "correct";
    else std::cout << "not correct";
}

Edit: I'm sorry I didn't pay attention to checkPassword() return type, Paul pointed it out. You cannot specify a return type for a function and you don't end up by returning an instance of that type. In this case I edited your checkPassword() to return bool. If true your password is correct, else it is not. Also, correct=="right", the operator "==" is not the assignment operator. I'll leave you below some links you need to take a look at.

Cheers!

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34
  • 1
    The function `checkPassword` is supposed to return a `std::string`, yet doesn't return anything. Undefined behavior. In addition, `correct == "right";` – PaulMcKenzie May 11 '19 at 06:55
  • Oh yes! That slipped away! – Oussama Ben Ghorbel May 11 '19 at 06:56
  • Say I wanted to add a function "Change Password" that once I entered the correct password would allow me to type in "Change Password" and it would let me change the password. How would I implement that? –  May 11 '19 at 08:54
  • Heuh? Change that specific password? In that case maybe you need to store password in some sort of data structure and map them with something else maybe usernames (database)? So changing password you would pass two arguments to your function: username and password. You’ll go through your data structure when you find the username passed as argument you change its old password by the new one. However, Stackoverflow is not the place to ask people to implement you stuff. So please either mark the whole question as off topic or accept my answer in case you judge it answers your first question. – Oussama Ben Ghorbel May 11 '19 at 09:06