I’m trying to make a program where I have a password in before it but it just says:
admission must return a value
And I do not understand what to do about it.
string Admission() {
string userinput; // users guess
string password = "passwordlol"; // actual password
cout << "Enter password:" << endl;
cin >> userinput;
if (userinput != password) {
cout << "Wrong password, please try again" << endl;
cin >> userinput;
while (userinput != password)
cout << "Wrong password, please try again" << endl;
cin >> userinput;
} else
cout << "Correct password, welcome in.";
}
int main() {
cout << Admission() << endl;
system("pause>nu1");
>Solution :
The issue has been mentioned in the comments. I just want to correct your code a bit.
std::endlslows down your program because of unnecessary flushes. Use'\n'instead.system("pause>nu1");is non-portable code. Usestd::cin.get()or another portable alternative.- Your loop is not good. Use a do-while loop instead of the if-else statement that has a while-loop inside.
- It’s good to declare a variable close to where it will be used. We’re not coding in pre-C99!!! This also reduces your mental load of memorizing where a variable can be modified. So you won’t have to worry much.
#include <iostream>
void Admission()
{
std::string password = "passwordlol"; // actual passaword
std::cout << "Enter password:\n";
bool isCorrect { };
do
{
std::string userinput; // users guess
std::cin >> userinput;
if ( userinput != password )
{
std::cout << "Wrong password, please try again\n";
}
else
{
isCorrect = true;
}
} while ( !isCorrect );
std::cout << "Correct password, welcome in.\n";
}
int main( )
{
Admission( );
std::cin.get( );
}