Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ better way?


ghost's Avatar
0 0

Ok I was kinda Bored so I coded this:

#include <string>
using namespace std;

int main()
{
string guess;
string password = "haveit";
string quit = "q";
do
{
cout << "try and guess password: \n"; 
cin >> guess;
if (guess == password)
{
cout <<"you got it chief\n";
system("pause");          
}
if (guess == quit)
{
cout <<"you quitted\n";
system("pause");  
}
else 
cout <<"wrong password\n";
}while(guess != password && guess != quit);

return 0;  
}

I know the basics of C++ and getting better. I know it's sloppy and newbish. Is there a better way to do this?. Thanks so much! (There are no errors)


ghost's Avatar
0 0

I don't know of a necessarily better way to do what you're trying to do, as it's fairly straight-forward… however, take a careful look at the output of your program when you input the correct password. if you input the password, it displays "you got it chief" and pauses, but after the pause if you look real quickly before the window closes, you can see that it also outputs "wrong password".

The reason for this is because each "else" statement gets paired with the if statement that comes immediately before it, so when you put in the correct password it executes the code in the first "if" statement, then runs into the second "if" statement and checks if "guess == quit". This returns false and executes the code in the else statement, which causes "wrong password" to flash on the screen before the program exits.

The way to avoid this would be to use an if-else if statement if you have multiple conditions that need to be checked:

if(guess == pass)
{
code here;
}
else if(guess == quit)
{
more code here;
}
else
{
other code;
}

While such a small detail doesn't matter much when you're first learning a language, it can definitely cause headaches when you start coding more serious programs.


chess_rock's Avatar
Member
0 0

Try to avoid using system procedures like:

markupsystem("pause");

When you program applications, you want to make sure they'll work in as many platforms as possible. That pause is a windows only function. And also remember that for console applications, you'll be most of the times opening the program on a console window, not needing any sort of pause.


ghost's Avatar
0 0

I don't know of a necessarily better way to do what you're trying to do, as it's fairly straight-forward… however, take a careful look at the output of your program when you input the correct password. if you input the password, it displays "you got it chief" and pauses, but after the pause if you look real quickly before the window closes, you can see that it also outputs "wrong password".

The reason for this is because each "else" statement gets paired with the if statement that comes immediately before it, so when you put in the correct password it executes the code in the first "if" statement, then runs into the second "if" statement and checks if "guess == quit". This returns false and executes the code in the else statement, which causes "wrong password" to flash on the screen before the program exits.

The way to avoid this would be to use an if-else if statement if you have multiple conditions that need to be checked:

totally forgot about the "else if" rule, thanks for helping

Try to avoid using system procedures like:

markupsystem("pause");

When you program applications, you want to make sure they'll work in as many platforms as possible. That pause is a windows only function. And also remember that for console applications, you'll be most of the times opening the program on a console window, not needing any sort of pause.

Should I try using markupcin.get(); instead?


GTADarkDude's Avatar
Member
0 0

nack wrote: Should I try using markupcin.get(); instead? Yes. cin.get() can 'fail' to wait for input though, when there's still something left in the stream buffer. But when you properly empty your stream buffers (cin.ignore(…)), cin.get() and/or getline(cin, …) are your best options I think.