Calling Functions in main()

i have this project for my class that i am having a few syntax errors with.
the whole story of the project is to create an array from 3 numbers stored in a txt file. I need to pull the data from the file, display it, write the data to an array, organize them(#order) and re-write them to the txt file in that order. then display that order, find the sum of those numbers, average them & display them both. these projects all require a 3 file object oriented project with a *.h file, *.cpp file and a main.cpp file. all the function prototypes are in the *.h file, & all match the functions in the test.cpp file.

i feel comfortable with the functions i have written(but im not 100%), but i am having issues with the program actually reading/using those functions. even with a cout <<" "<< in the function, it doesnt display in the program.


"Test.cpp"--where the work of my code is.
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
  #include <iostream>
#include<fstream>
#include<string>
#include "Test.h"
using namespace std;

const int size = 100;
ifstream ifs;

int mainer()
{
	void TakeTrial();
	void SortNums(int);
	int AveNums(int);
	int Total(int);
	void write(int);
	void display(int);



	return 0;
}

void Testing::TakeTrial()
{
	
	ifs.open("trial.txt");//using the text file.
	
	for (int i = 0; i < 100; i++)//reading files into the array
	{
		while (i < 100 && ifs >> array[i])
			i++;
	}

	for (int i = 0; i < 3; i++)
	{
		cout << array[i] << endl;	
	}	
	ifs.close();
}

void Testing::SortNums(int array[])
{
	ifs.open("trial.txt");

	int temp;
	
	if (array[0] > array[1])
	{//swapping
		temp = array[0];
		array[0] = array[1];
		array[1] = temp;
	}
	if (array[1] > array[2])
	{
		temp = array[1];
		array[1] = array[2];
		array[2] = temp;
	}
	for (int i = 0; i < 3; i++)
	{
		cout << "the new order is given" << endl;
		cout << array[i];
	}
	ifs.close();
}

int Testing::AveNums(int array[])
{
	int average;
	int total = 0;
	for (int i = 0; i < 3; i++)
	{
		total += array[i];
	}
	average = total / 3;
	cout<<"the average is: " << average << endl;
	return average;
}

int Testing::total(int array[])
{
	int total = 0;
	for (int i = 0; i < 3; i++)
	{
		total += array[i];
	}
	return total;
}

void Testing::write(int array[])
{
	ofstream ofs;
	ofs.open("trial.txt");
	cout << "writing to file" << endl;
	for (int i = 0; i < 3; i++)
		ofs << array[i];
	ofs.close();

}

void Testing::display(int array[])
{
	cout << "Numbers" << endl;
	cout << array[0] << endl << array[1] << array[2] << endl;

}


this is the header file "Test.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef Test_H//this will be the name of the .cpp file and header file
#define Test_H// this will also be the name of the .cpp file and header file
class Testing
{
private:
	int array[100];

public:
	void TakeTrial();////list prororypes
	void SortNums(int array[]);
	int AveNums(int array[]);
	int total(int array[]);
	void write(int array[]);
	void display(int array[]);

};
#endif 


the only function that runs is the first one, displaying the numbers from the text file.
<3 look forward to hearing your advise
Thanks in advance :D
(If you need the main.cpp file, i can post it)
Hello dragonxalli,

Yes please post the main program because as it is what you have can not be compiled or run.

Also there is the possibility that the problem could start or be in main not where you may be thinking.

And include the "trial.txt" file or at least a sample of the file, 2 or 3 records should work, so everyone knows what you are working with.

Andy
mainer() does nothing. It contains some prototypes and returns 0. What is it supposed to do?

(If you need the main.cpp file, i can post it)
Yes, you should post the code where it happens...
Hello dragonxalli,

As coder777 mentions mainer() does nothing. It contains some prototypes. These are prototypes of member functions that have been declared in the header file.

In the header file you have
1
2
3
4
5
6
7
public:
	void TakeTrial();////list prororypes
	void SortNums(int array[]);
	int AveNums(int array[]);
	int total(int array[]);
	void write(int array[]);
	void display(int array[]);

Be consistent in your naming of the function names. I like to start a function name with a capital letter to help me understand that it is a function. I also start a class or struct name with a capital letter. Not saying it is write or wrong, but it is what i do.

Although my main function has nothing in it the program did compile for me.

Hope that helps,

Andy

P.S. int array[100]; this may work for now, but it would be better to make "array" a more meaningful name.
Last edited on
Hello dragonxalli,

As I started working with the program I noticed that the function "TakeTrial()" uses a for loop to read the file. This works, but could be a potential problem if you have less than 100 numbers. A while loop would work better reading less than 100 numbers or set up to read a maximum of 100 numbers.

You declare in your class:
1
2
3
4
5
6
void TakeTrial();////list prororypes
void SortNums(int array[]);
int AveNums(int array[]);
int Total(int array[]);
void Write(int array[]);
void Display(int array[]);

Looking at lines 2 - 6 each function has a parameter. Since these are member functions of the class they already have access to int array[100] as defined in the class. These parameters are expecting an array that is defined out side the class.

When I removed the parameter for the function "Display()" this function worked fine. Also I made a change to this function by putting the "cout" statement in a for loop. Like you I only printed part of the array for now.

Removing the parameter on the other functions in both the ".h" and ".cpp" files should make a big difference.

Hope that helps,

Andy
Topic archived. No new replies allowed.