//flex table opened by JP

Click to See Complete Forum and Search --> : Why is this happening?


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

fishybawb
01-14-2004, 08:08 AM
It must be something to do with the way you're getting input - I took your function exactly as it is, and got the names like this:


string name;

cout << "Name: ";
cin >> name;

while(!check_database(name))
{
cout << "Name on record, try another: ";
cin >> name;
}

// rest of processing...
cout << "Salary: ";


Your function works fine :)

nothing
01-14-2004, 09:43 PM
If I replace getline() with cin, when the user doesn't type anything and just presses enter, the cursor will go down one line and the user won't get the error message. :rolleyes:

fishybawb
01-15-2004, 09:25 AM
So don't replace it then:


string check;
char name[10];

cout << "Name: ";
cin.getline(name, '\n');

check = name;

while(!check_database(check))
{
cout << "Name on record, or bad input: ";
cin.getline(name, '\n');
check = name;
}

// rest of processing...
cout << "Salary: ";


Alternatively get some less stupid users. If there is such a thing ;) :p :D

nothing
01-15-2004, 03:00 PM
I tried doing it like this and it didn't work when I entered the name José.


#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <fstream>
using std::ifstream;

bool check_database(string);

int main()
{
string name;

cout << "Name: ";
cin >> name;

while(!check_database(name))
{
cout << "Name on record, try another: ";
cin >> name;
}

cout << "Salary: ";

return 0;
}

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;
}


:(

fishybawb
01-15-2004, 03:16 PM
Works fine for me. You might need to look at the text file you're using as a database - make sure it's proper ASCII (save it as .txt in Notepad), and doesn't contain any invisible "EOF" of other formatting characters that might be confusing things.

nothing
01-15-2004, 03:20 PM
When you tested it, are you sure you entered the name José instead of Jose?

fishybawb
01-15-2004, 03:27 PM
Yes, José works :t

nothing
01-15-2004, 03:33 PM
What am I doing wrong then? :mad:

fishybawb
01-15-2004, 05:54 PM
I don't know :( Want me to email you the database.txt I was using for the test?

nothing
01-15-2004, 06:42 PM
Yes, please. I will pm you with my e-mail address.

:t

nothing
01-15-2004, 08:38 PM
It didn't work either :(

What version of Windows are you running?

fishybawb
01-16-2004, 10:44 AM
XP Pro... Maybe it's got something to do with the character set installed. Sorry, I don't really know how to fix this one :(

Dark_Raver
01-17-2004, 03:57 AM
Can't be sure about what might be wrong. Almost seems like a compiler setting or something that makes the code behave so little differently for you guys.

I do have some suggestions that you might give a try though.

try clearing the string every time before you get input.
try clearing the name_on_database string each time as well.
try using strcmp to compare the strings as opposed to ==


possibly a good way to see what is going on would be to get output of both strings right before comparison. that could show you if things are as they should be.


its been a while for me so im not sure but isn't there something you can do to force the string to use a specific ASCII set. since é is an extended ASCII character the string name might not be able to store it properly if you use a name with standard ASCII characters first.


just some ideas to look at.



DR