| | |
Using functions and arrays to create a program for grades
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
Please assist with creating a program using arrays and functions for student grades. These are the requirements:
To create and execute a C++ program with functions and arrays that will read student information input from an input text file, calculate the test average, program average, course average, and the letter grade earned and output the result to the monitor as well as to an output text file. Program must allow grades to be calculated for unknown but finite number of students’ records in the input text file. For this assignment, the maximum number of students is limited to 40. The letter grade earned is based on the course average as noted below.
Course Average
Letter Grade
90 - 100
A
80 - 89
B
Below 80
F
The following is required:
Program Input
The input must be read from a text file located on a disk. It must consist of the following information:
· Student’s First Name (alphabetic);
· Student’s Last Name (alphabetic);
· First Program Grade (a number between 0 and 100);
· Second Program Grade (a number between 0 and 100);
· Third Program Grade (a number between 0 and 100);
· First Test Grade (a number between 0 and 100);
· Second Test Grade (a number between 0 and 100);
· Third Test Grade (a number between 0 and 100);
this is what I have thus far which isn't running:
:mad:
To create and execute a C++ program with functions and arrays that will read student information input from an input text file, calculate the test average, program average, course average, and the letter grade earned and output the result to the monitor as well as to an output text file. Program must allow grades to be calculated for unknown but finite number of students’ records in the input text file. For this assignment, the maximum number of students is limited to 40. The letter grade earned is based on the course average as noted below.
Course Average
Letter Grade
90 - 100
A
80 - 89
B
Below 80
F
The following is required:
- Your program must prompt the user for and read the input text file name/path.
- You are required to use one or more functions for each of the following:
- Output descriptive header.
- Read a student’s name and scores into two dimensional string and integer arrays ( string names[40][2] and int scores[40][6] ). Must use an outer while loop and two inner for loops.
- Calculate the total ( int total[40] and two for loops)
- Calculate the program average, test average, course averages, (float averages[40][3] and two for loops). You may also declare three 1D float arrays for the averages and use one or more functions.
- Calculate the letter grade (char grades[40] and one outer for loop)..
- Output student name, total points, program average, test average, and course average to a text file as well as the monitor. Must use an outer for loop.
Program Input
The input must be read from a text file located on a disk. It must consist of the following information:
· Student’s First Name (alphabetic);
· Student’s Last Name (alphabetic);
· First Program Grade (a number between 0 and 100);
· Second Program Grade (a number between 0 and 100);
· Third Program Grade (a number between 0 and 100);
· First Test Grade (a number between 0 and 100);
· Second Test Grade (a number between 0 and 100);
· Third Test Grade (a number between 0 and 100);
this is what I have thus far which isn't running:

:mad: cpp Syntax (Toggle Plain Text)
//Preprocessor Directives #include<iostream> #include<conio.h> #include<iomanip> #include<fstream> using namespace std; //Global constrants and global variables void headerfn(); void inputfn(ifstream& inputFile,ofstream& outputFile,string names[],int scores[][],int count[]); void totalfn(ifstream& inputFile, ofstream& outputFile,int total[]); void averagesfn(ifstream& inputFile, ofstream& outputFile,float averages[][]); void calculategradefn(char grade[]); //Main function definition void main() { headerfn(); //************************************************************************************************** //block in main //Declare and open/close files fin.open("input.txt"); if(!fin)return; ifstream fin; ofstream fout; char inputFile[51]; char outputFile[51]; cout<<"Welcome to the IT210 Grade Calculator"<<endl; cout<<"Please enter the name/path of input file <e.g.input4.txt>: "; cin>>inputFile; cout<<endl; cout<<"Thank you! You entered"; cin>>fileName; fin.open(inputFile); if (!fin) { cout<<"Cannot open the input file."<<endl; return 1; } cout<<"Enter the output file name: "; cin>>outputFile; cout<<endl; fout.open(outputFile); if (!fout) { cout<<"Cannot open the output file."<<endl; return 1; } fin.close(); fout.close(); //************************************************************************************************ string names[40][2]; int scores[40][6]; int total[40]; float averages[40][3]; char grade[40]; int count; ifstream inputFile; ofstream outputFile; //input fn block void inputfn(string names[][],int scores[][], count); count=0; while (fin&&count<40) {fin>>name[count][0] >>name[count][1]; for (int col=0; col<6;col++) {fin>>scores[count][col];} if (fin.peek()=='\n')fin.ignore(); count++; }//end of while for (int row =0; row<count; row++) { total [row]=0; for (int col=0; col<6; col++) total[row]= total [row] + scores [row][col]; }//end of outer loop for (int row=0; row<count; row++) {cout<<setw(20)<<name[row][0]+' '+name[row][1]; for (int col=0; col<6;col++) {cout<<setw(5)<<scores[row][col];} cout<<setw(5)<<total[row]; cout<<endl; }//end of outer for cout<<"Pres any key to continue..."; getch(); }//end of main //Function Definitions }//end of headerfn //definition of function calculateAverage int calculateAverage(int number1, int number2, int number3) {//start of function calculateAverage int average; average= number1+number2+number3/3; return average; }//end of function calculateAverage //assign whether pass or fail char assignGrade(int average) { if (average > 50) { cout << "Pass:"; } else if(average == average) { cout << "Pass:"; } else { cout << "Fail:"; cout << "Fail:"; } }
Last edited by WaltP; Nov 27th, 2006 at 3:20 am. Reason: Added code tags
Don't use
You're having some errors with these lines of code because you have to declare all dimensions of an array except the first one. This problem also occurs often in later parts of your program. So, for the scores, you would declare an array like
You need to declare
You forgot to declare
I'm unsure what you're trying to do here, as you're redefining
That's pretty much all the errors I can see.
void main. It's old and outdated, and so instead you should use int main.Here are some things wrong with your code:void inputfn(ifstream& inputFile,ofstream& outputFile,string names[],int scores[][],int count[]); void totalfn(ifstream& inputFile, ofstream& outputFile,int total[]); void averagesfn(ifstream& inputFile, ofstream& outputFile,float averages[][]);
int scores[][6] and for averages, you would use int averages[][3].You need to declare
fin before trying to use it: C++ Syntax (Toggle Plain Text)
//block in main //Declare and open/close files fin.open("input.txt"); if(!fin)return; ifstream fin; ofstream fout;
fileName: C++ Syntax (Toggle Plain Text)
cout<<"Thank you! You entered"; cin>>fileName;
string names[40][2];
int scores[40][6];
int total[40];
float averages[40][3];
char grade[40];
int count;
// why are you redeclaring these variables?
ifstream inputFile;
ofstream outputFile;inputfn, so you're not really calling anything, plus count doesn't have a type:void inputfn(string names[][],int scores[][], count); count=0; while (fin&&count<40) // it should be "names" not "name" {fin>>name[count][0] >>name[count][1]; for (int col=0; col<6;col++) {fin>>scores[count][col];} if (fin.peek()=='\n')fin.ignore(); count++; }//end of while for (int row =0; row<count; row++) { total [row]=0; for (int col=0; col<6; col++) total[row]= total [row] + scores [row][col]; }//end of outer loop // again, you're using "name" instead of "names" for (int row=0; row<count; row++) {cout<<setw(20)<<name[row][0]+' '+name[row][1];
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
Don't usevoid main. It's old and outdated, and so instead you should useint main.
•
•
•
•
Originally Posted by faq lite
main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int.Some compilers accept void main(), but that is non-standard and shouldn't be used.
•
•
•
•
Originally Posted by www.eskimo.com
Even if the program with the misdeclared main() "works" (that is, compiles without error, and runs without crashing), it does result in a garbage (random) exit status being returned to the calling environment. You or your command invocation environment may not be noticing that particular glitch right now, but it is a glitch, and it may bite you later.
Hope it cleared the matter.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
void main( ) never was a C / C++ standard right from the interception of the language.
void main(), but thanks for clearing that up. "Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
For example, the latest version of Visual Studio complains that the Standard Template Library is "deprecated", which is total crap.
Maybe it is hoping to create its own C++ standard within the actual standard, and whats more its actually encouraging newbies to use the non portable functions and headers( stdafx ?).
Maybe we should post a sticky at the top of forum saying "Don't listen to MS's crap".
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Maybe it is hoping to create its own C++ standard within the actual standard, and whats more its actually encouraging newbies to use the non portable functions and headers( stdafx ?).
•
•
•
•
Maybe we should post a sticky at the top of forum saying "Don't listen to MS's crap".
http://cboard.cprogramming.com/showthread.php?t=78903
Probably the most important quote (many thanks to Bubba who kindly wrote this thread):
•
•
•
•
Originally Posted by Bubba
To get rid of all that deprecated crap define: _CRT_SECURE_NO_DEPRECATE
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- assigning arrays in VB (Visual Basic 4 / 5 / 6)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Variable pointers or References for .conf parser?
- Next Thread: Need help Writing a code which reads text files.
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






