Function cannot read data from file into array

Function loadData won't read data into an array, I don't know why.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function of max

void loadData (int *arra, int &size)
{
	cout <<"Enter data file name:" << endl;
	string name;
	getline(cin, name);	
	ifstream inputFile;
	inputFile.open(name.c_str(), ios_base::in); // Open file

	if (inputFile.is_open())
	{
		cout <<"Finished loading data" << endl;
		int count;
		while (inputFile >> count)  // Count the numbers in the file
			size++;
		inputFile.clear();
		inputFile.seekg (0, ios::end);
		inputFile.seekg (0, ios::beg);
	
		// Allocates new memory
		int* temp = new int[size];
		// Copies values over to temp
		for (int* i = arra; i; i ++)
		*temp = *i;

		// Frees old memory
		 delete[] arra;

		// Points arra to the resized array
		arra = temp;
		//Close the file stream
		inputFile.close();
	}
	else 
	{
		cout <<"Cannot read the file!"<< endl;
	}
	
}

// Function of sorting data

void sort ( int a[], int &size )
{
    bool swapped;
    do
    {
          swapped = false;
          for ( int i = 1; i < size; i++)
         {
              if ( a[i - 1] > a [i] )
              {
                    int temp = a[i-1];
                    a[i-1] = a[i];
                    a[i] = temp;
                    swapped = true;
               }
           }
           size = size -1;
   }while ( swapped );
}

// Function of showwing data

void showData ( int a[], int &size)
{
	cout << "Data:" << endl;

	for ( int i = 1; i < size; i++)
		cout <<  a[i] <<" | "<< endl;
}

int displayMenu(int &menu)
{
	cout <<"---- Main Menu ----\n"<< endl;
	cout <<"1. Load data from file\n"<< endl;
	cout <<"2. Sort data\n"<< endl;
	cout <<"3. Display data\n"<< endl;
	cout <<"4. Exit\n"<< endl;
	cout <<"Your menu choice: "<< endl;
	cin >> menu;	

	while ((menu < 1) || (menu > 4)) 
	{
	
		if ((menu < 1) || (menu > 4)) 
		{
			cout <<"Error! "<< endl;
			cout <<"Your menu choice: "<< endl;
			cin >> menu;	
		}
	}
	cin.get();
	return menu;
}

int main()
{
	int isize=100;
	int *iarr = new int[isize];
	int choice = 0;		
	while (choice != 4)
	{
		displayMenu(choice);
		if (choice == 1)
		{
			loadData (iarr, isize);
		}
		else if (choice == 2)
		{
			sort (iarr, isize);
		}
		else if (choice == 3)
		{
			showData (iarr, isize);
		}
	}
	return 0;
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	if (inputFile.is_open())
	{
		cout <<"Finished loading data" << endl;
		int count;
		while (inputFile >> count)  // Count the numbers in the file
			size++;
		inputFile.clear();
		inputFile.seekg (0, ios::end);
		inputFile.seekg (0, ios::beg);
	
		// Allocates new memory
		int* temp = new int[size];
		// Copies values over to temp
		for (int* i = arra; i; i ++)
		*temp = *i;

		// Frees old memory
		 delete[] arra;

		// Points arra to the resized array
		arra = temp;
		//Close the file stream
		inputFile.close();
	}


You read all of the information in from the text file into the variable count, which I'm presuming is purely to count the number of numbers in the file (there is a much easier way to do this), increase the value of size, which is already started at 100 (See here: int isize=100;) And then you resize a new array, copy the values over (which you had none stored as of yet), then finish your function. You never actually write any of your values into your array.
closed account (D80DSL3A)
What does this snippet do?
1
2
3
4
5
// Allocates new memory
int* temp = new int[size];
// Copies values over to temp
for (int* i = arra; i; i ++)
	*temp = *i;

What stops i from incrementing indefinitely here (until i=NULL)?
Haha, I wrote that for the OP while I was dead tired, oops. I honestly can't think of how to fix that either since it's 6:33AM again and I have been drinking this time. -.-'
Topic archived. No new replies allowed.