Wrong values read into array from file?

This is my code

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

//Function Prototypes
void readfile(string, string );
float sqDiff(int[], int[], int);
float sum(int[], int);
float f(int [], int, int, int);

int main()
{
	
	int size=0;
	float setA[size];
	float setB[size];
	
	string filename1; //declare first file
	string filename2;//declare second file
	
	cout<<"Enter file 1 : ";
	getline(cin,filename1); //input name for first file

	cout<<"Enter file 2 : ";
	getline(cin,filename2); //input name for second file
	
	readfile(filename1,filename2); //read file 1 & 2 into array
	
	
	return 0;
}

void readfile(string filenameA, string filenameB)
{
	ifstream indata;
	ifstream indata2;
	
	indata.open(filenameA.c_str());
	indata2.open(filenameB.c_str());
	
	int num=0;
	int num2=0;
	
	int setA[num];
	int setB[num2];
	
	
	int sqdiff;
	
        //PROBLEM START
	while(!indata.eof()) 
	{
		indata>>setA[num];
		num++;
	}
	indata.clear();
	
	while(!indata2.eof())
	{	
		indata2>>setB[num2];
		num2++;
	}
	indata2.clear();
        //PROBLEM END
	if(num==num2)
	{
		sqdiff=sqDiff(setA, setB, num);	
		
		
	}
	
	else
	{
		cout<<"** Both arrays are not of the same size. Please try again! **";
	}
}

float sqDiff(int arr1[], int arr2[], int size)
{
	int squarediff[size];
	int addition1, addition2;
	float Formula;

	int diff[size];
	
	for(int i=0; i<size; i++)
	{
		diff[i]=arr1[i]-arr2[i];

		squarediff[i]=pow(diff[i],2);
	}
   
	addition1=sum(arr1,size);
	addition2=sum(arr2,size);
	Formula=f(squarediff, addition1, addition2, size);
	cout<<endl<<endl<<"Result: f= "<<Formula;
}

float sum(int array[], int ArraySize)
{
	int s=0;
	
	for(int i=0; i<ArraySize; i++)
	{
		s+=array[i];
	}
	
	return s;
}

float f(int SQDIFF[], int SUM1, int SUM2, int SIZE)
{
	int SumSQdiff=0;
	float MeanX;
	float MeanY;
	float AbsoluteDiff;
	float F;
	
	for(int i=0; i<SIZE; i++)
	{
		SumSQdiff+=SQDIFF[i];
	}


	MeanX=static_cast<float>(SUM1)/SIZE;

	MeanY=static_cast<float>(SUM2)/SIZE;

	AbsoluteDiff=fabs(MeanX-MeanY);

	F=(sqrt(SumSQdiff))/AbsoluteDiff;
	
	return F;
}


The first txt file
3 2 2 1 4 5 5 7

The second txt file
8 8 9 4 1 2 5 0

I tried to compare the number of integers from 2 txt files. If they have the same number of integer, then proceed. Else, program will display error.

I was able to successfully compare the number of integer. However, when I attempt to store the integers of 1st txt file to array setA[num] and store the integers of 2nd txt file to array setB[num]. It shows the following problem:

**Before the line (used to read 2nd txt to setB[num] )
1
2
3
4
5
while(!indata2.eof())
	{	
		indata2>>setB[num2];
		num2++;
	}


SetA[num] is still correct

setA[num] :
3 2 2 1 4 5 5 7

However after the line

setA[num] unexpectedly become

1 2 5 0 4 5 5 7

Why is this happening? Can somebody please tell me what is wrong? Any help is appreciated...
Last edited on
Variable length arrays are not allowed in standard C++. I get this compiler error (and others):
[Error] ISO C++ forbids variable length array 'setA' [-Wvla]


Also, you are trying to use arrays with a size of zero. That will lead to corruption of some other area of memory.

And as well as that, you use eof() as a loop condition. Best avoided.

Maybe try something like this - just an example, I didn't try to copy all your code.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int readfile(string name, int data[], int n );
void print(int data[], int n );

int main()
{
    const int size  = 100;
    
    int setA[size];
    int setB[size];
    
    
    int numA = readfile("data1.txt", setA, size);
    int numB = readfile("data2.txt", setB, size);
    
        
    print(setA, numA);
    print(setB, numB);
}


int readfile(string name, int data[], int size )
{
    ifstream indata(name.c_str());

    int count =  0;
    
    while (count < size && indata >> data[count])
        count++;
        
    return count;    
}

void print(int data[], int n )
{
    cout << "Size = " << n << '\n';      
    for (int i=0; i<n; ++i)
        cout << data[i] << ' ';
    cout << "\n\n";         
    
}


Note, I used a fixed array size of 100 elements, you may change that if necessary.

A better solution would be to use vectors, which can resize as more data is added.

Last edited on
Version using vectors:
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void readfile(string name, vector<int>& data);
void print(const vector<int>& data);

int main()
{
    vector<int> setA;
    vector<int> setB;


    readfile("data1.txt", setA);
    readfile("data2.txt", setB);

    print(setA);
    print(setB);
}


void readfile(string name, vector<int>& data)
{
    ifstream indata(name.c_str());

    int n;

    while ( indata >> n )
        data.push_back(n);
}

void print(const vector<int>& data)
{
    cout << "Size = " << data.size() << '\n';
    for (int i=0; i<data.size(); ++i)
        cout << data[i] << ' ';
    cout << "\n\n";
}
But, when I compile my code using dev c++, it can be compiled.

I have to use the 2 input txt because there are part of the question.

I am new to C++, I haven't learn about vectors yet
But, when I compile my code using dev c++, it can be compiled.

That's because there are non-standard extensions provided by that compiler. It's best not to depend on them because when you use a different compiler, your code will fail.

I have to use the 2 input txt because there are part of the question.

You should be able to adapt some of the code from my first example to make it fit your requirements. I just showed a basic example for you to base your own code upon.

I am new to C++, I haven't learn about vectors yet

Then consider the code sample I gave as a gentle introduction to the topic. You don't need to use them right now, but it is something worth knowing in the future.
Topic archived. No new replies allowed.