cannot provoke function

Can any one check this program for me? It won't run function loadData properly.


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
#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.close();

		//Create an input file stream
		inputFile.open(name.c_str(), ios_base::in);

		int number;  //Variable to hold each number as it is read
	
        //Read number using the extraction (>>) operator
		for ( int i = 0; i< size; i++ )
			inputFile >> arra[i];

		//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)) 
	{
		cout <<"Error! "<< endl;
		cout <<"Your menu choice: "<< endl;
		cin >> menu;		
	}
	return menu;
}

int main()
{
	int isize=0;
	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
	int isize=0;
	int *iarr = new int[isize];


You're creating an int array with a size of 0, effectively making iarr point to NULL...

1
2
3
// From loadData()
		for ( int i = 0; i< size; i++ )
			inputFile >> arra[i];


Now you're trying to write to a non existent location...
The size changed when I use this? Right?

1
2
3
int count;
		while (inputFile >> count)  
			size++; 
No, you need to reallocate memory for your array, then copy the elements over as well.
I don't quite understand, could you give me an example?
1
2
3
4
5
6
7
8
9
10
11
// 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;


I'm tired, I'm still not great with pointers, and I haven't tested it, but in theory, that should work. I don't know of an easier way to do it while still using arrays.

Note: There are no new values, aside from the ones in the random memory when allocating the memory for the array (meaning you might have some garbage values in the array until you assign those values something.

If you're looking for something a little more dynamic, please consider using vectors, they're much easier to think about and to use.
Last edited on
Thank you, but I don't think it is the problem.
When I do like this, the function run
1
2
3
4
5
6
7
8
9
int main()
{
	int isize=0;
	int *iarr = new int[isize];
	int choice = 0;

			loadData (iarr, isize); 

	return 0;

however, if I put it in the code (like the code above) it won't run properly.
May be the problem is the loop
Last edited on
You need to specify what's not going right. When I see it doesn't run properly, I assume it doesn't run at all. If you're getting errors, you need to share them, if you're getting some odd results, you need to share them. It also helps if I know what kind of output you're expecting, and what your output is.

If you use your function the way you are now, you're going to end up trying to write to memory that you're not allowed to and crashing your program. I simply showed you a way to fix that. However, you need to implement that properly into your code before I can help you resolve any other issues, since writing to unallocated memory can cause a lot of issues.
Topic archived. No new replies allowed.