0

I am just studying so don't judge me hard please.

I have a problem. I know how to do a do-while loop. But today I have learned about functions. So I made do-while loops in functions and they are looping infinitely. How do I stop the loops?

#include <iostream>
using namespace std;

void text()
{
    cout << "Log in to see the Menu. " << endl;
}

void lg()
{
    const string login = "el1oz";
    string input;
    
    cout << "Login > " << flush;
    cin >> input;
    
    do{
        if(login == input){
            break;
        }
        else{
            cout << "Try again." << endl;
        }
    }while(true);
    
    cout << "Correct Login! " << endl;
}   
    
void pw()
{
    const string password = "Mau01171995";
    string input1;
    
    cout << "Password > " << flush;
    cin >> input1;
    
    do{
        if(password == input1){
            break;
        }
        else{
            cout << "Try again. " << endl;
        }
    }while(true);
    
    cout << "Correct Passsword! " << endl;
}

int main() 
{
    text();
    lg();
    pw();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    Hint: In your do-while loop, when do you change the value of `input1`? – NathanOliver Nov 25 '20 at 15:15
  • put cin>>input1 inside your while loop – user Nov 25 '20 at 15:15
  • I suggest using descriptive identifiers. No one knows what `lg()`and `pw()` mean. `text()` also doesn't tell you what the method does. Code should be as self-documenting as possible. If I see this sort of thing when interviewing someone for a development job, it's a huge red flag. – 3Dave Nov 25 '20 at 15:22
  • thank you 3Dave. i'll change namings ^^ –  Nov 25 '20 at 15:52

1 Answers1

2
  1. You're not changing input after the code enters in the loop. You should put the cin >> input inside the loop.
  2. Also consider when to use a while loop vs a do while loop. In this case a while loop is better.
  3. You probably should not use using namespace std; (More information here).
  4. You should use more descriptive names.
#include <iostream>
using std::cin;
using std::string;
using std::cout;
using std::flush;
using std::endl;


void printWelcome()
{
    cout << "Log in to see the Menu. " << endl;
}


void inputUser()
{
    const string login = "el1oz";
    string input;

    cout << "Login > " << flush;

    while(cin >> input){
        if(login == input){
            break;
        }
        else{
            cout << "Try again." << endl;
        }
    }

    cout << "Correct Login! " << endl;
}


void inputPassword()
{
    const string password = "Mau01171995";
    string input;

    cout << "Password > " << flush;

    while(cin >> input){
        if(password == input){
            break;
        }
        else{
            cout << "Try again. " << endl;
        }
    }

    cout << "Correct Passsword! " << endl;
}

int main()
{
    printWelcome();
    inputUser();
    inputPpassword();
    return 0;
}
Gary Strivin'
  • 908
  • 7
  • 20
  • why i should not use using namespace std? –  Nov 25 '20 at 15:29
  • 1
    [Why using namespace std; is considered a bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Gary Strivin' Nov 25 '20 at 15:30
  • also can you tell me when to use do while and when while? because i do not really understand –  Nov 25 '20 at 15:53
  • A while loop tests at the beginning of the loop, so a while loop can go round zero times. A do while loop tests at the end of the loop, so a do while loop always goes round at least once. Use which ever best fits your situation, but while loops are more common in general. – john Nov 25 '20 at 15:59
  • i got it. but i don't really understood why in this case "while" is better than "do while"? –  Nov 25 '20 at 17:03
  • Here, `while` should be used for two reasons. First, it makes the code more readable. Second, here you must evaluate the expresion `cin >> input` before the loop is executed or `input` will be unitialized during the first execution and it will result in UB. Usually `do while` loops are used in very specific cases where usually the body of the loop initializes data that is required to evaluate the condition, when you want to execute the body once even if the condition is false or when you ask to the user if he wants to repeat the process. This is neither of these cases so `while` is better. – Gary Strivin' Dec 09 '20 at 17:50