Populating an array with data from in ifstream

So I am working on a class lab and I am pulling a bunch of doubles from an infile and storing them into an array. I initially had this working but not to my professors liking/instruction guidelines.

I know there are 13 values in the infile but I have to set the array size to 20.

I need to set a sentinel (-99) to stop the loop and keep track of how many items were read in the function and not use eof like I currently have.

I'm pretty confused how to go about this. My initial code was working and I created the rest of the lab using it so is there anyway to alter this to keep the rest of it working while meeting those guidelines?

Here is what I have/need to change.

Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main()
{   int index = 13;
    double sales[index],
           sum = 0;


    ifstream inFile;
    ofstream outFile;

    open_files(inFile, outFile);

    sales_data(index, sales, inFile);

    total_sales(index, inFile, outFile); 

    minmax_amount(index, sales, inFile, outFile);

    sales_sort(index, sales, inFile, outFile);

    sales_sum(index, sales, sum, inFile, outFile);

    inFile.close();
    outFile.close();

    return 0;
}

void sales_data(int index, double sales[], ifstream& inFile) //storing data in an array
{
    if(!inFile.eof())
    {
        for(int i = 0; i < index; i++)
        {
            inFile >> sales[i];
        }
    }
}
Last edited on
When loading files into memory, I prefer to localise filestreams inside a function. Alongside this, when dealing with anything reading/writing to files it's better to not throw any potential results into the void.

Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Make a function to handle file loading
// Takes 4 arguments
// - Filename
// - Array
// - Array size
// - Sentinel number
int StoreFileInArray(std::string fn, double array[], const int& array_size, const double& sent)
{
	std::ifstream ifs(fn); // Open the file
	if(!ifs) return -1; // File could not be opened
	double next = 0; // Create a var to store next number
	int count = 0; // Create a var to keep track
	ifs >> next; // Get the next number

	while((next != sent) && count < array_size) // While the next number isn't the sentinel AND count is less than the array size
	{
		array[count] = next; // Add the next number to the array
		count++; // Increase the count
		ifs >> next; // Get the next number
	}
	return count; // Return the count
	// ifstream will close itself when it goes out of scope
}


Write what you are trying to accomplish as small documented steps, each step bringing you closer toward a solution. This function opens a file handle, stores it's results in an array passed to it while only saving numbers up to the given array size or sentinel.

When you begin writing functions like this, it's much simpler to write small chunks of code to accomplish tasks:

1
2
3
4
5
6
7
bool SaveArrayToFile(std::string fn, double array[], const int& array_size)
{
	std::ofstream ofs(fn); // Open the stream
	for(int n = 0; n < array_size; n++) // Save the contents
		ofs << array[n] << " ";
	return true; // Return the result
}



These smaller utility functions should be robust in there functionality while allowing the main function to still express it's intent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	const int array_size = 20; // Set the array size;
	double sales[array_size]; // Make the array
	double sentinel = -99; // Number to show end

	int count = StoreFileInArray("receipt.txt", sales, array_size, sentinel); // Call the function and store the count

	if(count == -1) // Check for input error
	{
		std::cout << "File could not be opened.\n"; // Say what went wrong
		return -1; // Exit the program
	}
	else std::cout << "Numbers read: " << count << "\n"; // Say how many were read

	double sum = 0; // Make a var to store accumulation
	sum = Accumulate(sales, count); // We know how many to read from our previous function call
	std::cout << "The sum is " << sum << "\n";
	return 0;
}



This code has not been compiled and should only be used as a guide.

- Arrays: http://www.cplusplus.com/doc/tutorial/arrays/
- Functions: http://www.cplusplus.com/doc/tutorial/functions/
- File I/O: http://www.cplusplus.com/doc/tutorial/files/

This was super helpful man thank you so much!
Topic archived. No new replies allowed.