|
 |
|
 |
05-01-2005, 08:12 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
help with functions c++
I would appreciate some help with this problem.This program is supose to read students name (i have created the infile) and test scores.
A ) a void function(calculateaverage) to determin the average of 5 test scores use a loop. this function does not output average test score.that is done in main.
B ) a value returning function to determin and return each student grade. function does not output the grade. that is done in main.
i have tried a few different things.
This is what i have:
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
void calculatescore(double totalscore, ifstream& infile);
void calculategrade(int grade);
string studentname;
double totalscore;
double test1;
double test2;
double test3;
double test4;
double test5;
int grade;
double average;
ifstream infile;
ofstream outfile;
infile.open("g://students.txt");
if (!infile)
{
cout << "unable to open files" << endl;
return 1;
}
outfile.open("g://stgrades.txt");
outfile << fixed << showpoint << setprecision(2);
cout << "Processing data" << endl;
outfile << left << setw(5) << "Student Name"
<< setw(20) << "Test 1"
<< setw(10) << "Test 2"
<< setw(10) << "Test 3"
<< setw(10) << "Test 4"
<< setw(10) << "Test 5"
<< setw(10) << "Average"
<< setw(10) << "Grade" << endl;
calculategrade(grade);
calculatescore(totalscore, infile);
average = totalscore / 5;
if (studentname == studentname)
do
{
infile >> studentname>> test1 >> test2 >> test3 >> test4 >> test5;
outfile << left << setw(10) << studentname
<< setw(25) << test1
<< setw(15) << test2
<< setw(15) << test3
<< setw(15) << test4
<< setw(10) << test5
<< setw(20) << average
<< setw(15) << grade << endl;
}
while (studentname == studentname);
infile.close();
outfile.close();
return 0;
}
void calculatescore(double totalscore);
infile.open("g://students.text");
double scores = test1, test2, test3, test4, test5;
if (scores == scores)
do
{
totalscore = totalscore + scores;
infile >> test1, test2, test3, test4, test5;
while(scores = test1, test2, test3, test4, test5)
return totalscore;
}// end while
}
void calculategrade(int grade);
char a;
char b;
char c;
char d;
char f;
{
calculatescore(totalscore);
average = totalscore / 5
{
if (average >= 90)
grade = A
if (average >= 80)
grage = B
if (average >= 70)
grade = c
if (average >= 60)
grade = D
if (average < 60)
grade = F
return grade;
}
}
Last edited by redhead; 05-01-2005 at 09:30 AM.
Reason: Remember to use [code ] [/code ] tags
|
|
|
05-01-2005, 10:22 AM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Yes, and what is the problem?

__________________
|
|
|
05-01-2005, 10:37 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
i keep getting syntax errors and i think one was a connection error with the functions but here is what i have now. I tried to fix it some I might have made it worse.
Compiler: Default compiler
Compiler: Default compiler
Executing g++.exe...
g++.exe "G:\grades\grades.cpp" -o "G:\grades\grades.exe" -I"C:\Dev-Cpp\include\c++\3.3.1" -I"C:\Dev-Cpp\include\c++\3.3.1\mingw32" -I"C:\Dev-Cpp\include\c++\3.3.1\backward" -I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.3.1\include" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
G:/grades/grades.cpp:83: error: syntax error before `if'
G:/grades/grades.cpp:87: error: syntax error before `>>' token
G:/grades/grades.cpp:105: error: `totalscore' was not declared in this scope
G:/grades/grades.cpp:105: error: ISO C++ forbids declaration of `calculatescore
' with no type
G:/grades/grades.cpp:105: error: `int calculatescore' redeclared as different
kind of symbol
G:/grades/grades.cpp:75: error: previous declaration of `void
calculatescore(double)'
G:/grades/grades.cpp:106: error: ISO C++ forbids declaration of `average' with
no type
G:/grades/grades.cpp:106: error: `totalscore' was not declared in this scope
G:/grades/grades.cpp:107: error: syntax error before `{' token
Execution terminated
|
|
|
05-01-2005, 10:49 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
A few things here confuses me, first of all:
Code:
void calculatescore(double totalscore, ifstream& infile);
void calculategrade(int grade);
should be placed outside the main scope, next thing you have an int representign the grade, yet a double representing totalscore, now I have no idear how you are planing to count grades, being 4.3 or as we do here, where it ranges from 0 to 13, but if you plan on giving grades of type int, then the totalscore will easily be kept in an int aswell.
Another thing:
Code:
if (studentname == studentname)
...
while (studentname == studentname);
...
if (scores == scores)
is the same as saying:
Code:
if (1)
...
while (1)
...
if (1)
And the calculatescore() and calculategrade(), needs to be made into functions ie:
Code:
void calculatescore(double totalscore)
{
infile.open("g://students.text");
double scores = test1, test2, test3, test4, test5;
...
And the code in the functions are way to fscked up to produce what ever you want it to do.
Code:
...
infile >> test1, test2, test3, test4, test5;
while(scores = test1, test2, test3, test4, test5)
Won't work, I'm not even sure what you want to achieve with this, but I'm guessing some sort of test for how long your calculation of totalscore should run.
Since I have no idear how your "g://students.text" file looks like I can't say what you're looking for here.. But again I'm guessing it might be the end of file you want to look for.. Or perhaps not.. Another thing, unless you've got a realy screwed up folder on your computer the "g://students.text" wont even exist, "g:\\students.text" might be a better aproach.
|
|
|
05-01-2005, 12:54 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
This is the text file
Balto 85 83 77 91 76
Mickey 80 90 95 93 48
Minnie 78 81 11 90 73
Doc 92 83 30 69 87
Goofy 23 45 96 38 59
Duckey 60 85 45 39 67
Grumpy 27 31 52 74 83
Sunny 93 94 89 77 97
Piggy 79 85 28 93 82
Pluto 85 72 49 75 63
|
|
|
05-01-2005, 06:42 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
now all of the errors are gone but the only thing printing is the headings.
could someone look at this and see what is wrong. here are the instructions word for word.
write a program that reads the students name together with his or her test scores. the program should compute the average test score for each student and assign the appropriet grade.
a.) a void function, calculateaverage, to determin the average of the five testscores for each student. use a loop to read and sum the test scores. this function does not output the average test score. that is done in main.
b.) a value returning function,calculategrade, to determin and return each students grade. this function does not output the grade that is done in main.
do not use global variables
the infile is in the previous note.
Thank you very much.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void calculatescore();
int calculategrade();
int main ()
{
string studentname;
int total;
int totalscore;
int test1;
int test2;
int test3;
int test4;
int test5;
int grade;
int average;
ifstream infile;
ofstream outfile;
infile.open("g:\\student.txt");
outfile.open("g:\\grades.txt");
outfile << fixed << showpoint << setprecision(2);
outfile << left << setw(20) << "Student Name"
<< setw(10) << "Test 1"
<< setw(10) << "Test 2"
<< setw(10) << "Test 3"
<< setw(10) << "Test 4"
<< setw(10) << "Test 5"
<< setw(10) << "Average"
<< setw(10) << "Grade" << endl;
while (infile >> studentname);
{
{
grade = calculategrade();
calculatescore();
average = total / 5;
}
outfile << left << setw(20) << studentname
<< setw(25) << test1
<< setw(15) << test2
<< setw(15) << test3
<< setw(15) << test4
<< setw(10) << test5
<< setw(20) << average
<< setw(15) << grade << endl;
}// end while
infile >> studentname;
}
void calculatescore()
{
int total;
int test1;
int num = test1, test2, test3, test4, test5;
string studentname;
int counter;
ifstream infile;
infile >> studentname >> test1 >> test2 >> test3 >> test4 >> test5;
counter = 0;
while (counter < 4)
{
total = 0;
infile >> num;
while (num != -999);
{
total = total + num;
counter++;
infile >> num;
}
}
}
int calculategrade()
{
int total;
int grade;
int A;
int B;
int C;
int D;
int F;
int average;
calculatescore();
average = total / 5;
{
if (average >= 90)
return A;
if (average >= 80)
return B;
if (average >= 70)
return C;
if (average >= 60)
return D;
if (average < 60)
return F;
}
}
Last edited by redhead; 05-01-2005 at 11:41 PM.
Reason: Addition of [code] [/code] tags
|
|
|
05-01-2005, 07:05 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
CODE tags. You can do it 
__________________
|
|
|
05-01-2005, 09:55 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
i tryed to figure out what you mean by code tags but i'm not sure. i do know about comments but we haven't been told about tags.
|
|
|
05-01-2005, 11:48 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
in your calculategrade() you return an int, which is holding nothing, so when calling this you end up with nothing.
Same with your calculatescore(), it does alot of calculations, but it dosn't do anything with what it achieves, you have no way of getting hold of whatever it calculates.
If I can find the time today, I will try and make my version of this, but don't count on it.
|
|
|
05-02-2005, 12:00 AM
|
#10 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
thank you for replying and i will appreciate any help i can get on this
|
|
|
05-03-2005, 07:50 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
Just wanted to say thanks to you for replying to my question. I did get it all figured out this morning and my program is running.
|
|
|
05-03-2005, 11:13 AM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
See if you like this sort of a setup. There are a few minor things to improve, there is a lot to play with:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void get_student_grades(ifstream&, int*);
int calc_total_score(const int*, const int);
char calc_grade(int avg);
void save_record(ofstream& file, string name,int* arr, int size, int avg);
int main ()
{
string StudentName;
//Always initialize arrays when possible: prevents weird behaviour.
int Grades[5] = {0};
int TotalScore(0);
int AverageScore(0);
//--
ifstream StudentDB("students.txt");
//Could the file be opened without any errors?
if(!StudentDB)
{
cout<<"Couldn't open file (correctly). Exiting application..."<<endl;
return(1);
}
//Let's create a file to store formatted data.
ofstream FmtDB("grades.txt");
//Setup the header of the formatted data on a different file.
FmtDB << "Name\tScore 1\tScore 2\tScore 3\tScore 4\tScore 5\tAverage\tFinal\t"<<endl;
//--
while(StudentDB >> StudentName)
{
//First, let's collect data.
get_student_grades(StudentDB, Grades);
TotalScore = calc_total_score(Grades, 5);
AverageScore = TotalScore/5;
//Second, we store the record on already prepared file.
save_record(FmtDB, StudentName, Grades, 5, AverageScore);
}
//--
return 0;
}
//-------------------------------------
void get_student_grades(ifstream& dbase, int* arr)
{
unsigned i(0);
while(dbase >> arr[i])
{
++i;
}
//Give database file back in a decent state.
dbase.clear();
}
//-------------------------------------
int calc_total_score(const int* arr, const int size)
{
int total(0);
for(int i = 0; i < size; ++i)
{
total += arr[i];
}
return total;
}
//-------------------------------------
char calc_grade(int avg)
{
if (avg >= 90)
return 'A';
if (avg >= 80)
return 'B';
if (avg >= 70)
return 'C';
if (avg >= 60)
return 'D';
//Bad student: score lower then 60.
return 'F';
}
//-------------------------------------
void save_record(ofstream& file, string name, int* arr, int size, int avg)
{
file << name <<"\t";
for(int i = 0; i < size; ++i)
{
file << arr[i] << "\t";
}
file << avg << "\t";
file << calc_grade(avg) <<endl;
}
__________________
|
|
|
05-04-2005, 07:03 PM
|
#13 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 15
|
thanks I will check this out.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:35 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|