Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:58:39+00:00 2026-06-18T12:58:39+00:00

When I run my program the user can log in, but if the enter

  • 0

When I run my program the user can log in, but if the enter in a wrong user name, it runs a check username loop again, saying that they did not enter in a valid user name. This work perfectly fine except for one thing. Say they attempt to log in in three times and the third attempt is correct and the get prompted for a password. Once they input it it ask for a second password and then a third. It seems like it is completing the function for the other attempts. I can not think of a way to check for this. Any ideas.

If you look at it you can see that I am calling UserCheck inside of getNameIndex. I am almost positive this is where the error is occurring.

The Function that checks the users:

void User_Psw::UserCheck()
{
    // read from the database
    ifstream fin("dataBase.txt", ios::in);

    if( !fin.good() )
    {
        cout << "Failed to open database file." << endl;
        return;
    }

    while (fin >> username >> password)
    {
        Usernames.push_back(username);
        Password.push_back(password);
        ++sizeOfDatabase; // This may or may not be needed elsewhere.
    }

    // rest of the program
    cout << "Username: ";
    cin >> username;

    getNameIndex();

    cout << "Password: ";
    cin >> password;

    if(!PasswordMatches())
    {
        cout << "Access denied";
    }
    else
    {
        cout << "Success! You have logged in.";
    }
}

This is the username check function

void User_Psw::getNameIndex()
{
    userThere = false;

    for(int i=0; i < sizeOfDatabase; i++)
    {
        if (Usernames[i] == username)
        {
            index = i;
            userThere = true;
        }
    }
    if (userThere == false)
    {
        cout << "\nThat user name does not exsist. \n";
        cout << "Please try again. \n\n";
        UserCheck();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:58:41+00:00Added an answer on June 18, 2026 at 12:58 pm

    The structure of your program is wrong.

    Instead of getNameIndex calling UserCheck() again, you should have getNameIndex return a bool – true on success, false on failure. Run it inside of a loop, something like this:

    bool success = false;
    
    while (!success)
    {
     cout << "Username: ";
     cin >> username;
    
     success = getNameIndex();
    }
    

    Also, instead of having global variables, you should pass them to the function. Something like:

    success = getNameIndex(username);

    and getNameIndex() shouldn’t do any I/O – the function that calls getNameIndex() should also be responsible for printing the error message. Imagine if you used getNameIndex() in a different context, such as when when the program is being run by another program or in an automated way – then printing to the console would be meaningless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can run my program outside of a jar file, but I want to
Where i can save string , and when program run, user can change it
I have a program that the user can switch the program language on run
I'm writing a program to be run from the Linux user space which spawns
I want to design a c# program which can run program a 3rd exe
There is a space in the string, but when I run program, it returns
I am trying to make a simple calculator program where a user can opt
Finally i am able to run super user commands in android program. Now i
I'm writing a Python program that runs on Linux server. It monitors some log
Can someone tell me exactly what the args argument should look like for sb-ext:run-program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.