Frustrated! Issues with calling on functions, that need to use arrays?

Ok, so here are the 2 functions i needed to write for my code.
However i'm not sure what to return, or how to do the input???
And i'm having trouble calling on them from my main function.
Not getting any compiling error, except when trying to call on the function
and yes functions are referenced before main.
should I make it a class of functions instead??
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
.
.
.
	cout << int FREQ(int NELM[]);   /*tried to call on function immediatley
 below to no avail, tried with "int" without "int", technically this function
 should return an array. */
.
.
int FREQ(int array[]) /* Not sure how to call on this function either, 
because technically it does need input but it prompts the user for it*/

return 0;
.
.
int FREQ(int array[]) 
/*Not sure if this is the proper way to initialize this
 function, it will need an array, the actual array name is NELM[], it is a
 2D array created in the main function by pulling data from a file the user 
inputs. therefore the exact number of elements isn't known.*/

{
int freq1=0,freq2=0,freq3=0,freq4=0,freq5=0,freq6=0,freq7=0;
int freq[7]={freq1, freq2, freq3, freq4, freq5, freq6, freq7};

for(int i = 0; array[i] != '\0'; i++)
		if (array[i]=1)
			freq1+=1;
		else if (array[i]=2)
			freq2+=1;
		else if (array[i]=3)
			freq3+=1;
		else if (array[i]=4)
			freq4+=1;
		else if (array[i]=5)
			freq5+=1;
		else if (array[i]=6)
			freq6+=1;
		else 
			freq7+=1;
	
	
	return freq[7]; /* I would like to return a array called freq[] with 
7 elements from this function. However I'm not sure if I should return 
the array like this or the array location. and i'm not even sure how to 
go about it at this point. */
}

void RemoveBad() /*Used Void because don't need to return file. 
File is written and saved outside of c++ 
(Not sure how to put that we need an input file for the function, 
or if I even need that since it is prompted for???) */
{
	string testtxt;
  int array_size = 15360; // define the size of character array
	char * NELM = new char[array_size]; // allocating an array of 15kb
	int position = 0; //this will be used incremently to fill characters in the array 
  cout << "What is the name of the data file you would like to use, please be sure it is a .txt file, and include that in the name." << endl;
  getline(cin, testtxt);
	ifstream fin(testtxt.c_str()); //opening an input stream for file test.txt
 if(fin.is_open())
	{
    //file opened successfully so we are here
    cout << "File Opened successfully. Now reading data from file into array" << endl;
   		while(!fin.eof() && position < array_size)
		{
			fin.get(NELM[position]);
			position++;
		}
		NELM[position-1] = '\0'; 
			}
	else //file could not be opened
	{
		cout << "File could not be opened." << endl;
	}
	
	//Inputting the retrieved data
	ofstream fout("OnlyGood.txt");
  if(fout.is_open())
	{
    cout << "File Opened successfully. Now writing data from array to file" << endl;
    int NumBad = 0;

		for(int i = 0; NELM[i] != '\0'; i++)
		{
			if (NELM[i] >= 0)
				{
      				fout << NELM[i]; //writing ith character of array in the file
  				}
			else 
				{
					NumBad += 1;      	
				}
		}
    cout << "Array data successfully saved into the file OnlyGood.txt" << endl;
    cout << "However you had " << NumBad << "counts of data that did not fall between 0-7 NELM Value." << endl;
    
	}
else //file could not be opened
	{
		cout << "File could not be opened." << endl;
	}
	return ???;  // Not sure how to return a file, or if it should just be a file location, and then convert it back with the other function.
}
Last edited on
To return an array from a function you have to return a pointer. An array usually 'decays' into a pointer type so they are effectively the same in this case.

Therefore, function should look something more like this:

1
2
3
4
5
6
7
8
9
10
11
int* FREQ(int array[])
{

    int freq[7] = {0}; // array with 7 elements all initialised to 0

    for(int i = 0; array[i] != '\0'; i++)
        freq[array[i]-1]++;


    return freq;
}


I've also trimmed down some of your code in the function and replaced it with a much better way of doing things. By the way, in your loop you were using '=' instead of '==' to compare numbers. The single equals assigns values and the second one compares values, it's quite a common mistake to make actually.

And then to call your function you only need to write the name of the function and then its parameters, like this:
 
cout << FREQ(NELM);

Another thing, in your "RemoveBad" function you are trying to return a value when the type of function is a void which means it returns no value.

Hope this helps!
Last edited on
for the remove bad i initially had it as an int instead of void I was still playing around with the code. Not to mention I still have NO idea how to end the return, I am returning a file from the function. My brain is just dead at this point of the year. But thanks for some of it lol

I tried changing some of the code like to call the function, but no it's returning the address with
cout << FREQ(NELM);
I tried adding the & to get the array back but i'm still not retrieving the array

And sadly that was my main issuing when posting this was calling functions (both the ones I made) , i know it's simple but it's giving me issues.
Last edited on
I forgot to put this in my previous post but to retrieve a certain element of the array returned by the function you would do it in a similar way to a normal array.

Looks a bit weird:
cout << FREQ(NELM)[0]; // outputs the first element

Anyway, if you wanted to return from a void function you would just use 'return' on its own. But if you wanted to return a file, you would have to either return some kind of array containing the data or return the file stream.
@tom56785

Line 4 allocates a local array. That array goes out of scope when FREQ exits. Therefore, you can't return a pointer to it. Well you can, but the pointer will point to something that no longer exists.
Yeah, I just realised that error. You would have to either allocate an array onto the heap or pass a reference to an local array declared in your main function.
Topic archived. No new replies allowed.