So I added identical code to my project and the 2nd for loop won't print out "testing". userandPwNumb value is the same for both for loops. I tried cin.clear() and cin.sync() to see if that would some how help but it did not.
c = '0' + userAndPwNumb;
for (std::string line; std::getline(DataIn, line);){
if (sizeof(line)){
if (line[0] == c){
++numberOfUserBalancesCounter;
cout << "line[0] of line = " << line[0];
}
}
}
for (std::string line; std::getline(DataIn, line);){
if (sizeof(line)){
if (line[0] == c){
++numberOfUserBalancesCounter;
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
As the TheToastersaid you need to post enough ,or all, of the program that can be compiled and tested along with the input file, or a a good sample if large, so that everyone can use the same information.
Based on what you did post I would say the the first for loop is reading the file until the "eof" bit is set. So in the second for loop the file stream is unusable.
Try adding
1 2
DataIn.clear(); // <--- Resets the state bits on the file stream.
DataIn.seekg(0); // <--- Positions the file pointer to the beginning of the file.
This works, the first seek I have inserted isn't actually required because the file is all set to go but your testing and series of file reads lends itself to a function and there's no harm in going to the start even if it's already there.
#include <iostream>
#include <fstream>
int main()
{
// OPEN THE FILE
std::ifstream DataIn ("data_in.txt");
if (DataIn.is_open())
{
std::cout << "Enter password: (Use 1234) : ";
int userAndPwNumb; // 1234
std::cin >> userAndPwNumb;
std::string c = std::to_string(0) + std::to_string(userAndPwNumb); //01234
std::string password;
std::getline(DataIn, password);
if(password == c)
{
std::cout << "Ready to go!\n";
}
else
{
std::cout << "Password failed.\n";
DataIn.close();
return -95;
}
}
else
{
std::cout << "Unable to open file";
return -99;
}
constint LIMIT{5};
std::string line[LIMIT];
int no_balances{0};
DataIn.seekg(std::ios::beg);
while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
{
std::cout
<< "line[" << no_balances << "] = "
<< line[no_balances] <<'\n';
++no_balances;
}
// AGAIN
DataIn.seekg(std::ios::beg);
no_balances = 0;
while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
{
std::cout << "testing AGAIN\n";
++no_balances;
}
// AND AGAIN
DataIn.seekg(std::ios::beg);
no_balances = 0;
while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
{
std::cout << line[no_balances]
<< " *** TESTING and again ***\n";
++no_balances;
}
DataIn.close();
return 0;
}
Enter password: (Use 1234) : 1234
Ready to go!
line[0] = 01234
line[1] = Dear Russians, very
line[2] = history. The year 200
line[3] = all measured this date
line[4] = then after we grew up
testing AGAIN
testing AGAIN
testing AGAIN
testing AGAIN
testing AGAIN
01234 *** TESTING and again ***
Dear Russians, very *** TESTING and again ***
history. The year 200 *** TESTING and again ***
all measured this date *** TESTING and again ***
then after we grew up *** TESTING and again ***
Program ended with exit code: 0
And the data_in.txt file - I suspect your data file isn't the same.
01234
Dear Russians, very
history. The year 200
all measured this date
then after we grew up
mothers would be, and
the extraordinary New
today I am wishing
all. Today I am addres
made a decision. I hav
of the outgoing centur
Yeltsin will try to ho
anyone. That is all li