My code is supposed to take numbers from an .txt file, assign them variables, print out the numbers and THEN if the number is less than 25, add them up according to parity.

So, if the list of numbers is 1,2,3,4,5

It'll output: 1 2 3 4 5
Sum of even numbers: 6
Sum of odd numbers: 9

And, it needs to disregard any number larger than 25, not add it to either total and stop the loop when it gets to it.

Any help? Here's what I have so far

[#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int num1, num2, num3, num4, num5, num6, num7, num8; //Each number in the file
	ifstream infile;	
	int eventot=0, oddtot=0;	//Total counters for even and odd numbers


	infile.open("numlist.txt");	//Opens the text file with numbers

	infile>>num1>>num2>>num3>>num4>>num5>>num6>>num7>>num8; //Assigns numbers in list

	cout<<num1<<" "<<num2<<" "<<num3<<" "
		<<num4<<" "<<num5<<" "<<num6
		<<" "<<num7<<" "<<num8<<endl;	//Echos numbers in list to output screen
	infile.close();	//Force closes the file

while (infile)
{
	if(
}

	return 0;

}
]

Are you absolutely sure the file opens correctly? You need to test the return value of the file open to make sure it's really open.

Can you guarantee that there are exactly 8 values in the file? What if there are only 7? Or 10? You should probably loop, reading 1 number at a time while watching for EOF, and adding the numbers to an array instead of individual values.

How could I set up a loop that would take an x number of values from the file?

Would it just be

while (infile)
{
        if(n<25)
            if(n%2==0)
               add one to the even counter
       else if(n%2!=0)
             add one to the odd counter
}

?

And, what is the EOF?

EOF means end of file. You can check for it like this:

while(!infile.eof())

It looks to me like you have closed your file and then tried to read from it.

infile.close();	//Force closes the file

while (infile)
{
	if(

If the values were stored, I didn't see a point in it being open anymore. But, like Walt said, I need to rewrite it so it takes any number of values

This is frequently a bug waiting to happen:

while(!infile.eof())

because unless you are very sensitive to how you use it sooner or later you are going to end up with duplicate input of the last item in the file you are reading. It's because eof() doesn't return true until after you try to read beyond EOF, not when you find EOF, or something like that.

This is better:

while (infile)

Calling an input function like this is better yet.

int input;
while(infile >> input)

The value of input could be used on the fly or put into an array, or a vector or a list or some other container of your choice for later use or for use on the fly if you wish. Using this syntax, when the while loop ends you can evaluate using eof(). If that returns true it means the whole file was successfully read. If not, the file was contaminated somehow and input terminated before end of file was reached, and who knows why (you would have to evaluate the file some other way to figure it out).

Just so you know, this syntax will also leave the input stream in a failed state when the file ends no matter what happens to stop the loop, so if you want to reuse it for something else you need to clear it and put it back to whereever you want to go in the file to start over.

Oh, and you don't want to add one to a running total, you want the value of the input to a running total.

EOF means end of file. You can check for it like this:

while(!infile.eof())

You could, but it's not recommended unless you are verrrrrry careful. Here's why.

(feof() and .eof() operate identically)

Actually, I have never used while(!infile.eof()) { Sorry for giving not-so-great advice here. I haven't seen it used any other way though.

Using this syntax, when the while loop ends you can evaluate using eof().

How would you do that? With an if statement? while(infile >> input) { is a little confusing to me. What if you need to read things from the file into other variables besides input? Won't that mess it up? If not, why not?

Is there anything wrong with this: while(infile.good()) { ?

Every time you use an input statement, you should be checking the return status. Therefore, you should have no problem unless your input file is hosed.

What's the difference between while(infile.good()) and while(!infile.eof()) ? Aren't they (in this context) the same test?

>>How would you do that? With an if statement?

Yes.

>>What if you need to read things from the file into other variables besides input? Won't that mess it up? If not, why not?

No. You can always do multiple serial calls to other variables:

while(infile >> input)
{
    infile >> name >> rank >>  serialNumber;
    cout << name << '  ' << rank << ' ' << serialNumber << endl;
   infile >> nationality >> maritalStatus;
   cout << nationality << ' ' << maritalStatus << endl; 
}
if(infile.eof())
  cout << "file read successfully" << endl;
else
  cout << "unable to read complete file.  File may be corrupted" << endl;
cout << "either way, file stream is now in a failed state and will need to be cleared before used again" << endl;

or input could be a user defined type with an overloaded >> operator to read everything with one call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.