New to c++ and tried writing a login program. When running, you need to answer login or register to choose to make or to login to an account. Though for some reason when either typing login or register it runs the login function( logFunction() ). Plus, when typing in a command that isn’t login or register for the second time , it will say wrong command and then it will run logFunction. Sry if this is a bad explanation. I would love to understand what went wrong and how to fix it.
Sry, don’t mean declared just mean called. I understand declaring a function is defining the name and type plus perimeters in the header of the code file. I followed the advice “Ganado” gave me. And saw that the function would run twice in the if statement if called.
#include <iostream>
int userPassword;
std::string text;
std::string text1;
int password = 1234;
int attepmts = 1;
bool in = false;
bool logFunction() {
std::cout << "Please type in your password: ";
std::cin >> userPassword;
while (userPassword != password && attepmts <= 2) {
std::cout << "Please try again\n";
std::cin >> userPassword;
attepmts++;
}
if (userPassword == password) {
std::cout << "Password Correct\n";
returntrue;
}
std::cout << "Only three attempts allowed , try again later\n";
returnfalse;
}
bool regFunction() {
std::cout << "registered";
returntrue;
}
bool userCheckFunction() {
returnfalse;
}
int main() {
bool loggedin {};
std::cin >> text1;
if (text1 == "login")
loggedin = logFunction();
elseif (text1 == "register")
loggedin = regFunction();
else {
std::cout << "That command isn't available, please try another""\n";
std::cin >> text1;
}
const std::string opp[4] { "help", "bankb", "profile", "logout" };
if (loggedin) {
std::cout << "\nWelcome to The bank, type help for navigation help\n";
std::cin >> text;
if (text == opp[0]) {
std::cout << " Type:""\n""'bankb' for bank balance""\n""'profile' to see your username and password""\n""'logout' to logout""\n";
std::cin >> text;
} elseif (text == opp[1]) {
//...
} elseif (text == opp[2]) {
// ...
} elseif (text == opp[3]) {
std::cout << "logged out";
} else {
std::cout << "that command isn't available\n";
}
}
}
Note that usage of global variables is not considered good practice. They should either be locally defined near to where they are first used, or as function parameter. There' also the issue of re-entering a command both after one has completed or if an invalid one is entered.
isn’t it a if statement that would run if logFunction returns true?
If statement has:
1 2 3
if ( condition ) {
body
}
The condition is an expression that has to be evaluated first.
IF the result of condition is true THEN the body is executed.
In other words, in
1 2 3
if ( logFunction() ) {
body
}
The body is run only if the logFunction() returns true.
The only way to know what logFunction() returns is to call it.
The if calls logFunction(). There are no ifs about that.
This worked, thank you. Could you or someone explain why I have to make another bool. I just want to understand for future projects. Thanks again. (Edit: sry didn’t see the post above, makes sense now, thanks)
Your functions called logFunction() and regFunction() are of type bool. They return a bool value, which is stored in the bool variable loggedin. This variable is used on line 54.