Mar 29, 2016 at 12:56am UTC
Hello folks, i'm a newbie herein this forum.
I'm using QT Creator in compiling my work.
i can't seem to read the next line of my file, whenever i switch username, password and fullname:
1 2 3
username;password;fullname
user1;1234;reese reese's
user2;123456;snicky snickers
here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
std::ifstream inFile("/signup.txt" );
char str[255];
std::string uname;
std::string pword;
QString username = ui->lineEditUsername->text();
inFile.getline(str,255,';' );
uname = str;
QString password = ui->lineEditPassword->text();
inFile.getline(str,255,';' );
pword = str;
if (uname == username.toStdString() && pword == password.toStdString())
{
std::string fullname;
std::string line;
inFile.getline(str,255,'\n' );
fullname.append("Welcome " );
fullname.append(str);
ui->label->setText(fullname.c_str());
while (std::getline(inFile, line))
{
line->push_back(line);
}
} else
{
ui->label->setText("Invalid login." );
}
inFile.close();
Is there something that i should do to make my code work?
Last edited on Mar 29, 2016 at 12:59am UTC
Mar 29, 2016 at 9:57am UTC
I already tried your suggestion on line 8 and 11 to use the std::string version of getline it only says:
'std::getline' is not a class member
=====================
1 2 3
username;password;fullname
user1;1234;reese reese's
user2;123456;snicky snickers
and without altering line 8 and 11, your suggested while loop only reads the username and password correctly, meanwhile, the 3rd part (names) it skips to the last line and reads only snicky snickers
"fullname"and "reese reese's" can't be read. Anyway, thanks. (my code reads any line of the file now.)
EDIT: I just found out that setting while to check if username and password matches, it assumes all line are true even if either username and password is blank.
Last edited on Mar 29, 2016 at 11:46am UTC
Mar 29, 2016 at 1:38pm UTC
I did include the <string> header file.
here's the code so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void MainWindow::on_pushButtonLogin_clicked()
{
//checks for a match in each entry on the same line
bool isMatch = false ;
std::ifstream inFile("/signup.txt" );
char str[255];
std::string uname;
std::string pword;
std::string fullname;
QString username = ui->lineEditUsername->text();
inFile.std::getline(str,255,';' );
uname = str;
QString password = ui->lineEditPassword->text();
inFile.std::getline(str,255,';' );
pword = str;
while (std::getline(inFile, uname, ';' ) && std::getline(inFile, uname, ';' ) && std::getline(inFile, fullname))
{
if (uname == username.toStdString() && pword == password.toStdString())
{
//this->hide(); //OPTIONAL::this will hide the login window
//won't show welcome <fullname> label.
isMatch = true ;
break ;
}
}
if (isMatch)
{
//outputs the correct name of the user.
ui->label->setText(("Welcome " + fullname).c_str());
//will open a new window after successfull login
PeriodicTable elements;
elements.setModal(true );
elements.exec();
} else
{
ui->label->setText("Invalid login." );
}
inFile.close();
}
line 13 and 16 says it is not a class member, even if <string> header file is included.
Last edited on Mar 29, 2016 at 2:01pm UTC
Mar 29, 2016 at 1:59pm UTC
Line 19: Change the second uname
-> pword
while (std::getline(inFile, uname, ';' ) && std::getline(inFile, pword , ';' ) && std::getline(inFile, fullname))
Mar 29, 2016 at 2:25pm UTC
Thank you sir coder777 for that.
Now isMatch = true inside the if statement doesn't work, it always output an invalid login in every user.
http://i.imgur.com/eVWaPAM.png
Last edited on Mar 29, 2016 at 2:25pm UTC
Mar 29, 2016 at 2:34pm UTC
I checked it with the changes and it works as expected.
Did you check whether the file could be opened?
Last edited on Mar 29, 2016 at 2:43pm UTC
Mar 29, 2016 at 2:46pm UTC
now i'm confused
http://i.imgur.com/4eSdj29.png
EDIT:
i put some minor changes, noticed that i removed char and changed to string
but still if(isMatch) doesn't work properly, it will jump to invalid login instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
void MainWindow::on_pushButtonLogin_clicked()
{
//checks for a match in each entry on the same line
bool isMatch = false ;
std::ifstream inFile("D:/signup.txt" );
//char str[255];
std::string str;
std::getline(inFile, str);
std::string uname;
std::string pword;
std::string fullname;
QString username = ui->lineEditUsername->text();
uname = str;
QString password = ui->lineEditPassword->text();
pword = str;
while (std::getline(inFile, uname, ';' ) && std::getline(inFile, pword, ';' ) && std::getline(inFile, fullname))
{
if (uname == username.toStdString() && pword == password.toStdString())
{
//this->hide(); //OPTIONAL::this will hide the login window
//won't show welcome <fullname> label.
isMatch = true ;
break ;
}
}
if (isMatch)
{
//outputs the correct name of the user.
ui->label->setText(("Welcome " + fullname).c_str());
//will open a new window after successfull login
PeriodicTable elements;
elements.setModal(true );
elements.exec();
} else
{
ui->label->setText("Invalid login." );
}
inFile.close();
}
yes, the file can be open, i put
1 2 3 4 5
if (inFile.is_open())
{
std::cout << "File was created successfully!" << endl;
}
else std::cout << "Unable to open your file." ;
and the compiler issued,
std::cout << "File was created successfully!" << endl;
will always evaluate to true, so i guess it is accessible right?
Last edited on Mar 29, 2016 at 3:13pm UTC
Mar 30, 2016 at 7:52am UTC
I did not test it with the qt stuff (just with console). You need to debug in order to figure out where it is going wrong.