ifstream not reading everything

E

Eli Luong

Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("numbers.nbr");

int currNum = 0, totalSum = 0;
int count = 0;
double average;

// summation of everything
while (fin >> currNum);
{
cout << currNum << endl;
totalSum += currNum;
count++;
}

if (count > 0)
{
ofstream fout("avg.txt");
average = totalSum/count;
fout << average << endl;

cout << totalSum << "," << count << "," << average << endl;
}

//system("pause");
return 0;
}
 
J

Jim Langston

Eli Luong said:
Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("numbers.nbr");

int currNum = 0, totalSum = 0;
int count = 0;
double average;

// summation of everything
while (fin >> currNum);
{
cout << currNum << endl;
totalSum += currNum;
count++;
}

ifstream >> number stops when it hits the end of line. It won't get past
this until you force it to. What I generally do is add at the end of the
loop:

// Disgard rest of line
std::string temp;
getline( fin, temp );
 
E

Eli Luong

Jim said:
ifstream >> number stops when it hits the end of line. It won't get past
this until you force it to. What I generally do is add at the end of the
loop:

// Disgard rest of line
std::string temp;
getline( fin, temp );

Ah, I don't quite understand this. If each of the values of my input
are on a different line, then adding it at the end of the loop wouldn't
do any good, or did you mean to place it at the end of the while loop,
inside the while loop? I don't think this would do any good because on
the first iteration of the while loop, it's getting the last line and
skipping all of the other lines. I think I might be better off just
using getline and then converting the string into an int somehow. I'm
following this code from a textbook, and it doesn't seem to be working.
 
R

Rahul

Eli said:
Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("numbers.nbr");

int currNum = 0, totalSum = 0;
int count = 0;
double average;

// summation of everything
while (fin >> currNum);
{
cout << currNum << endl;
totalSum += currNum;
count++;
}

if (count > 0)
{
ofstream fout("avg.txt");
average = totalSum/count;
fout << average << endl;

cout << totalSum << "," << count << "," << average << endl;
}

//system("pause");
return 0;
}

while (fin >> currNum);
There is a semi colon at the end of "while()" which is causing the loop
to go on until the condition becomes false.

And when the loop exits currNum has the last value.

just remove this semi colon and it will work.
 
M

Marcus Kwok

Eli Luong said:
Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("numbers.nbr");

int currNum = 0, totalSum = 0;
int count = 0;
double average;

// summation of everything
while (fin >> currNum);

As Rahul has said, remove this semicolon and it should work.
{
cout << currNum << endl;
totalSum += currNum;
count++;
}

if (count > 0)
{
ofstream fout("avg.txt");
average = totalSum/count;

Note that here, since both totalSum and count are ints, then this will
perform integer division, which truncates. Since you are storing the
result in a double, I don't think this is what you want. You will need
to cast one of the arguments to get the correct result:

average = static_cast said:
fout << average << endl;

cout << totalSum << "," << count << "," << average << endl;
}

//system("pause");

Instead of using a system-dependant call like this, I usually do:

std::cout << "Press <Enter> to continue\n";
std::string trash;
std::getline(std::cin, trash);
 
R

Richard Herring

Jim Langston said:
ifstream >> number stops when it hits the end of line. It won't get past
this until you force it to.

.... which will be done automatically by the next call of fin >> currNum,
which will eat any leading whitespace including the newline character.

As others have pointed out, the problem here is not a sticky newline but
the superfluous semicolon.
What I generally do is add at the end of the
loop:

// Disgard rest of line
std::string temp;
getline( fin, temp );

If newline extraction really is needed, other possibilities are ignore()
and ws().
 
E

Eli Luong

Marcus said:
As Rahul has said, remove this semicolon and it should work.


Note that here, since both totalSum and count are ints, then this will
perform integer division, which truncates. Since you are storing the
result in a double, I don't think this is what you want. You will need
to cast one of the arguments to get the correct result:



Instead of using a system-dependant call like this, I usually do:

std::cout << "Press <Enter> to continue\n";
std::string trash;
std::getline(std::cin, trash);


Thanks to everyone who replied with input, it works now; did not see
that semi-colon after the while loop. This means it's unnecessary to
add the getline() function, too.

Just wondering, what's the benefit of using std::string, etc? I've read
it's unnecessary once you define the namespace std.
 
M

mlimber

Eli said:
Just wondering, what's the benefit of using std::string, etc? I've read
it's unnecessary once you define the namespace std.

I presume you mean "Why use std::string instead of just string with a
'using namespace std;' or 'using std::string;' above?" (If not, see
this FAQ on why to prefer std::string:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1.) In
that case, see this FAQ:

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

but know that there are different schools of thought on the matter. For
instance, Sutter and Alexandrescu say in _C++ Coding Standards_, item
59 (italics in original): "You can and should use namespace using
declarations and directives liberally /in your implementation files
after #include directives/ and feel good about it. Despite repeated
assertions to the contrary, namespace using declarations and directives
are not evil and they do not defeat the purposes of namespaces. Rather,
they are what make namespaces usable."

Cheers! --M
 
M

Marcus Kwok

Eli Luong said:
Just wondering, what's the benefit of using std::string, etc? I've read
it's unnecessary once you define the namespace std.

This is true that one you have "using namespace std;" you do not need to
qualify the names with std:: in front of them. However, be aware that
putting this in a header file is considered bad practice, since anyone
who includes your header has a greater chance of name collisions. Using
them in implementation files where you can control the scope of them can
be useful though.

The FAQ has some info on it:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top