Hello. I am in a beginning C++ programming course in which we have just finished loops. I have been asked to plan, code, and execute a program to do the following things: Ask the user for the file name, then calculate an average of the numbers and display two lists of numbers, one of which will display the numbers less the average and the other displaying numbers greater than or equal to the average. The amount of numbers on the files is unknown. I have written the first part to input numbers from the file and to calculate an average but it does nothing and I can't figure out why. Any help whatsoever will be greatly appreciated.

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

using namespace std;

ifstream inData;
ofstream outData;

int avgcounter = 0;
float number, sum = 0, average;
char filename[256];

int main()
{
	cout << "Please enter the name of the file." << endl;
	cin >> filename;
	
	inData.open(filename);
	
	while(!inData.eof())
	{
		inData >> number;
		
		sum = number + sum;
		average = sum / avgcounter;
		avgcounter++;
		
	}
	
	cout << "The average is " << average << '.';
	
	return 0;
}

move line 26 down outside the loop to line 30. Its not necessary to calculate the average every time a number of read. Do it just once, after all numbers have been read, summed and counted.

Your program may do nothing at all if the file can not be opened. Add some code to check that

inData.open(filename);
if( !inData.is_open() )
{
   cout << "Failed to open the file " << filename << '\n';
   return 1;
}

Ok. I moved the line out of the while loop. But it is still doing the same thing. I added the code to check to see if the file is open and it doesn't display the message so I guess it's opening. The name of the data file that i'm trying to open is "List1". I type List1 in and the cursor moves to the next line and just blinks.... I do not know what to make of this. Any suggestions here?

Is "list1" the actual file name? No file extension such as "list1.txt"? And is that file in the current working directory (where the program is being executed)?

And did you make the changes correctly? I can't tell from here.

Wow. I am dumbfounded. I completely forgot the fact that I would have to type the extension. The extension, by the way, is ".dat". Sorry for posting such a simple problem. I will post the final code when I am finished. Thank you both for input.

Another quick question... Is there a way to use temporary files to hold the values to determine whether or not a value is greater than, less than, or equal to the average? If so, how do they work?

It would be easier to create a linked list (either vector or list header files) of the values when they are read in. Then all you have to do is run through the values in the list.

Why a linked list? What about just reading all the values into an array? Then you have everything you need right at your program-tips -- so to speak.

Why a linked list? What about just reading all the values into an array? Then you have everything you need right at your program-tips -- so to speak.

We have not yet started arrays. I believe after our test thursday we begin arrays, but this program is due thursday before the test. She, the professor that is, said it might be helpful to use temporary files, but I do not know anything about them. Do you have any suggestions with temporary files?

Do you know about regular files?

Temporary files are just regular files you use to hold data you need to temporarily save. They aren't special or anything.

Why a linked list? What about just reading all the values into an array? Then you have everything you need right at your program-tips -- so to speak.

The reason for linked lists is because he doesn't know how many numbers there will be. But that's a mute point if he doesn't know either lists or arrays yet.

To use temp files you would have to read the original file twice. The first time through you calculate the average. Then rewind the file, open two more files for output and begin reading the original. For each number read determine if it is lower or higher than the average. If lower write it out to file1, else if higher write it out to file2. When done, close all three files. Reopen the two temp files and display their contents.

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.