12 Replies - 66197 Views - Last Post: 29 April 2009 - 02:47 PM Rate Topic: -----

#1 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Calculate Average and Grade

Post icon  Posted 29 January 2006 - 07:31 PM

I need to write a program that reads a student's name together with their test scores. Then it should compute the average test scores for each student & assign the appropriate grade.

I need to use a void function called calculateAverage to determine the ave of the 5 test scores for each student. I need to use a loop to read and sum the scores. I need the output to be done in the function main.

Then I need to use a value-returning function calculteGrade to determine & return each student's grade. need the output to be done in the function main.

I need to then read the data from a file and send the output to a file. I can not use any global variables.

I have some code but I know this is not what I need.
#include <iostream>
#include <cmath>
using namespace std;

char name;
double score1;
double score2;
int score3;
int score4;

int main()
{
	cout << "Enter student's name: ";
	cin >> name;
	cout << "Enter test score 1: ";
	cin >> score1;
	cout << "Enter test score 2: ";
	cin >> score2;
	cout << "Enter test score 3: ";
	cin >> score3;
	cout << "Enter test score 4: ";
	cin >> score4;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate Average and Grade

#2 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Calculate Average and Grade

Posted 29 January 2006 - 07:53 PM

As much as I would love to be able to use a single character to hold a string, it just won't happen the way I want it to. Well, it does sometimes, but then my professor yells at me for doing dangerous things. ;) For the name you have two choices. You can use an array of char, but then you have to guess the size, and we programmers are notoriously bad at guessing. 20 is a common size, I don't know why.
#include <iostream>
#include <iomanip>

const int size = 20;

int main()
{
  char name[size];

  std::cin >> std::setw(size) >> name;
  std::cout << name << '\n';
}


Oh darn. So soon and I already have to explain some stuff that's good but not really used often. The const int makes sense because I use the size of the array more than once, and being lazy, it makes life easier on me when I change the size for debugging. The scary part is that std::setw(size) thingie. Because arrays have a finite size, and cin's >> operator is very good at filling arrays beyond their size, I limited how many characters can be read by cin using the setw() function from <iomanip>. It makes sure that no matter how much you try to type, 19 characters is the limit of how many will be written to the array (size - 1 to allow for the null character).

The other option is the one I like best. The std::string class is tailor made for lazy people like me who are also bad at guessing because it grows the array with voodoo magic behind the scenes. I won't get into the voodoo stuff because people's eyes have a tendency to glaze over when I try. The result is simple enough that you don't have to worry about it.
#include <iostream>
#include <string>

int main()
{
  std::string name;

  std::cin >> name;
  std::cout << name << '\n';
}


Much better, isn't it? :) Now you don't have to worry until you're asked to read names with spaces in the middle. cin's >> operator doesn't like that, and you have to stretch in funky ways to make it think otherwise, so the std::getline() function is much better.
#include <iostream>
#include <string>

int main()
{
  std::string name;

  getline(std::cin, name);
  std::cout << name << '\n';
}


Nifty. Wait a sec, why didn't I add the std:: part to getline? That's a nice feature called Koenig Lookup, and for now just ignore it and store away that term for later because you'll probably be doing the 'using namespace std;' thing for now anyway, despite how many std:: thingies my code uses. You're gonna edit them out, aren't you? I bet you will. ;) Anyway, you can use getline with arrays too, but this time getline expects a size in much the same way std::setw() did.
#include <iostream>

const int size = 20;

int main()
{
  char name[size];

  std::cin.getline(name, size);
  std::cout << name << '\n';
}


In this case, getline is a member function of cin, and Koenig Lookup won't work here. But it's still better than the setw() thing, right? Okie dokie, onward and upward. You need to read the name, which we're okay with now, and 5 grades. Five variables is a lot when you have to write them all, so an array would be more convenient. The requirements don't specify the type, so we can assume the usual 0 to 100 grading scheme and use ints. The nice part about using an array is that now we can use a loop to read the grades. :)
#include <iostream>

const int max_size = 20;
const int max_grades = 5;

int main()
{
  char name[max_size];
  int grades[max_grades];

  std::cin.getline(name, max_size);
  std::cout << "Grades for " << name << '\n';

  for (int i = 0; i < max_grades; i++)
    std::cin >> grades[i];
}


Normally I would stress error checking heavily, but I don't think you care much at this point. And if you don't care, I still care, but I can overlook it until you're ready. :) An average function is a good exercise, as is working out how to turn an average into a letter grade, so I'll just neglect to include the body of it and provide a declaration that makes sense.
#include <iostream>

const int max_size = 20;
const int max_grades = 5;

int calculateAverage(int grades[], int size);
char calculateGrade(int average);

int main()
{
  char name[max_size];
  int grades[max_grades];

  std::cin.getline(name, max_size);
  std::cout << "Grades for " << name << '\n';

  for (int i = 0; i < max_grades; i++)
    std::cin >> grades[i];

  int average = calculateAverage(grades, max_grades);

  std::cout << name << " earned a(n) "
    << calculateGrade(average) << '\n';
}


There's a little problem with your requirements, and I can't really go any further because of it. First, you can't use any global variables. The max_size and max_grades constants don't really count toward that, but you're also required to use a void function in calculateAverage, but the output has to be done in main... The only way to do that is to pass pointers or references, and it doesn't look like you've gotten that far in your classes yet. So I picked a function that returns the average.

It also looks like you need to handle multiple students, but they can be done one at a time so all you need to do is wrap everything in a loop. The file handling stuff can be easily added and those tasks are simple enough that I think you can manage without me.

But if you have any problems at all, don't hesitate to ask. :)

This post has been edited by Voodoo Doll: 29 January 2006 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calculate Average and Grade

Posted 29 January 2006 - 07:56 PM

[deleted]...not quick enough! :)

This post has been edited by Amadeus: 29 January 2006 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 eLliDKraM  Icon User is offline

  • PepŤ Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Calculate Average and Grade

Posted 30 January 2006 - 07:22 PM

Amadeus...
Posted Image

This post has been edited by eLliDKraM: 30 January 2006 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calculate Average and Grade

Posted 30 January 2006 - 08:19 PM

LMAO...you got that right! :)
Was This Post Helpful? 0
  • +
  • -

#6 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Re: Calculate Average and Grade

Posted 02 February 2006 - 05:39 AM

This is what I have so far but it is not working can some one help on this.
#include <cstring>
#include <fstream>

using namespace std;

void main ()
{
	ifstream inData;	//file pointer
	ofstream outData;
	string name;
	int score1, score2, score3, score4, score5;
	double average

	inData.open("input.txt");
	outData.open("output.txt");
		while (! inData.eof())
{
inData>>name>>score1>>score2>>score3>>score4>>score5;
average= calculateAverage(score1, score2, score3, score4, score5);
}

double calculateAverage(int score1, int score2, int score3, int score4, int score5)
{
	return ((s1+s2+s3+s4+s5)/5);
}
}

Was This Post Helpful? 0
  • +
  • -

#7 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Calculate Average and Grade

Posted 02 February 2006 - 07:49 AM

string name;

To use the string class, you'll want to include the <string> header. The <cstring> header supplies functions and types for a completely different purpose. Well, same purpose, different implementation. :) The string class is a standard C++ class that encapsulates all kinds of good stuff for handling strings. The <cstring> header holds all of the string stuff from C that's not as convenient.
while (! inData.eof())

This is a tricky one because sometimes it causes problems and sometimes it doesn't. The problem is that the eofbit for an input stream is only set after you try to read from it and hit end-of-file. But if you use it as the condition for a file reading loop, you might end up going through the loop twice with the last bit of input in the file. It goes like this.
while not at end-of-file
  read data
  process data


Since you read the data after checking to see if you're at the end of the file, if the read fails and sets the eofbit, you still do the process data step. In most cases that amounts to the read data step not reading anything so the variables still have their previous contents, and process data processes the last record twice. In theory, at least, and I've seen it happen too many times to ignore it. :)

You can avoid the problem by using this method instead.
while read data
  process data


Most read data steps return something that you can use as a boolean test if the step failed or not, so it's a convenient solution that always works. Your code would be changed like so.
while (inData>>name>>score1>>score2>>score3>>score4>>score5)
{
average= calculateAverage(score1, score2, score3, score4, score5);
}



return ((s1+s2+s3+s4+s5)/5);

I do this all the time. :blush: You were probably working with some test variable names and then decided to make them more informative but forgot to change them here. s1, s2, s3 and so on should be score1, score2, score3 and so on to match the function parameters.

So far you're doing great! Keep it up. :)

This post has been edited by Voodoo Doll: 02 February 2006 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Re: Calculate Average and Grade

Posted 02 February 2006 - 08:26 AM

Thanks for the help. I am still getting error messages.
ave= calculateAverage(score1, score2, score3, score4, score5);
it says "function does not take 5 arguments

double calculateAverage(int score1, int score2, int score3, int score4, int score5)
{
return ((score1+score2+score3+score4+score5)/5);
}
it says"local function definitions are illegal"
any suggestions
Was This Post Helpful? 0
  • +
  • -

#9 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Calculate Average and Grade

Posted 02 February 2006 - 11:50 AM

Oh right, I forgot to mention that in my hurry to explain the loop. You can't define a function inside of another function in C++. In compy lingo, functions are at the top lexical level of a translation unit. :)
#include <string>
#include <fstream>

using namespace std;

double calculateAverage(int score1, int score2, int score3, int score4,int score5)
{
  return ((score1+score2+score3+score4+score5)/5);
}

void main()
{
  ifstream inData; //file pointer
  ofstream outData;
  string name;
  int score1, score2, score3, score4, score5;
  double average;

  inData.open("input.txt");
  outData.open("output.txt");
  while (inData>>name>>score1>>score2>>score3>>score4>>score5)
  {
    average= calculateAverage(score1, score2, score3, score4, score5);
  }
}


Sorry about that, if I didn't forget to mention it then you wouldn't have spent however many hours being stuck. :(

This post has been edited by Voodoo Doll: 02 February 2006 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#10 codenovice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 06

Re: Calculate Average and Grade

Posted 01 December 2006 - 01:05 PM

What if the file had 1000 scores that you needed to average together??

You wouldn't use:

while (inData>>name>>score1>>score2>>score3>>score4>>score5>>score6>>score7>>.......>>score1000)

How would it be accomplished?????
Was This Post Helpful? 0
  • +
  • -

#11 WHITJAQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 28-November 06

Re: Calculate Average and Grade

Posted 04 December 2006 - 04:54 PM

View PostVoodoo Doll, on 29 Jan, 2006 - 07:53 PM, said:

As much as I would love to be able to use a single character to hold a string, it just won't happen the way I want it to. Well, it does sometimes, but then my professor yells at me for doing dangerous things. ;) For the name you have two choices. You can use an array of char, but then you have to guess the size, and we programmers are notoriously bad at guessing. 20 is a common size, I don't know why.
#include <iostream>
#include <iomanip>

const int size = 20;

int main()
{
 †char name[size];

 †std::cin >> std::setw(size) >> name;
 †std::cout << name << '\n';
}


Oh darn. So soon and I already have to explain some stuff that's good but not really used often. The const int makes sense because I use the size of the array more than once, and being lazy, it makes life easier on me when I change the size for debugging. The scary part is that std::setw(size) thingie. Because arrays have a finite size, and cin's >> operator is very good at filling arrays beyond their size, I limited how many characters can be read by cin using the setw() function from <iomanip>. It makes sure that no matter how much you try to type, 19 characters is the limit of how many will be written to the array (size - 1 to allow for the null character).

The other option is the one I like best. The std::string class is tailor made for lazy people like me who are also bad at guessing because it grows the array with voodoo magic behind the scenes. I won't get into the voodoo stuff because people's eyes have a tendency to glaze over when I try. The result is simple enough that you don't have to worry about it.
#include <iostream>
#include <string>

int main()
{
 †std::string name;

 †std::cin >> name;
 †std::cout << name << '\n';
}


Much better, isn't it? :) Now you don't have to worry until you're asked to read names with spaces in the middle. cin's >> operator doesn't like that, and you have to stretch in funky ways to make it think otherwise, so the std::getline() function is much better.
#include <iostream>
#include <string>

int main()
{
 †std::string name;

 †getline(std::cin, name);
 †std::cout << name << '\n';
}


Nifty. Wait a sec, why didn't I add the std:: part to getline? That's a nice feature called Koenig Lookup, and for now just ignore it and store away that term for later because you'll probably be doing the 'using namespace std;' thing for now anyway, despite how many std:: thingies my code uses. You're gonna edit them out, aren't you? I bet you will. ;) Anyway, you can use getline with arrays too, but this time getline expects a size in much the same way std::setw() did.
#include <iostream>

const int size = 20;

int main()
{
 †char name[size];

 †std::cin.getline(name, size);
 †std::cout << name << '\n';
}


In this case, getline is a member function of cin, and Koenig Lookup won't work here. But it's still better than the setw() thing, right? Okie dokie, onward and upward. You need to read the name, which we're okay with now, and 5 grades. Five variables is a lot when you have to write them all, so an array would be more convenient. The requirements don't specify the type, so we can assume the usual 0 to 100 grading scheme and use ints. The nice part about using an array is that now we can use a loop to read the grades. :)
#include <iostream>

const int max_size = 20;
const int max_grades = 5;

int main()
{
 †char name[max_size];
 †int grades[max_grades];

 †std::cin.getline(name, max_size);
 †std::cout << "Grades for " << name << '\n';

 †for (int i = 0; i < max_grades; i++)
 † †std::cin >> grades[i];
}


Normally I would stress error checking heavily, but I don't think you care much at this point. And if you don't care, I still care, but I can overlook it until you're ready. :) An average function is a good exercise, as is working out how to turn an average into a letter grade, so I'll just neglect to include the body of it and provide a declaration that makes sense.
#include <iostream>

const int max_size = 20;
const int max_grades = 5;

int calculateAverage(int grades[], int size);
char calculateGrade(int average);

int main()
{
 †char name[max_size];
 †int grades[max_grades];

 †std::cin.getline(name, max_size);
 †std::cout << "Grades for " << name << '\n';

 †for (int i = 0; i < max_grades; i++)
 † †std::cin >> grades[i];

 †int average = calculateAverage(grades, max_grades);

 †std::cout << name << " earned a(n) "
 † †<< calculateGrade(average) << '\n';
}


There's a little problem with your requirements, and I can't really go any further because of it. First, you can't use any global variables. The max_size and max_grades constants don't really count toward that, but you're also required to use a void function in calculateAverage, but the output has to be done in main... The only way to do that is to pass pointers or references, and it doesn't look like you've gotten that far in your classes yet. So I picked a function that returns the average.

It also looks like you need to handle multiple students, but they can be done one at a time so all you need to do is wrap everything in a loop. The file handling stuff can be easily added and those tasks are simple enough that I think you can manage without me.

But if you have any problems at all, don't hesitate to ask. :)

Was This Post Helpful? 1
  • +
  • -

#12 WHITJAQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 28-November 06

Re: Calculate Average and Grade

Posted 04 December 2006 - 05:03 PM

here is the work that i did for my class hope this can help.





#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);
char CalculateGrade(double courseAvg);
int main()
{
int counter;
string name;
double classAvg = 0;
char grade;
double courseAvg;
ifstream infile;
ofstream outfile;
infile.open ("c:\\test.txt");
outfile.open ("c:\\CAvg.out");
outfile<<fixed<<showpoint;
outfile<<setprecision(2);
outfile<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
for (counter=1;counter<=10;counter++)
{
infile>>name;
outfile<<left<<setw(10)<<name;
CalculateAverage(infile,outfile,courseAvg);
outfile<<setw(5)<<courseAvg<<endl;
grade=CalculateGrade(courseAvg);
outfile<<setw(6)<<grade<<endl;
}
classAvg = classAvg + courseAvg;
classAvg = classAvg/10;
outfile<<"Class Average ="<<classAvg<<endl;
infile.close();
outfile.close();
return 0;

}

void CalculateAverage(ifstream& inp,ofstream& outp,double& courseAvg)
{
int score;
int sum=0;
int count;
for(count=1;count<=5;count++)
{
inp>>score;
outp<<left<<setw(5)<<score;
sum=sum+score;
}
courseAvg=sum/5;
}



char CalculateGrade(double courseAvg)
{
if(courseAvg>=90)
return 'A';
else if (courseAvg>=80)
return 'B';
else if (courseAvg>=70)
return 'C';
else if (courseAvg>=60)
return 'D';
else
return 'F';
}#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);
char CalculateGrade(double courseAvg);
int main()
{
int counter;
string name;
double classAvg = 0;
char grade;
double courseAvg;
ifstream infile;
ofstream outfile;
infile.open ("c:\\test.txt");
outfile.open ("c:\\CAvg.out");
outfile<<fixed<<showpoint;
outfile<<setprecision(2);
outfile<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
for (counter=1;counter<=10;counter++)
{
infile>>name;
outfile<<left<<setw(10)<<name;
CalculateAverage(infile,outfile,courseAvg);
outfile<<setw(5)<<courseAvg<<endl;
grade=CalculateGrade(courseAvg);
outfile<<setw(6)<<grade<<endl;
}
classAvg = classAvg + courseAvg;
classAvg = classAvg/10;
outfile<<"Class Average ="<<classAvg<<endl;
infile.close();
outfile.close();
return 0;

}

void CalculateAverage(ifstream& inp,ofstream& outp,double& courseAvg)
{
int score;
int sum=0;
int count;
for(count=1;count<=5;count++)
{
inp>>score;
outp<<left<<setw(5)<<score;
sum=sum+score;
}
courseAvg=sum/5;
}



char CalculateGrade(double courseAvg)
{
if(courseAvg>=90)
return 'A';
else if (courseAvg>=80)
return 'B';
else if (courseAvg>=70)
return 'C';
else if (courseAvg>=60)
return 'D';
else
return 'F';
}
Was This Post Helpful? 1

#13 jebevins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-April 09

Re: Calculate Average and Grade

Posted 29 April 2009 - 02:47 PM

HEY THERE!!! I HAVE ALMOST THE SAME ASSIGNMENT, MY PROGRAM IS SIMILAR TO THE ONE YOU JUST POSTED...YOU HAVE SOME ERRORS WHEN i TRIED TO COMPILE IT, BUT YOUR PROGRAMED IS HELPING ME ALOT..


View PostWHITJAQ, on 4 Dec, 2006 - 04:03 PM, said:

here is the work that i did for my class hope this can hel



#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);
char CalculateGrade(double courseAvg);
int main()
{
int counter;
string name;
double classAvg = 0;
char grade;
double courseAvg;
ifstream infile;
ofstream outfile;
infile.open ("c:\\test.txt");
outfile.open ("c:\\CAvg.out");
outfile<<fixed<<showpoint;
outfile<<setprecision(2);
outfile<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
for (counter=1;counter<=10;counter++)
{
infile>>name;
outfile<<left<<setw(10)<<name;
CalculateAverage(infile,outfile,courseAvg);
outfile<<setw(5)<<courseAvg<<endl;
grade=CalculateGrade(courseAvg);
outfile<<setw(6)<<grade<<endl;
}
classAvg = classAvg + courseAvg;
classAvg = classAvg/10;
outfile<<"Class Average ="<<classAvg<<endl;
infile.close();
outfile.close();
return 0;

}

void CalculateAverage(ifstream& inp,ofstream& outp,double& courseAvg)
{
int score;
int sum=0;
int count;
for(count=1;count<=5;count++)
{
inp>>score;
outp<<left<<setw(5)<<score;
sum=sum+score;
}
courseAvg=sum/5;
}



char CalculateGrade(double courseAvg)
{
if(courseAvg>=90)
return 'A';
else if (courseAvg>=80)
return 'B';
else if (courseAvg>=70)
return 'C';
else if (courseAvg>=60)
return 'D';
else
return 'F';
}#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);
char CalculateGrade(double courseAvg);
int main()
{
int counter;
string name;
double classAvg = 0;
char grade;
double courseAvg;
ifstream infile;
ofstream outfile;
infile.open ("c:\\test.txt");
outfile.open ("c:\\CAvg.out");
outfile<<fixed<<showpoint;
outfile<<setprecision(2);
outfile<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
for (counter=1;counter<=10;counter++)
{
infile>>name;
outfile<<left<<setw(10)<<name;
CalculateAverage(infile,outfile,courseAvg);
outfile<<setw(5)<<courseAvg<<endl;
grade=CalculateGrade(courseAvg);
outfile<<setw(6)<<grade<<endl;
}
classAvg = classAvg + courseAvg;
classAvg = classAvg/10;
outfile<<"Class Average ="<<classAvg<<endl;
infile.close();
outfile.close();
return 0;

}

void CalculateAverage(ifstream& inp,ofstream& outp,double& courseAvg)
{
int score;
int sum=0;
int count;
for(count=1;count<=5;count++)
{
inp>>score;
outp<<left<<setw(5)<<score;
sum=sum+score;
}
courseAvg=sum/5;
}



char CalculateGrade(double courseAvg)
{
if(courseAvg>=90)
return 'A';
else if (courseAvg>=80)
return 'B';
else if (courseAvg>=70)
return 'C';
else if (courseAvg>=60)
return 'D';
else
return 'F';
}

This post has been edited by jebevins: 29 April 2009 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1