user3429531
user3429531

Reputation: 355

getting information and matching it from text file c++

I am trying to do a program that creates new users. username and password will be needed to create this new users. if the username exist in the text file, the program will prompt that "there is a existing username and will be asked to key in another user name" the user name and password will be stored inside a text file.

assuming my text file(userandPassword) already has the following usernames and passwords

text file formatted by username password

joe abc
jane def

my problem with my codes is

if I key in joe the first time, the program will prompt"User Name existed!"

and if I key in jane after that, the program will prompt"User Name existed!"

BUT if I key in joe after that , the program will just assume that the username joe does not exist and prompts me to key in the password.

my output(fail attempt)

Enter Desired UserName: joe
User Name existed!
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
Enter Desired Password: 

desired output

Enter Desired UserName: joe
User Name existed!
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: bob
Enter Password: <---(password will only be prompted to key in if the username does not exist in the text file, otherwise it's will contiune to show "User Name existed" if username exist in text file)

This is my codes

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{

string line, userName,userNameInFile,password;
ofstream fout;
ifstream readFile("userandPassword.txt");
cout << "Enter Desired UserName: ";
cin >> userName;

while (getline(readFile, line)) {
    stringstream iss(line);
    iss >> userNameInFile;
    while (userNameInFile == userName)  {
        cout << "User Name existed!" << endl;
        cout << "Enter Desired UserName: ";
        cin >> userName;

    }
}

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

fout.open("userandPassword.txt",ios::app);
fout << userName <<  ' ' << password << endl;
// close file.
fout.close();
cout << "\nAccount Created and stored into TextFile!" << endl;
return 0;

}

I am not really sure what is causing it to be like this. Please help. Thanks.


updated answer*

string line, userName,userNameInFile,password;
ofstream fout;
vector<string> storeUserName;
ifstream readFile("userandPassword.txt");

while (getline(readFile, line)) {
    stringstream iss(line);
    iss >> userNameInFile;
    storeUserName.push_back(userNameInFile);
}

cout << "Enter Desired UserName: ";
do {
for (int i =0; i<storeUserName.size(); i++) {

        if (storeUserName[i] == userName) {
            cout << "Existing UserName Existed!\n";
            cout << "Enter Desired UserName: ";
        }

}
}while (cin >> userName);

Upvotes: 1

Views: 689

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409206

First you read the user "joe" from the file, and check if it matches with the username from the users input. It does, and you then ask for another username which is then "jane". It doesn't match, so the inner loop breaks and the outer loop continues. This loop reads the next username from the file, and it matches the one last entered by the user, so you ask the user for a new username. This new one doesn't match the current name from the file so the inner loop breaks, and the outer loop continues but it's at the end of the file so it breaks, and you create a user with an existing username.

This problem would have been very easy to find out if you stepped through the code in a debugger.

To solve it you might want to do it in two steps. First read the file into a collection, e.g. a std::vector containing structures with the usernames and passwords. Then after that you ask the user for the username, and look it up in the collection.

Upvotes: 3

rahulroy9202
rahulroy9202

Reputation: 2848

Read existing user names into a array and then match the input username to elements of the array.

If found, ask for different user name.

Else ask for Password.

Upvotes: 0

Related Questions