-2

I put a do/while loop in my password function but it doesn't work. I use xcode10 to code c++ and when I use a semicolon after the while statement it shows an error saying code will never execute

string password (string g)
{
    string ch = "hello" ;
    cout << "Enter password";
    getline(cin, g);
    do {
        if (ch.compare(g) != 0) {
            cout << " INCORRECT PASSWORD";
            return (0);
        } else {
            cout << "correct password";
            return (string(g));
        }
    } while (ch.compare(g) == 0);  //this is where it shows the error that the code will never exec
}

I wanted to put this loop and a few other things so I can make this a infinite loop till you enter the correct password.

  • 1
    So when do you think that `while` condition will be executed? Also, `return (0);` for constructing `std::string` has undefined behavior. – Praetorian Feb 10 '19 at 20:47

3 Answers3

1

Well in your if statement you will return in both cases causing the function to stop so it will never get to the while condition to test it

  string password(string g)
{
    string ch = "hello";
    cout << "Enter password\n";
    do
    {
        getline(cin, g);

        if (ch.compare(g) != 0)
        {
            cout << " INCORRECT PASSWORD\n";
        }
        else {
            cout << "correct password";
            return (string(g));
        }
    } while (ch.compare(g) != 0);  
}
Waqar
  • 8,558
  • 4
  • 35
  • 43
Spinkoo
  • 2,080
  • 1
  • 7
  • 23
  • hey! so I removed the return function from if and else, its still not working. the do while loop. if I enter the correct password it continues, it I enter the wrong password then the output displays incorrect password and stops. it displays incorrect password and its like this. Enter password no Incorrect Password (11db). I am using Xcode for c++. – Vibhor Sagar Feb 12 '19 at 00:49
0

There is Return statement in the "if" and also in the "else". You can see that no matter what will be the result of ch.compare(g), the function will return to it's caller.

That's why it will never do the "while".

Try to set the Return statements in different place in the code :)

Waqar
  • 8,558
  • 4
  • 35
  • 43
Yuvalta
  • 5
  • 6
0

You need to check if you get input at all too, in case of EOF.

string password() { // you don't need g as parameters in, your overwriting it
    string const ch = "hello"; // make ch const to show it not supposed to change
    cout << "Enter password";
    string g; // defining g here since now you need it
    while (getline(cin, g)) { // check that the read from cin is OK
        if (ch != g) { // simple notation for comparing two strings. There's also == available
            cout << "INCORRECT PASSWORD. Please try again\n"; // no return if you want to try again
        } else {
            cout << "correct password";
            return g; // you could also return ch here since they have the same content
        }
    }
    cout << "Unable to read line. aborting\n"; // not really a lot you can do if there is no input to read.
    return string(); // returning empty string.
}
Waqar
  • 8,558
  • 4
  • 35
  • 43
Bo R
  • 2,334
  • 1
  • 9
  • 17