Issue understanding Modular programming

Pages: 12
So, I recently had a homework assignment where I was supposed to drop the lowest grade and give an average of the top 4 grades, but with twist. Instead of input from cin it will be input for a text file called grades.txt. Below is my code and under the code will be my errors.
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
#include <iostream>
#include <fstream> //needed to input files
#include <string>  //needed for string input
using namespace std;

//Function prototype.
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades);
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5);
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5);
void open(ifstream&);
void didnotopen(ifstream&);

int main()
{
	ifstream myfile;


	//open file
	open(myfile);
	//get score
	getScore();      //This where I am having an issue. What should I put in ()?
}
//****************************************************************************
//open file
//****************************************************************************
void open(ifstream& myfile)
{
	myfile.open("grades.txt");

	didnotopen(myfile);
}
//****************************************************************************
//did the file open
//****************************************************************************
void didnotopen(ifstream& myfile)
{
	if (!myfile)
	{
		cerr << "\n Error: Unable to read file." << endl;
		exit(10);
	}
}
//****************************************************************************
//GetScore function
//****************************************************************************
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades)
{
	{
		while (myfile >> grades)
		myfile >> grade1;
		myfile >> grade2;
		myfile >> grade3;
		myfile >> grade4;
		myfile >> grade5;

		cout << grade1 << endl;
		cout << grade2 << endl;
		cout << grade3 << endl;
		cout << grade4 << endl;
		cout << grade5 << endl;
	}
}
//*********************************************
//Find lowest function
//*********************************************
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	int lowest = grade1;

	if (grade2 < lowest)
	{
		lowest = grade2;
	}
	else if (grade3 < lowest)
	{
		lowest = grade3;
	}
	else if (grade4 < lowest)
	{
		lowest = grade4;
	}
	else if (grade5 < lowest)
	{
		lowest = grade5;
	}
	return lowest;
}	
//*********************************************
//CalcAverage
//*********************************************
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	double gpa;
	int lowest = findLowest(grade1, grade2, grade3, grade4, grade5);
	gpa = ((grade1 + grade2 + grade3 + grade4 + grade5) - lowest) / 4.0;
	cout << "The grade point average is " << gpa << endl;
}

 

Errors I am getting
Severity Code Description Project File Line Suppression State
Error (active) too few arguments in function call project5
Error C2660 'getScore': function does not take 0 arguments project5


closed account (48T7M4Gy)
getScore(...) has a series of parameters that must be provided for the function to work. See line 40.

So they have to be provided in the function call at line 21
Ok, I see you mentioned line 40 which is exit(10); should the value be bigger. As for the series of parameters. I have seen example like (int grades), but when I try this I get "int" is illegal. Should it look like
getScore(int grade1);
getScore(int grade2);
etc....

?
closed account (48T7M4Gy)
Sorry I meant Line 46
closed account (48T7M4Gy)
You have to pass the list of parameters to the function as specified in brackets, not the way you have suggested it should look like. I'm surprised you don't realise that after writing the function.
Have you learned about arrays or vectors? They will make this assignment much easier.
Kemot, so your saying it should be

I under stand this is needed for protocol, but does it really need to be in three places?

getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades)
Dhayden,
No we have not learn vectors or arrays. I believe that is the next chapter which I am currently reading, but I wait to make sure I full understand the last chapter, so this is why I am asking for help.
closed account (48T7M4Gy)
If you haven't learned arrays etc forget about them because the principle you need to be clear on is the parameter list in the function vs the parameter list when you call the function. When you call getScore you need to write getScore (my file, grade1 etc) and not getScore ( ifstream myfile, int etc)

Note that your line 21 which is the third place must include the parameters and you must specify what value they take otherwise the function won't have a schmick about what they are. You must pass the values to the function for processing inside the function. Effectively you have to fill in the blanks and the function does the rest.

BTW Where arrays come in is you can combine all the grades into one group and eliminate repetitions like grade1, grade2 etc. But that comes later. :)
Last edited on
Ok, so I when I call the function it should look like
getScore(myfile, grade1, grade2, grade3, grade4, grade5)
This will get each grade from the void getScore function and will pass it to the calAverage and than calAverage will plug the values in and call lowest score to get the lost score the drop. I just want to make sure I understand.
closed account (48T7M4Gy)
Yep, run the program and let me know whether it runs (of course) and whether the output is what you intended.
#include <iostream>
#include <fstream> //needed to input files
#include <string> //needed for string input
using namespace std;

//Function prototype.
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades);
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5);
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5);
void open(ifstream&);
void didnotopen(ifstream&);

int main()
{
ifstream myfile;


//open file
open(myfile);
//get score
getScore(myfile, grade1, grade2, grade3, grade4, grade5); //This where I am having an issue. What should I put in ()?
}
//****************************************************************************
//open file
//****************************************************************************
void open(ifstream& myfile)
{
myfile.open("grades.txt");

didnotopen(myfile);
}
//****************************************************************************
//did the file open
//****************************************************************************
void didnotopen(ifstream& myfile)
{
if (!myfile)
{
cerr << "\n Error: Unable to read file." << endl;
exit(10);
}
}
//****************************************************************************
//GetScore function
//****************************************************************************
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades)
{
{
while (myfile >> grades)
myfile >> grade1;
myfile >> grade2;
myfile >> grade3;
myfile >> grade4;
myfile >> grade5;

cout << grade1 << endl;
cout << grade2 << endl;
cout << grade3 << endl;
cout << grade4 << endl;
cout << grade5 << endl;
}
}
//*********************************************
//Find lowest function
//*********************************************
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5)
{
int lowest = grade1;

if (grade2 < lowest)
{
lowest = grade2;
}
else if (grade3 < lowest)
{
lowest = grade3;
}
else if (grade4 < lowest)
{
lowest = grade4;
}
else if (grade5 < lowest)
{
lowest = grade5;
}
return lowest;
}
//*********************************************
//CalcAverage
//*********************************************
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
double gpa;
int lowest = findLowest(grade1, grade2, grade3, grade4, grade5);
gpa = ((grade1 + grade2 + grade3 + grade4 + grade5) - lowest) / 4.0;
cout << "The grade point average is " << gpa << endl;
}
So I put this in and I get identifier grade-grade5 undefined.
Do I need to put int grade1- grade1 inside int main()?
closed account (48T7M4Gy)
Copy and paste the exact error message
closed account (48T7M4Gy)
In the abscence of error feedback, there are a number of errors and this overcomes most up to a point so replace main() with this:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	ifstream myfile;
	int grade1 = 0,  grade2 = 0,  grade3 = 0,  grade4 = 0,  grade5 = 0;
	string grades;
	//open file
	open(myfile);
	//get score
	getScore( myfile,  grade1,  grade2,  grade3,  grade4,  grade5,  grades);
	
}


There are quite a few steps such as file handling to be attended to.
Last edited on
So, I made the recommend changes and now I am getting too few arguments.
Severity Description Project Line
Error (active) too few arguments in function call project5 25

You need to post your current code.

Your error message mentions function project5, but I see no such function in the previous code you posted.

Line 46: All your arguments are passed by value. Nothing you read will be passed back to to the caller.

Line 50: Only grade1 is read as part of the while loop. The other grades won't be read.
You want {} around the lines that are conditional on the while.
1
2
3
4
5
6
7
8
  while (myfile >> grades)
  {  //  Note the {} around the statements to be conditional
    myfile >> grade1;
    myfile >> grade2;
    myfile >> grade3;
    myfile >> grade4;
    myfile >> grade5;
  }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
AbstractionAnon
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
#include <iostream>
#include <fstream> //needed to input files
#include <string>  //needed for string input
using namespace std;

//Function prototype.
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades);
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5);
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5);
void open(ifstream&);
void didnotopen(ifstream&);

int main()
{
	ifstream myfile;
	int grade1 = 0, grade2 = 0, grade3 = 0, grade4 = 0, grade5 = 0;


	//open file
	open(myfile);
	//get score
	getScore(myfile, grade1, grade2, grade3, grade4, grade5);      //This where I am having an issue. What should I put in ()?
}
//****************************************************************************
//open file
//****************************************************************************
void open(ifstream& myfile)
{
	myfile.open("grades.txt");

	didnotopen(myfile);
}
//****************************************************************************
//did the file open
//****************************************************************************
void didnotopen(ifstream& myfile)
{
	if (!myfile)
	{
		cerr << "\n Error: Unable to read file." << endl;
		exit(10);
	}
}
//****************************************************************************
//GetScore function
//****************************************************************************
void getScore(ifstream& myfile, int grade1, int grade2, int grade3, int grade4, int grade5, string grades)
{
	{
		while (myfile >> grades)
			myfile >> grade1;
		myfile >> grade2;
		myfile >> grade3;
		myfile >> grade4;
		myfile >> grade5;

		cout << grade1 << endl;
		cout << grade2 << endl;
		cout << grade3 << endl;
		cout << grade4 << endl;
		cout << grade5 << endl;
	}
}
//*********************************************
//Find lowest function
//*********************************************
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	int lowest = grade1;

	if (grade2 < lowest)
	{
		lowest = grade2;
	}
	else if (grade3 < lowest)
	{
		lowest = grade3;
	}
	else if (grade4 < lowest)
	{
		lowest = grade4;
	}
	else if (grade5 < lowest)
	{
		lowest = grade5;
	}
	return lowest;
}
//*********************************************
//CalcAverage
//*********************************************
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	double gpa;
	int lowest = findLowest(grade1, grade2, grade3, grade4, grade5);
	gpa = ((grade1 + grade2 + grade3 + grade4 + grade5) - lowest) / 4.0;
	cout << "The grade point average is " << gpa << endl;
}

The Project5 is the name of the project or the file.
Line 7,47: getScore takes 7 arguments.

Line 22: You're only passing 6 arguments to getScore.

Line 49-62: Your {} are in the wrong place. I already showed you where to put them.

Line 47-63: You're going to read the file (not correctly, but I already pointed that out). You're not doing anything with the data you read. Each iteration of the loop is going to overlay the previous values and those values are going to be lost when you exit getScore. You have a structural problem here. You want to do something with each set of grades you read.


Talked to my teacher and it is making more since. This is my new code and is working.
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
#include <iostream>
#include <fstream> //needed to input files
#include <string>  //needed for string input
using namespace std;

//Function prototype.
void getscore(int&, ifstream&);       //int is the variable type and ifstream is needed because we are using file for input
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5);    //using the 5 variables
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5);  //using the 5 variables


int main()
{
	//need to read file input
	ifstream myfile;  
	
	//this are the five variables that will hold each grade
	int grade1;
	int grade2;
	int grade3;
	int grade4;
	int grade5;




	//opens file
	myfile.open("grades.txt");

	//Checks to make sure file is open
	if (!myfile)
	{
		cout << "\n Error: Unable to read file." << endl;
		return 1000;
	}

	//Calls getscore and assign a variable to each grade
	getscore(grade1, myfile);
	getscore(grade2, myfile);
	getscore(grade3, myfile);
	getscore(grade4, myfile);
	getscore(grade5, myfile);

	//After getscore assigns values to each variable calcAverage will call the void function to find the average
	calcAverage(grade1, grade2, grade3, grade4, grade5);

	return 0;
}


//****************************************************************************
//GetScore function
//****************************************************************************

//hold the data from the file and gives each line a variable when it is called
void getscore(int & grades, ifstream& myfile)
{
		myfile >> grades;	
}
//*********************************************
//Find lowest function
//*********************************************

//CalAverage call the function and this function takes the variables that are assigned to each getscore and finds the lowest
int findLowest(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	int lowest = grade1;   //sets the first value as the lowest

	if (grade2 < lowest)   //Each value is compared to find the lowest
	
		lowest = grade2;
	
	if (grade3 < lowest)  //When the lowest is find that will be the grade that is dropped
	
		lowest = grade3;
	
	if (grade4 < lowest)
	
		lowest = grade4;
	
	if (grade5 < lowest)
	
		lowest = grade5;
	
	return lowest;
}
//*********************************************
//CalcAverage
//*********************************************

//Int main calls this function and plugs in the variables from each get call
//CalcAverage calls lowest funciton to find the lowest grade to drop it and than finds the average of the 4 grades
void calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
	double gpa;  //variable to find GPA
	int lowest;  //variable that gives the lowest grade

	lowest = findLowest(grade1, grade2, grade3, grade4, grade5); //calls findLowest funciton
	gpa = ((grade1 + grade2 + grade3 + grade4 + grade5) - lowest) / 4.0;  //Adds all values and subtracts the lowest and divide by 4
	cout << "The grade point average is " << gpa << endl <<endl;
}
Last edited on
closed account (48T7M4Gy)
Excellent! We can all move on to the next challenge :)
Pages: 12