nothing
01-13-2004, 10:03 PM
I have a file called database.txt with the following lines on it:
Carlos
Junior
Tiago
Kleiton
José
I wrote a function that checks whether the name entered by the user is already registered or not, and I am having a problem with it. When I run my program, this is what happens....
Name: Carlos
Carlos is already registered. Enter another name: Junior
Junior is already registered. Enter another name: Tiago
Tiago is already registered. Enter another name: Kleiton
Kleiton is already registered. Enter another name: José
Salary:
José is already registered, but my program doesn't "see" that. Now here is the funny part:
Name: José
José is already registered. Enter another name:
If I enter the name José first, my program works lol.
Name: Carlos
Carlos is already registered. Enter another name: José
Salary:
See?
Here is the function I wrote:
bool check_database(string name)
{
string name_on_database;
ifstream database("database.txt", ios::in);
if(!database) {
cout << "Error opening file.\n";
cin.get();
exit(1);
}
while(!database.eof()) {
std::getline(database, name_on_database);
name_on_database = name_on_database.substr(0, name.length());
if(name == name_on_database)
return false;
}
return true;
}
Does anybody see what is wrong with it? Thank you :t
Carlos
Junior
Tiago
Kleiton
José
I wrote a function that checks whether the name entered by the user is already registered or not, and I am having a problem with it. When I run my program, this is what happens....
Name: Carlos
Carlos is already registered. Enter another name: Junior
Junior is already registered. Enter another name: Tiago
Tiago is already registered. Enter another name: Kleiton
Kleiton is already registered. Enter another name: José
Salary:
José is already registered, but my program doesn't "see" that. Now here is the funny part:
Name: José
José is already registered. Enter another name:
If I enter the name José first, my program works lol.
Name: Carlos
Carlos is already registered. Enter another name: José
Salary:
See?
Here is the function I wrote:
bool check_database(string name)
{
string name_on_database;
ifstream database("database.txt", ios::in);
if(!database) {
cout << "Error opening file.\n";
cin.get();
exit(1);
}
while(!database.eof()) {
std::getline(database, name_on_database);
name_on_database = name_on_database.substr(0, name.length());
if(name == name_on_database)
return false;
}
return true;
}
Does anybody see what is wrong with it? Thank you :t