read and store data into array

i think the void data is working properly.
void readstorefile and void printArr aren't working -- i dont think
now, i know how to create and print an array in one function, but i don't know how to pass in data file into an array in one function and then print the array in another

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;

const int SIZE = 10;
double arr[SIZE];
const string inputFile = "file.txt";
void data(const string inputFile, int& num);
void readStoreFile(const string inputFile, double arr[], int num);
void printArr(const double arr[], int num);

int main()
{
	int num;

	data(inputFile, num);

	return 0;
}

void data(const string inputFile, int& num)
{
	ofstream fout;
	fout.open(inputFile);

	for (num = 1; num <= rand() % SIZE; num++)
		fout << endl << num << "  " << rand() % SIZE;

	fout.close();

	cout << "done\n";
}

void readStoreFile(const string inputFile, double arr[], int num)
{
	ifstream fin;
	fin.open(inputFile);
	if (fin.fail())
	{
		cout << "ERROR\n";
	}
	else
	{
		int count = 0;
		while (!fin.eof())
		{
			fin >> arr[count];
			count++;
		}
		num = count;
		cout << "done\n";
	}
	fin.close();
}

void printArr(const double arr[], int num)
{
	ifstream fin;
	fin.open(inputFile);
	num = 1;

	cout << "\n\t\tThe array:\n" << "\t\t------------------------\n";

		while (!fin.eof())
		{
			double data;
			fin >> num >> data;
			cout << num++ << "  " << data << endl;
		}

	fin.close();
}
Last edited on
The argument "inputFile" is not needed, the variable is global and can be accessed by the function already.

In line 23, the argument for "num" (int& num) gives the function the variable by reference, meaning the actual variable in int main will change along with the one in the function. However, in line 36, you don't do that (int num). You seem to want to though, because you set "num" equal to count on line 52, but that change doesn't occur to the actual variable outside of that function.

but i don't know how to pass in data file into an array in one function and then print the array in another

Your "readStoreFile" function seems fine, but if something is wrong with the way you "fin" input, I wouldn't know since I don't know what the .txt looks like or how you're wanting to use the information. Otherwise though, it seems alright.

Your printArr function is what's troubling. I assume you want this function to print out the contents of the array? You don't need ifstream at all for that. All you're doing right now is reopening the file and outputting the information that was in it, you're NOT outputting the data as saved in your array in the previous function.

To output your array, do something like this:

1
2
3
4
for(int i = 0; i < 10; i++)
{
     std::cout << arr[i] << ' ';
}


And again, on line 58, you don't need to give arr as an argument, it already has access to that array since it was declared in the global scope.
Last edited on
@zapshe thanks for your reply! The data file would look something like this:

1 26.272
2 20.032
3 30.64
4 9.424
5 29.392

1-5 int is obviously the number and the double would be the array.

I tried the output printArr, and it gave me a bunch of zeros. So I'm trying to debug it as much as I could and see the problem.

UPDATE: now it shows me all of the numbers as just one array, both the 1-5 int and the double arr[], as follows:

1
26.272
2
20.032
3
30.64
4
9.424
5
29.392
Last edited on
Take a look at this code and the comments

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;

const int SIZE = 10;
double arr[SIZE];
const string inputFile = "file.txt";
void data(const string inputFile, int& num);
void readStoreFile();
void printArr();

int main()
{
	int num = 0;

	readStoreFile(); //CALL THIS FUNCTION TO GET DATA FROM .txt
	printArr(); //CALL THIS FUNCTION TO PRINT THE ARRAY

	return 0;
}

void data(const string inputFile, int& num)  //IS THIS FUNCTION NEEDED?
{
	ofstream fout;
	fout.open(inputFile);

	for (num = 1; num <= rand() % SIZE; num++)
		fout << endl << num << "  " << rand() % SIZE;

	fout.close();

	cout << "done\n";
}

void readStoreFile() //REMOVED UNNEEDED ARGUMENTS
{
	ifstream fin;
	fin.open(inputFile);
	if (fin.fail())
	{
		cout << "ERROR\n";
	}
	else
	{
		int count = 0;
		while (!fin.eof())
		{
			fin >> count; //INPUT INTEGER (due to .txt formatting)
			std::cout << count << ": ";
			fin >> arr[count]; //INPUT DOUBLE
			std::cout << arr[count] << '\n';
			//count++; WE CAN GET THE NUMBER DIRECTLY FROM THE .txt
		}
		//num = count; IF THEY'LL BE THE SAME VALUE THEN ONLY USE ONE VARIABLE
		cout << "done\n";
	}
	fin.close();
}

void printArr() //REMOVED UNNEEDED ARGUMENTS
{
	for (int i = 0; i < 10; i++) //PRINT OUT THE ARRAY
	{
		std::cout << arr[i] << ' ';
	}
}


^This code will take in the values from the .txt, save it into the array, then print out the array.


Understand that the way I did it makes it so the array starts at "1" rather than 0. The value in arr[0] is 0 . There are still 4 open slots after the last number that gets inputted, and those slots are also set to 0.
Last edited on
Just read your update. That's the intended outcome right? Glad you figured it out.
so the data function is supposed to create the data file with numbered randomized double values.

and the console outcome should mirror the data file which is something like num >> setw(10) >> arr[]
The data function is doing that from what I see. If the console should look the same as the .txt file, then simply output it again the same way as the you're outputting in "data" by replacing "fout" with "cout" (perhaps add it to the function or create a new function for it.)
Last edited on
Topic archived. No new replies allowed.