trouble with void functions[SOLVED]

having trouble following the logic of this program using void

Page 1 of 1

12 Replies - 3708 Views - Last Post: 29 October 2009 - 06:33 PM Rate Topic: -----

#1 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:29 PM

*Please read this first post carefully. Thanks.

I'm not sure if I should say "intermediate" yet or if i should wait until I get into arrays for that, but this program feels pretty intermediate to me. here are the directions directly out of the book:

Quote

Write a program that reads a student's name together with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows:
90-100, A; 80-89, B; 70-79, C; 60-69, D; 0-59, F. Your program must use the following functions:

a. A void function, calculateAverage, to determine the average of the five test scores for each student. Use a loop to read and sum the five test scores. (this function does not output the average test score. That task must be done in the function main.)

b. A value returning function, calculateGrade, to determine and return each student's grade. (this function does not output the grade. that task must be done in the function main.)

Test your program on the following date. Read the data from a file and send the output to a file. Do not use any global variables. Use the appropriate parameters to pass values in and out of functions.


the input file testScores.txt is attatched. with the code below this file must be in c:\ in order to work. I have done this the best that I can but I do not see any way to use a void function for so simple a task as calculating the five test scores. The code below works and runs. It outputs a file "averages.txt" to c:\ with the information that is specified by the assignment. however, it does not use a void function and the last name and test scores read into the file are output twice. any help you guys could give me on this assignment would be greatly appreciated as I have already spent four hours on this assignment. finally, here is my code and thanks in advance:

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>



using namespace std;


//user defined function, calculates the average of five test scores.
double calculateAverage(double test1, double test2, double test3, double test4, double test5);


//user defined function. takes the average test scores and returns the letter grade.
char calculateGrade(double average);



//calculate the average score
double calculateAverage(double test1, double test2, double test3, double test4, double test5)
{

	int average;

	average=(test1+test2+test3+test4+test5)/5; //add the test scores and divide by the number of tests.

	return average; //return the average of the five tests.

}


//assign a letter grade
char calculateGrade(double average)

{
	char grade;

	if (average >= 90)

		grade='A';

	else if(average>=80)

		grade='B';

	else if(average>=70)

		grade='C';

	else if(average>=60)

		grade='D';

	else

		grade='F';

	return grade;

}


int main()
{

	double test1, test2, test3, test4, test5; //using the same variables as the average calculator
	double average;
	double classaverage=0; //set class average to zero for latter update
	double count=0;   //set count to zero for later update

	char grade;	//letter grade to be returned by the calculateGrade function

	string name;   //name to be taken from a file later specified

	ifstream infile;  //creates an operator that extracts input from a file
	ofstream outfile; //creates an operator that prints output to a file


	infile.open("c:\\testScores.txt"); //opens the file including the test scores.
	outfile.open("c:\\averages.txt");  //creates a file for average and grade output

	outfile<<fixed<<showpoint<<setprecision(2);  //sets decimal places to 2

	if (!infile)  //if the file does not exist or is in the wrong place
	{

		cout<<"cannot open the input file. please ensure that the file "<<"testScores is in c:/"<<endl;

	return -1;  //return statement rather than using else
	}

	

	outfile<<"Student\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade"<<endl;//outputs top column of output file



	while(!infile.eof()) //until the file "testScores.txt" has ended, do the following.
	{

		infile>> name>> test1>> test2>> test3 >> test4 >>test5; //extracts test scores from file

		//outputs test score to the new file
		outfile<<name<<'\t'<<test1<<'\t'<<test2<<'\t'<<test3<<'\t'<<test4<<'\t'<<test5<<'\t';

		average=calculateAverage(test1, test2, test3, test4, test5);

		grade=calculateGrade(average); //initialize grade to be the returned value of calculateAverage.

		classaverage=classaverage+average; //adds each average output by the calculateAverage function

		count=count++;   //counts every time the loop is run for later use

		outfile<<average<<'\t'<<grade<<endl;  //outputs each average and letter grade

	}

	//divides the classAverage by the count for class average.

	classaverage=classaverage/count;

	outfile<<'\n'<<"Class Average: "<<classaverage;

	return 0;

}

Attached File(s)


This post has been edited by woodpryan: 29 October 2009 - 07:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trouble with void functions[SOLVED]

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1010
  • View blog
  • Posts: 4,213
  • Joined: 14-February 08

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:34 PM

You could call your calculateAverage function from within your calculateGrade function and return the value, so get all the values from the file and put each one through the calculateAverage and then return the value.
Was This Post Helpful? 0
  • +
  • -

#3 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:43 PM

View Poststayscrisp, on 29 Oct, 2009 - 01:34 PM, said:

You could call your calculateAverage function from within your calculateGrade function and return the value, so get all the values from the file and put each one through the calculateAverage and then return the value.


wouldn't that, then make 'calculateGrade' a void function? calculateAverage must be a void function and calculateGrade must be a value returning function.
Was This Post Helpful? 0
  • +
  • -

#4 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:44 PM

calculateAverage must be of type void. that means the prototype should look something like:

// Prototype for calculateAverage
// Takes 5 grades in the form of an array and a reference variable for the
// average
void calculateAverage(double* grades, double& average);



So you are handing it an array (by pointer, so like if i had an array called 'myArray' i'd pass it to the function as just 'myArray') and whatever you want to store the average in. So if you had this in main:

double avg;
double grade_array[5];
// and you read in the grades...



your function call would look like calculateAverage(grade_array, avg) and when the function returned to main, if you wrote calculateAverage right, avg would now be set to your average!
Was This Post Helpful? 0
  • +
  • -

#5 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:52 PM

View Postbodom658, on 29 Oct, 2009 - 01:44 PM, said:

calculateAverage must be of type void. that means the prototype should look something like:

// Prototype for calculateAverage
// Takes 5 grades in the form of an array and a reference variable for the
// average
void calculateAverage(double* grades, double& average);



So you are handing it an array (by pointer, so like if i had an array called 'myArray' i'd pass it to the function as just 'myArray') and whatever you want to store the average in. So if you had this in main:

double avg;
double grade_array[5];
// and you read in the grades...



your function call would look like calculateAverage(grade_array, avg) and when the function returned to main, if you wrote calculateAverage right, avg would now be set to your average!


thank you for your reply. we have not gotten into arrays yet and would not get credit if i used one. I am meant to use a void function (calculateAverage), a value returning function (calculateGrade), a while loop and a switch or if/else statement. No arrays are allowed.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1010
  • View blog
  • Posts: 4,213
  • Joined: 14-February 08

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 02:53 PM

View Postwoodpryan, on 29 Oct, 2009 - 01:43 PM, said:

View Poststayscrisp, on 29 Oct, 2009 - 01:34 PM, said:

You could call your calculateAverage function from within your calculateGrade function and return the value, so get all the values from the file and put each one through the calculateAverage and then return the value.


wouldn't that, then make 'calculateGrade' a void function? calculateAverage must be a void function and calculateGrade must be a value returning function.


No, calculateGrade still returns a value. The value calculated by the calculateAverage function.
Was This Post Helpful? 0
  • +
  • -

#7 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 03:08 PM

View Poststayscrisp, on 29 Oct, 2009 - 01:53 PM, said:

View Postwoodpryan, on 29 Oct, 2009 - 01:43 PM, said:

View Poststayscrisp, on 29 Oct, 2009 - 01:34 PM, said:

You could call your calculateAverage function from within your calculateGrade function and return the value, so get all the values from the file and put each one through the calculateAverage and then return the value.


wouldn't that, then make 'calculateGrade' a void function? calculateAverage must be a void function and calculateGrade must be a value returning function.


No, calculateGrade still returns a value. The value calculated by the calculateAverage function.


I do not understand how I could call the void calculateAverage function from my value returning calculateGrade function since calculateAverage returns no value and there is no value for calculateGrade to manipulate. could you show me the syntax as to how this might work?
Was This Post Helpful? 0
  • +
  • -

#8 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 03:16 PM

e mail response from my instructor:

Quote

Declare all your variables in main. Pass the average as a reference parameter to the function. Read in the data in a loop in the main program and calculate a running total. Pass that total as a value parameter to the function so you can calculate the average.

we are to use pointers but i do not understand them. i'm working on figuring them out.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 03:37 PM

Don't see any pointers in that response, and he/she appears to have told you exactly what to do?
Was This Post Helpful? 0
  • +
  • -

#10 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 03:49 PM

View PostJackOfAllTrades, on 29 Oct, 2009 - 02:37 PM, said:

Don't see any pointers in that response, and he/she appears to have told you exactly what to do?

between my first post in this thread and my last post in this thread, I have stated everything that I know. her e mail response to my questions about this was a bit vague but still conveyed the idea that i need to use pointers, my loops needs to be in my main function, and I think i need to use only one test value rather than 5 (test rather than test1, test2, etc.). i'm still working on it.
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 04:35 PM

Here is a Pass by reference variable example:

#include <iostream>

using namespace std;

// Pass result as reference variable
void assign(int &result, int number)
{
  result = number;
}


int main()
{
  int res = 0;
  int num = 82;

  assign (res, num);

  cout << res << endl;

  cin.get();

}


No worries in the pointer department. :)
Was This Post Helpful? 1
  • +
  • -

#12 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 04:45 PM

View Post#define, on 29 Oct, 2009 - 03:35 PM, said:

Here is a Pass by reference variable example:

#include <iostream>

using namespace std;

// Pass result as reference variable
void assign(int &result, int number)
{
  result = number;
}


int main()
{
  int res = 0;
  int num = 82;

  assign (res, num);

  cout << res << endl;

  cin.get();

}


No worries in the pointer department. :)


thanks, i think this post may be very helpful as a write this program.
Was This Post Helpful? 0
  • +
  • -

#13 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: trouble with void functions[SOLVED]

Posted 29 October 2009 - 06:33 PM

most of the problem is now fixed. thanks to the last post, i was able to figure this out. there is only one problem left and that is that the last column of the file is run through my loop twice. here is my code
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

//user defined function, calculates the average of five test scores.
void calculateAverage(int &avg, int tes1, int tes2, int tes3, int tes4, int tes5);

//user defined function. takes the average test scores and returns the letter grade.
char calculateGrade(double average);

//calculate the average score
void calculateAverage(int &avg, int tes1, int tes2, int tes3, int tes4, int tes5)
{

	avg=(tes1+tes2+tes3+tes4+tes5)/5; //add the test scores and divide by the number of tests.

}

//assign a letter grade
char calculateGrade(double average)
{
	char grade;

	if (average >= 90)
		grade='A';
	else if(average>=80)
		grade='B';
	else if(average>=70)
		grade='C';
	else if(average>=60)
		grade='D';
	else
		grade='F';
	return grade;
}


int main()
{
	int test1, test2, test3, test4, test5; //using the same variables as the average calculator
	int average;
	double classaverage=0.0; //set class average to zero for latter update
	int count=0;   //set count to zero for later update
	char grade;	//letter grade to be returned by the calculateGrade function
	string name;   //name to be taken from a file later specified

	ifstream infile;  //creates an operator that extracts input from a file
	ofstream outfile; //creates an operator that prints output to a file


	infile.open("c:\\testScores.txt"); //opens the file including the test scores.
	outfile.open("c:\\averages.txt");  //creates a file for average and grade output

	outfile<<fixed<<showpoint<<setprecision(2);  //sets decimal places to 2

	if (!infile)  //if the file does not exist or is in the wrong place
	{
		cout<<"cannot open the input file. please ensure that the file "<<"testScores is in c:/"<<endl;
		return -1;  //return statement rather than using else
	}

	outfile<<"Student\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade"<<endl;//outputs top column of output file

	while(!infile.eof()) //until the file "testScores.txt" has ended, do the following.
	{
		infile>> name>> test1>> test2>> test3 >> test4 >>test5; //extracts test scores from file
		//outputs test score to the new file
		outfile<<name<<'\t'<<test1<<'\t'<<test2<<'\t'<<test3<<'\t'<<test4<<'\t'<<test5<<'\t';
		calculateAverage(average, test1, test2, test3, test4, test5);
		grade=calculateGrade(average); //initialize grade to be the returned value of calculateAverage.
		classaverage=classaverage+average; //adds each average output by the calculateAverage function
		count=count++;   //counts every time the loop is run for later use
		outfile<<average<<'\t'<<grade<<endl;  //outputs each average and letter grade
	}

	//divides the classAverage by the count for class average.
	classaverage=classaverage/count;
	outfile<<'\n'<<"Class Average: "<<classaverage;

	return 0;
}


this is my input file "c:\testScores.txt"

Quote

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 75
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kenedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sonny 79 85 28 93 82
Smith 85 72 49 75 63

Quote

and this is my output file "c:\averages.txt"
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76 82 B
Aniston 80 90 95 93 48 81 B
Cooper 78 81 11 90 75 67 D
Gupta 92 83 30 69 87 72 C
Blair 23 45 96 38 59 52 F
Clark 60 85 45 39 67 59 F
Kenedy 77 31 52 74 83 63 D
Bronson 93 94 89 77 97 90 A
Sonny 79 85 28 93 82 73 C
Smith 85 72 49 75 63 68 D
Smith 85 72 49 75 63 68 D

Class Average: 70.45


well, it's actually a little more lined up than that, but i think you get the point
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1