Having troble using functions

Pages: 12
Really having problem with the function, I'm really new to function been trying to get it to work can someone help me out.
Having problems writing out to file in a function and calling the function.
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
#include<iostream> // 
#include<fstream> // need to read file
#include<string> // string name

using namespace std;

//functions
void CalcScore(double maxNumber,double minNumber,double Score1,double Score2,double Score3,double Score4,double Score5,double final_Score,char name,ofstream results);


int main()
{
    //declare variables
    ifstream starsearch;       //declare fstream to read from file
	ofstream results;
    string name;               //name of person
    int number;                //number of people to process
    int countWhile = 1;        //counter for primary while loop
    double Score1;             //Score number one
	double Score2;             //Score number two
	double Score3;             //Score number three
	double Score4;             //Score number four
	double Score5;             //Score number five
	double maxNumber;		   //max number
	double minNumber;		   //min number
	double final_Score;       //average

    //read data from file, "Scores.in"
    starsearch.open("starsearch.dat");
	results.open("results.dat");
    starsearch >> number;
   
    //loop to process number of people
	
    while (countWhile <= number){ // if count is greater than or equal to number stop
        //read more data from file
	   //input name and scores
        starsearch >> name;
       // cout << name << " ";
        starsearch >> Score1;	//Score number one
		starsearch >> Score2;	//Score number two
		starsearch >> Score3;	//Score number three
		starsearch >> Score4;	//Score number four
		starsearch >> Score5;	//Score number five
		// Display all scores read in
		// cout << Score1 << " " << Score2 << " " << Score3 << " " << Score4 << " " << Score5 << endl;
        CalcScore();
		// add one to countwhile
        countWhile = countWhile + 1;
        //cout << endl;
	}
	// close file
    starsearch.close();
    //cout << endl << endl;
    system("pause"); //Pause
    return 0; // end
}

// function
void CalcScore(double maxNumber,double minNumber,double Score1,double Score2,double Score3,double Score4,double Score5,double final_Score,char name,ofstream results)
{
		//Find Max
		maxNumber= max(Score1, Score2);
		maxNumber= max(maxNumber, Score3);
		maxNumber= max(maxNumber, Score4);
		maxNumber= max(maxNumber, Score5);

		//Find Min
		minNumber= min(Score1, Score2);
		minNumber= min(minNumber, Score3);
		minNumber= min(minNumber, Score4);
		minNumber= min(minNumber, Score5);

		//add all scores up - max and min / 3
		final_Score= (Score1 + Score2 + Score3 + Score4 + Score5 - minNumber - maxNumber) / 3;
		// display name and the finalAverage
		results << name << " " << final_Score << endl;




}

In your read loop, you need parameters in the CalcScore call. If CalcScore() calls like this all the values are 0 or undefined. Personally I am surprised it compiled.
Ok, sorry don't think I really understand about parameters. Can you give me an example?
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
// the call in your read loop would look like this filled with the variables I see in your code.
CalcScore(score1, score2, score3, score4, score5, name,  results);

// changes to the function
void CalcScore(double Score1,
                        double Score2,
                        double Score3,
                        double Score4,
                        double Score5, 
                        string name, 
                        ofstream results)
{
                //local variables don't go in function header/prototype
                double maxNumber;
                double minNumber;
                double final_Score;

		//Find Max
		maxNumber= max(Score1, Score2);
		maxNumber= max(maxNumber, Score3);
		maxNumber= max(maxNumber, Score4);
		maxNumber= max(maxNumber, Score5);

		//Find Min
		minNumber= min(Score1, Score2);
		minNumber= min(minNumber, Score3);
		minNumber= min(minNumber, Score4);
		minNumber= min(minNumber, Score5);

		//add all scores up - max and min / 3
		final_Score= (Score1 + Score2 + Score3 + Score4 + Score5 - minNumber - maxNumber) / 3;
		// display name and the finalAverage
		results << name << " " << final_Score << endl;
}


I hope you can see what I was trying to tell you. Don't forget to change the prototype the top of the program
Last edited on
OK, I see where I went wrong with the function but the call that you put up is sending me a undeclared identifier for score 1-5 but not one for the result.
Nevermind, my bad. the call is in low case "s" so i just changed to uppercase
Changed that then i get another error function doesn't take 7 arguments
I counted 7 in the code I posted, in both calling and definition.
Ok, still can't find why I keep getting a ERROR of function doesn't take 7 argument. Also is there anything wrong with my definition of CalcScore
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
#include<iostream> // 
#include<fstream> // need to read file
#include<string> // string name

using namespace std;

//functions
void CalcScore(double ,double ,double ,double ,double ,double ,double ,double ,char ,ofstream);


int main()
{
    //declare variables
    ifstream starsearch;       //declare fstream to read from file
	ofstream results;
    string name;               //name of person
    int number;                //number of people to process
    int countWhile = 1;        //counter for primary while loop
    double Score1;             //Score number one
	double Score2;             //Score number two
	double Score3;             //Score number three
	double Score4;             //Score number four
	double Score5;             //Score number five
	double maxNumber;		   //max number
	double minNumber;		   //min number
	double final_Score;       //average

    //read data from file, "Scores.in"
    starsearch.open("starsearch.dat");
	results.open("results.dat");
    starsearch >> number;
   
    //loop to process number of people
	
    while (countWhile <= number){ // if count is greater than or equal to number stop
        //read more data from file
	   //input name and scores
        starsearch >> name;
       // cout << name << " ";
        starsearch >> Score1;	//Score number one
		starsearch >> Score2;	//Score number two
		starsearch >> Score3;	//Score number three
		starsearch >> Score4;	//Score number four
		starsearch >> Score5;	//Score number five
		// Display all scores read in
		// cout << Score1 << " " << Score2 << " " << Score3 << " " << Score4 << " " << Score5 << endl;
        CalcScore(Score1, Score2, Score3, Score4, Score5, name, results);
		results << name << " " << final_Score << endl;
		// add one to countwhile
        countWhile = countWhile + 1;
        //cout << endl;
	}
	// close file
    starsearch.close();
    //cout << endl << endl;
    system("pause"); //Pause
    return 0; // end
}

// function CalcScore
void CalcScore(double Score1,double Score2,double Score3,double Score4,double Score5, string name, ofstream results)
{
                //local variables 
                double maxNumber;
                double minNumber;
                double final_Score;

		//Find Max
		maxNumber= max(Score1, Score2);
		maxNumber= max(maxNumber, Score3);
		maxNumber= max(maxNumber, Score4);
		maxNumber= max(maxNumber, Score5);

		//Find Min
		minNumber= min(Score1, Score2);
		minNumber= min(minNumber, Score3);
		minNumber= min(minNumber, Score4);
		minNumber= min(minNumber, Score5);

		//add all scores up - max and min / 3
		final_Score= (Score1 + Score2 + Score3 + Score4 + Score5 - minNumber - maxNumber) / 3;
		// display name and the finalAverage
		results << name << " " << final_Score << endl;

}
Your prototype specifies the function takes more than 7 arguments, but your definiton/call site both act as if it only takes 7.
OK, changed up the prototype to be 7, really don't know why I had so many double. it runs then i got a string error with name removed name out of the calcScore function and its fixed but now i got what looks like the one of the biggest problems error C2248. Guess I should make another thread? looks like it has to do with reading in the file.
Last edited on
closed account (zwA4jE8b)
what does your input data file look like?
Nvm, saw what it was having problem with. as I moved the result output out of the call function it had nothing to do in the function, so it just created problems. removed that and now the function only.

Current problem is a: The variable 'final_Score' is being used without being initialized. But i don't know where to initialize it.

input file:
2
Jennifer 10 9 8 9.5 10
Michael 10 9 10 9 10

Current 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
#include<iostream> // 
#include<fstream> // need to read file
#include<string> // string name

using namespace std;

//functions
void CalcScore(double ,double ,double ,double ,double, double);


int main()
{
    //declare variables
    ifstream starsearch;       //declare fstream to read from file
	ofstream results;
    string name;               //name of person
    int number;                //number of people to process
    int countWhile = 1;        //counter for primary while loop
    double Score1;             //Score number one
	double Score2;             //Score number two
	double Score3;             //Score number three
	double Score4;             //Score number four
	double Score5;             //Score number five
	//double maxNumber;		   //max number
	//double minNumber;		   //min number
	double final_Score;       //average
	

    //read data from file, "Scores.in"
    starsearch.open("starsearch.dat");
	results.open("results.dat");
    starsearch >> number;
   
    //loop to process number of people
	
    while (countWhile <= number){ // if count is greater than or equal to number stop
        //read more data from file
	   //input name and scores
        starsearch >> name;
       // cout << name << " ";
        starsearch >> Score1;	//Score number one
		starsearch >> Score2;	//Score number two
		starsearch >> Score3;	//Score number three
		starsearch >> Score4;	//Score number four
		starsearch >> Score5;	//Score number five
		// Display all scores read in
		// cout << Score1 << " " << Score2 << " " << Score3 << " " << Score4 << " " << Score5 << endl;
        CalcScore(Score1, Score2, Score3, Score4, Score5, final_Score);
		results << name << " " << final_Score << endl;
		// add one to countwhile
        countWhile = countWhile + 1;
        //cout << endl;
	}
	// close file
    starsearch.close();
    //cout << endl << endl;
    system("pause"); //Pause
    return 0; // end
}

// function CalcScore
void CalcScore(double Score1,double Score2,double Score3,double Score4,double Score5, double final_Score)
{
                //local variables 
                double maxNumber;
                double minNumber;
               

		//Find Max
		maxNumber= max(Score1, Score2);
		maxNumber= max(maxNumber, Score3);
		maxNumber= max(maxNumber, Score4);
		maxNumber= max(maxNumber, Score5);

		//Find Min
		minNumber= min(Score1, Score2);
		minNumber= min(minNumber, Score3);
		minNumber= min(minNumber, Score4);
		minNumber= min(minNumber, Score5);

		//add all scores up - max and min / 3
		final_Score= (Score1 + Score2 + Score3 + Score4 + Score5 - minNumber - maxNumber) / 3;
}
closed account (zwA4jE8b)
void CalcScore(double ,double ,double ,double ,double, double&); void CalcScore(double Score1,double Score2,double Score3,double Score4,double Score5, double &final_Score)

You need to pass by reference.
So i changed the prototype too the first code you posted and the function to the second you posted. And it works, thank you.!!! lol. Now I got to start on another function.
Quick question. Can you call a void function in a string function?
closed account (zwA4jE8b)
you can call any function in any function as long as all the parameters are defined and the scope is correct


1
2
3
4
5
6
7
8
9
10
void output(string name)
{
    cout << name << endl;
}

string callvoid()
{
    string name = "ME";
    output(name);
}
Last edited on
So I need to place the output to file in the function. I put it in and I can't get pass this: error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>

Code currently:
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
#include<iostream> // 
#include<fstream> // need to read file
#include<string> // string name

using namespace std;

//functions
void CalcScore(double ,double ,double ,double ,double, double&, string, ofstream);


int main()
{
    //declare variables
    ifstream starsearch;       //declare ifstream to read from file starsearch
	ofstream results;		   //declare ofstream to write to file results
    string name;               //name of person
    int number;                //number of people to process
    int countWhile = 1;        //counter for primary while loop
    double Score1;             //Score number one
	double Score2;             //Score number two
	double Score3;             //Score number three
	double Score4;             //Score number four
	double Score5;             //Score number five
	double final_Score;        //average
	

    //read data from file, "Scores.in"
    starsearch.open("starsearch.dat");
	results.open("results.dat");
    starsearch >> number;
   
    //loop to process number of people
	
    while (countWhile <= number){ // if count is greater than or equal to number stop
        //read more data from file
	   //input name and scores
        starsearch >> name;
       // cout << name << " ";
        starsearch >> Score1;	//Score number one
		starsearch >> Score2;	//Score number two
		starsearch >> Score3;	//Score number three
		starsearch >> Score4;	//Score number four
		starsearch >> Score5;	//Score number five
        CalcScore(Score1, Score2, Score3, Score4, Score5, final_Score, name, results);
		//results << name << " " << final_Score << endl;
		// add one to countwhile
        countWhile = countWhile + 1;
        //cout << endl;
	}
	// close file
    starsearch.close();
    //cout << endl << endl;
    system("pause"); //Pause
    return 0; // end
}

// function CalcScore
void CalcScore(double Score1,double Score2,double Score3,double Score4,double Score5, double &final_Score, string name, ofstream results)
{
                //local variables 
                double maxNumber;
                double minNumber;
               

		//Find Max
		maxNumber= max(Score1, Score2);
		maxNumber= max(maxNumber, Score3);
		maxNumber= max(maxNumber, Score4);
		maxNumber= max(maxNumber, Score5);

		//Find Min
		minNumber= min(Score1, Score2);
		minNumber= min(minNumber, Score3);
		minNumber= min(minNumber, Score4);
		minNumber= min(minNumber, Score5);

		//add all scores up - max and min / 3
		final_Score= (Score1 + Score2 + Score3 + Score4 + Score5 - minNumber - maxNumber) / 3;
		//output results
		results << name << " " << final_Score << endl;
}
closed account (zwA4jE8b)
your ofstream also need to be passed by reference.
EDIT: HOLD
Last edited on
Pages: 12