Van Guard
Van Guard

Reputation: 1

basic C++ (cout buffer or overflow i think?!)

ok, so im kinda new to C++ and im trying to build a basic login program. iv got it to work except for one problem... this is what iv got

void InitialLogin()
{
        cout << "\t\t   ===========LOGIN==========" << endl;
        cout << "\t\n\n\n\tUsername/>> " ;
        getline(cin, sUsername);

        cout << "\t\n\tPassword/>> ";
        getline(cin, sPassword);

    if (sUsername == "myname" && sPassword == "mypass")
     {
        cout << "\n\t\t\t--ACCESS GRANTED--";
        system("CLS");
     }
        else if (sUsername != "myname" && sPassword != "mypass")
        {
            SetConsoleTextAttribute(h, FOREGROUND_RED);
            cout << "\n\t\t\t--ACCESS DENIED--\n\n\n";
            SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            main();
        }

}

after i run this, if the login was successfull i have the following run after the screen is cleard...

void Initialization()
{
    cout << "/>> Aquireing file list......" << endl;
    cout << "/>> file list aquired........" << endl;
    cout << "/>> determing file location.." << endl;



}

now my problem is, if i get the username or password wrong say once i get the initialization output twice, if i get it wrong twice, the output is three times.

i need to know how to clear the overflow or buffer or something... please help :)

Upvotes: 0

Views: 83

Answers (2)

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

Your function is attempting to call main(). This is not legal C++.

You should return a status to see if the login worked or not. Then just loop until the status either becomes "true", or the user has run out of chances. Here is a small example:

bool InitialLogin();

int main()
{
   int numChances = 3;
   int numCount = 0;
   bool loginOk = false;
   while (numCount < numChances && !loginOk )
   {
      loginOk = InitialiLogin();
      ++numCount;
   }
   if ( !loginOk )
   {
      // number of chances ran out
      return 0;
   }
   else
   {
      // proceed.  Login was successful
   }
}


bool InitialLogin()
{
    //...
    if (sUsername == "myname" && sPassword == "mypass")
    {
        //...
        return true;     
    }
    // anything here means that the login failed.  There is no need for an if()
    //... 
    return false;
}

Upvotes: 2

Songy
Songy

Reputation: 851

I feel you are getting this because you are calling the main() function when both the password and username are incorrect (As a side note you might want to change the && in this check to || as you want access to be denied if either the password or username are incorrect).

You might want to consider getting the InitialLogin() function to return a bool to show weather the login was successful or not. If not the get it to run InitialLogin() again if successful then get it to proceed with the code.

In the place where you call InitialLogin() you can use a while loop to get it to repeat.

while(!InitialLogin()){}

So when InitialLogin() is not false (bad login) it will try again and when it is not true (good login) it will continue onto next section of code.

Upvotes: 0

Related Questions