Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,190 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,141 people online right now. Registration is fast and FREE... Join Now!




Changing into a function

 
Reply to this topicStart new topic

Changing into a function

zandiago
4 Aug, 2007 - 09:15 AM
Post #1

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
CODE
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    int stunum;//# of students in the class
    string stuname;// Name of the student
    float Test1;//score for test 1
    float Test2;//score for test 2
    float Midterm;//score for midterm
    float Final;//score for final
    float Term;//score for term project
    float Grade;
    float NumA=0;
    float NumB=0;
    float NumC=0;
    float NumF=0;
    float Avg1;
    float Avg2;
    float Avg3;
    float Avg4;

    ifstream inFile;
    ofstream outFile;

    inFile.open ("ClassData.dat");
    outFile.open ("ClassOutput");
    inFile>>stunum;//reads in the # of students in the class
    outFile<<"The # of students in the class:"<<stunum<<endl;
    outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(14)<<"Project"<<setw(14)<<"Total Grade"<<setw(14)<<"Letter Grade"<<endl;
    outFile<<& #34;****************************************************************************
****************"<<endl;
    for (int j=0; j<stunum; j++)
            {
            inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;            
            
            Avg1=((Test1+Test2)/2*(.2));//Calculate average test score
            Avg2=(Midterm*(.25));//Calculate average midterm score
            Avg3=(Final*(.3));//calculate the final
            Avg4=(Term*(.25));
            Grade= Avg1+Avg2+Avg3+Avg4;    
    
            outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(14)<<Term<<setw(14)<<Grade;            

                if(Grade>=90)
                {
                outFile<<setw(15)<<" A "<<endl;
                NumA++;
                }
                else if (Grade>=80)
                {
                outFile<<setw(15)<<" B "<<endl;
                NumB++;
                }
                else if (Grade>=70)
                {
                outFile<<setw(15)<<" C "<<endl;
                NumC++;
                }
                else if (Grade<70)
                {
                outFile<<setw(15)<<" F "<<endl;
                NumF++;
                }
    }
            outFile<<endl<<endl;
            outFile<<"The # of A's: "<<NumA<<endl;
            outFile<<"The # of B's: "<<NumB<<endl;
            outFile<<"The # of C's: "<<NumC<<endl;
            outFile<<"The # of F's: "<<NumF<<endl;

inFile.close();
outFile.close();

    return 0;
}

My assignment is as follws:
Accounting teachers, being naturally inept with numbers, have a difficult time calculating students’ averages and assigning letter grades at the end of each semester. Your accounting teacher is no exception. To make this end of semester grading easier, and more accurate, he hires you to write a program (C++) to automate the task. There are a variable number of students in his class, with each students grade being calculated as follows:



2 Tests 10% each

Midterm Exam 25%

Final Exam 30%

Term Project 25%



Input data is stored in a file named ClassData.dat. The first line in the input file has the number of students in the class. Each subsequent line contains one student’s Lastname, test-scores, exam scores and project score. Your program should begin by reading the number of students in the class, and then for each student, read the student’s name and test, exam and project scores. The program should then calculate the student’s average and assign him/her a letter grade (“A” for 90-100; “B” for 80-89; “C” for 70-79; and “F” for 69 and below). Finally, for each student, the program should print, to output file, his/her name, average, and grade.



Once all students have been processed, the program should print a summary report stating the number of “A”s, “B”’s, “C”’s and “F”’s assigned.

----------------------------------------------------------------------------------

I've created a Text file using notepad with the following data(grades are in %):

10
Gayle 86 88 92 96 78
Brown 47 40 57 29 49
Byles 61 49 28 52 71
Holst 71 82 78 70 69
Welsh 71 54 55 80 68
Hamms 93 77 86 74 84
Ellis 88 96 80 87 73
Spear 100 100 85 88 91
Wilks 48 53 62 74 75
Bolop 65 74 73 82 51
------------------------------------------------------------------------------------
How do i change this program(which works) into a program that has functions?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Changing Into A Function
4 Aug, 2007 - 09:21 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
If I understood correctly the program works but you need to modify it with functions, right?
If so then you can define one function say getTheResults() and call this function in the main(), inside that function put the code that does the job.
User is offlineProfile CardPM
+Quote Post

zandiago
RE: Changing Into A Function
4 Aug, 2007 - 09:29 AM
Post #3

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
PennyBoki- you are correct about what I need-that's a good idead, but i'm really new to functions and don't understand it too well, would it be possible for you to edit my post showing what u mean so as to define one function say getTheResults() and call this function in the main(), inside that function put the code. Thanks.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Changing Into A Function
4 Aug, 2007 - 09:42 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
well see if this could work:
CODE
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>

using namespace std;
void getTheResults(void);

int stunum;//# of students in the class
    string stuname;// Name of the student
    float Test1;//score for test 1
    float Test2;//score for test 2
    float Midterm;//score for midterm
    float Final;//score for final
    float Term;//score for term project
    float Grade;
    float NumA=0;
    float NumB=0;
    float NumC=0;
    float NumF=0;
    float Avg1;
    float Avg2;
    float Avg3;
    float Avg4;

    ifstream inFile;
    ofstream outFile;
int main()
{
    
getTheResults();
    

    return 0;
}

void getTheResults()
{
    inFile.open ("ClassData.dat");
    outFile.open ("ClassOutput");
    inFile>>stunum;//reads in the # of students in the class
    outFile<<"The # of students in the class:"<<stunum<<endl;
    outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(14)<<"Project"<<setw(14)<<"Total Grade"<<setw(14)<<"Letter Grade"<<endl;
   // outFile<<& #34<<endl;
    for (int j=0; j<stunum; j++)
            {
            inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;            
            
            Avg1=((Test1+Test2)/2*(.2));//Calculate average test score
            Avg2=(Midterm*(.25));//Calculate average midterm score
            Avg3=(Final*(.3));//calculate the final
            Avg4=(Term*(.25));
            Grade= Avg1+Avg2+Avg3+Avg4;    
    
            outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(14)<<Term<<setw(14)<<Grade;            

                if(Grade>=90)
                {
                outFile<<setw(15)<<" A "<<endl;
                NumA++;
                }
                else if (Grade>=80)
                {
                outFile<<setw(15)<<" B "<<endl;
                NumB++;
                }
                else if (Grade>=70)
                {
                outFile<<setw(15)<<" C "<<endl;
                NumC++;
                }
                else if (Grade<70)
                {
                outFile<<setw(15)<<" F "<<endl;
                NumF++;
                }
    }
            outFile<<endl<<endl;
            outFile<<"The # of A's: "<<NumA<<endl;
            outFile<<"The # of B's: "<<NumB<<endl;
            outFile<<"The # of C's: "<<NumC<<endl;
            outFile<<"The # of F's: "<<NumF<<endl;

inFile.close();
outFile.close();
}

and here is a tutorial on functions in C++
User is offlineProfile CardPM
+Quote Post

zandiago
RE: Changing Into A Function
4 Aug, 2007 - 11:05 AM
Post #5

D.I.C Head
**

Joined: 13 Jul, 2007
Posts: 190


My Contributions
Ok....thx for the help and also for the link for the tutorial..appreciate it.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:26AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month