Using functions and arrays to create a program for grades

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 1
Reputation: DeeIT is an unknown quantity at this point 
Solved Threads: 0
DeeIT DeeIT is offline Offline
Newbie Poster

Using functions and arrays to create a program for grades

 
0
  #1
Nov 26th, 2006
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:
  • 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.
Note: You must check for file input/output stream failure.


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:
  1. //Preprocessor Directives
  2.  
  3. #include<iostream>
  4. #include<conio.h>
  5. #include<iomanip>
  6. #include<fstream>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. //Global constrants and global variables
  12. void headerfn();
  13. void inputfn(ifstream& inputFile,ofstream& outputFile,string names[],int scores[][],int count[]);
  14. void totalfn(ifstream& inputFile, ofstream& outputFile,int total[]);
  15. void averagesfn(ifstream& inputFile, ofstream& outputFile,float averages[][]);
  16. void calculategradefn(char grade[]);
  17.  
  18. //Main function definition
  19.  
  20. void main()
  21. {
  22. headerfn();
  23. //**************************************************************************************************
  24. //block in main
  25. //Declare and open/close files
  26. fin.open("input.txt");
  27. if(!fin)return;
  28. ifstream fin;
  29. ofstream fout;
  30.  
  31. char inputFile[51];
  32. char outputFile[51];
  33.  
  34. cout<<"Welcome to the IT210 Grade Calculator"<<endl;
  35. cout<<"Please enter the name/path of input file <e.g.input4.txt>: ";
  36. cin>>inputFile;
  37. cout<<endl;
  38.  
  39. cout<<"Thank you! You entered";
  40. cin>>fileName;
  41.  
  42. fin.open(inputFile);
  43. if (!fin)
  44. {
  45. cout<<"Cannot open the input file."<<endl;
  46. return 1;
  47. }
  48.  
  49.  
  50. cout<<"Enter the output file name: ";
  51. cin>>outputFile;
  52. cout<<endl;
  53.  
  54. fout.open(outputFile);
  55. if (!fout)
  56. {
  57. cout<<"Cannot open the output file."<<endl;
  58. return 1;
  59. }
  60.  
  61. fin.close();
  62. fout.close();
  63. //************************************************************************************************
  64.  
  65.  
  66.  
  67. string names[40][2];
  68. int scores[40][6];
  69. int total[40];
  70. float averages[40][3];
  71. char grade[40];
  72. int count;
  73. ifstream inputFile;
  74. ofstream outputFile;
  75.  
  76.  
  77. //input fn block
  78. void inputfn(string names[][],int scores[][], count);
  79. count=0;
  80. while (fin&&count<40)
  81. {fin>>name[count][0]
  82. >>name[count][1];
  83.  
  84. for (int col=0; col<6;col++)
  85. {fin>>scores[count][col];}
  86.  
  87. if (fin.peek()=='\n')fin.ignore();
  88. count++;
  89. }//end of while
  90.  
  91. for (int row =0; row<count; row++)
  92. {
  93. total [row]=0;
  94. for (int col=0; col<6; col++)
  95. total[row]= total [row] + scores [row][col];
  96. }//end of outer loop
  97.  
  98. for (int row=0; row<count; row++)
  99. {cout<<setw(20)<<name[row][0]+' '+name[row][1];
  100. for (int col=0; col<6;col++)
  101. {cout<<setw(5)<<scores[row][col];}
  102. cout<<setw(5)<<total[row];
  103. cout<<endl;
  104. }//end of outer for
  105.  
  106. cout<<"Pres any key to continue...";
  107. getch();
  108.  
  109. }//end of main
  110.  
  111. //Function Definitions
  112.  
  113. }//end of headerfn
  114.  
  115.  
  116. //definition of function calculateAverage
  117. int calculateAverage(int number1, int number2, int number3)
  118. {//start of function calculateAverage
  119. int average;
  120. average= number1+number2+number3/3;
  121. return average;
  122. }//end of function calculateAverage
  123.  
  124.  
  125.  
  126. //assign whether pass or fail
  127.  
  128. char assignGrade(int average)
  129. {
  130. if (average > 50) {
  131. cout << "Pass:";
  132. }
  133. else if(average == average) {
  134. cout << "Pass:";
  135. }
  136. else {
  137. cout << "Fail:";
  138. cout << "Fail:";
  139. }
  140. }
Last edited by WaltP; Nov 27th, 2006 at 3:20 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using functions and arrays to create a program for grades

 
0
  #2
Nov 26th, 2006
please edit your post and enclose the code in code tags. Read the links in my signature if you do not know how to do that.

Also what are specific questions?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Using functions and arrays to create a program for grades

 
0
  #3
Nov 26th, 2006
Don't use 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[][]);
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 int scores[][6] and for averages, you would use int averages[][3].

You need to declare fin before trying to use it:
  1. //block in main
  2. //Declare and open/close files
  3. fin.open("input.txt");
  4. if(!fin)return;
  5. ifstream fin;
  6. ofstream fout;
You forgot to declare fileName:
  1. cout<<"Thank you! You entered";
  2. 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;
I'm unsure what you're trying to do here, as you're redefining 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];
That's pretty much all the errors I can see.
"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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Using functions and arrays to create a program for grades

 
0
  #4
Nov 27th, 2006
Originally Posted by joeprogrammer View Post
Don't use void main. It's old and outdated, and so instead you should use int main.
void main( ) never was a C / C++ standard right from the interception of the language.

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Using functions and arrays to create a program for grades

 
0
  #5
Nov 27th, 2006
Originally Posted by ~s.o.s~ View Post
void main( ) never was a C / C++ standard right from the interception of the language.
What I meant is that Visual C++ is very old, and has been "encouraging" its own standards right from the beginning... (For example, the latest version of Visual Studio complains that the Standard Template Library is "deprecated", which is total crap.) In other words, Visual C++ never complained about 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Using functions and arrays to create a program for grades

 
0
  #6
Nov 27th, 2006
Originally Posted by joeprogrammer View Post
For example, the latest version of Visual Studio complains that the Standard Template Library is "deprecated", which is total crap.
Didnt know about this one but it does the same thing with scanf( ).

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Using functions and arrays to create a program for grades

 
0
  #7
Nov 27th, 2006
Originally Posted by ~s.o.s~ View Post
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 ?).
Yes, I think that is basically Microsoft's idea. They go against some of the C++ standards, and say, "this isn't our standard".

Maybe we should post a sticky at the top of forum saying "Don't listen to MS's crap".
Actually, that may not be such a bad idea. I don't know if you ever read CBoard, but at the moment they have a sticky in the General Discussions on how to port your apps to Visual Studio 2005:
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
And sorry, I am getting a little bit off-topic.
"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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC