473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ifstream not reading everything

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

Jun 6 '06 #1
9 2515
"Eli Luong" <el******@gmail.com> wrote in message
news:11********************@g10g2000cwb.googlegrou ps.com...
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 );


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

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

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

Jun 6 '06 #2
Jim Langston wrote:

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.

Jun 6 '06 #3

Eli Luong wrote:
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.

Jun 6 '06 #4
mylea
3
surely,there is a semi colon in "while (fin >> currNum);".
Jun 6 '06 #5
Eli Luong <el******@gmail.com> wrote:
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<double>(totalSum) / count;
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);
return 0;
}


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 6 '06 #6
In message <j6*************@fe05.lga>, Jim Langston
<ta*******@rocketmail.com> writes
"Eli Luong" <el******@gmail.com> wrote in message
news:11********************@g10g2000cwb.googlegro ups.com...
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.


.... 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().

--
Richard Herring
Jun 6 '06 #7

Marcus Kwok wrote:
Eli Luong <el******@gmail.com> wrote:
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<double>(totalSum) / count;
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);
return 0;
}


--
Marcus Kwok
Replace 'invalid' with 'net' to reply

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.

Jun 6 '06 #8
Eli Luong wrote:
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-lit....html#faq-34.1.) In
that case, see this FAQ:

http://www.parashift.com/c++-faq-lit....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

Jun 6 '06 #9
Eli Luong <el******@gmail.com> wrote:
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-lit....html#faq-27.5

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
5
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
8
by: Jing Cheng | last post by:
Hi, I'm using ifstream reading data from a file, as following: ifstream finput("track.dat"); if(finput.fail()){ cerr << "Open input DATA file error!\n"; exit(-1);
3
by: sredd01 | last post by:
Hello, Please look the code below where I am reading the first 2,2,4 bytes from a binary file using two methods. I am getting a wierd (wrong) output with ifstream and memcpy method, but get the...
2
by: olig9 | last post by:
Hello, is there a way to feed an ifstream to a parser generated with flex and bison? I could see that flex wants a FILE* for reading, but I only have an ifstream (actually and ifstream*) for...
0
by: Chris | last post by:
I am reading in image files in a program and I read in the header in ascii mode. The problem is, sometimes tellg () gives me a completely incorrect result and sometimes it is just fine. Here is...
1
by: Chris | last post by:
I am reading in image files in a program and I read in the header in ascii mode and the data in binary mode. The problem is, sometimes tellg() gives me a completely incorrect result and sometimes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.