Thread: Printing From Class/Struct

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Printing From Class/Struct

    Does anyone know why my GenerateReports function is not printing anything from the class/struct? Is it even set up right?

    Code:
    // Example for reading records from the input files for DB.
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const int Max_Stud = 30;  //Max. no. of enrolled students
    
    struct NameType
    {
        string last;
        string first;
        char middle;
    };
    
    struct CourseType 
    {
        string name;
        char grade;
        int credits;
    
    };
    
    struct  StudRecType 
    {
        string ssno;
        NameType name;
        string tel;
        char gender;
        string level;
        int totalCredit;
        float cumGPA;
        int noCo;
        CourseType course[5];
        int semCredit;
        float semGPA;
    };
    
    typedef StudRecType StudDBType[Max_Stud];
        // Type definition for an array of records
    
    class DBType
    {
        public: 
            DBType( );
            void Insert(StudRecType );
            bool GetNext(StudRecType &);
            void Reset( );
        private:
            StudDBType Stud;
            int current;
            int nost;
    };    
    
    DBType::DBType( )
    {
        current = 0;
        nost = 0;
    }
    
    void DBType:: Insert(StudRecType  srec)
    {
        Stud[current] = srec;
        current ++;
        nost ++;
    }
    
    bool  DBType::GetNext (StudRecType & srec)
    {
        if(current > nost)
            return false;
        else
        {
            srec = Stud[current];
            current ++;
            return true;
        }
    }
    
    void DBType::Reset ( )
    {
        current = 0;
    }
    
    void Get_Inf (ifstream&, DBType&, int&);
    
    void  UpdateGPA(StudRecType&);  // Calculate Sem. GPA and Cum GPA etc…
    void  GenerateReports(ofstream&, DBType&, int&);
    
    void GenerateReports(ofstream& printFile, DBType& displayDB, int& noStud)
    {
    	StudRecType student;
    
        int i = 0;
    
        for (i = 1; i < noStud; i++)
        {
    		cout << "SS#: " << student.ssno << endl;
    		cout << "Name(last, first, mid): " << student.name.last << " " << student.name.last << " " << student.name.middle << endl;
    		cout << "Telphon Number: " << student.tel << endl;
    		cout << "Gender: " << student.gender;
    		cout << "Class Level: " << student.level;
    		if (student.noCo > 0)
    		{
    			cout << "Registration of Spring 2008: Yes" << endl;
    			int j = 0;
    
    			for (j = 0; j < student.noCo; j++)
    			{
    				cout << "Unoffical Report Card" << endl;
    				cout << "Course: " << j << endl;
    				cout << student.course[j].name;
    				cout << "Credits: " << endl; 
    				cout << student.course[j].credits << endl;
    				cout << "Grade: " << endl;
    				cout << student.course[j].grade << endl;
    
    				displayDB.GetNext(student);
    			}
    			
    			cout << "Credits for Spring: " << student.semCredit << endl;
    			cout << "Spring Semester GPA: " << student.semGPA << endl;
    			cout << "Total Credits: " << student.totalCredit + student.semCredit << endl;
    			cout << "New cumulative GPA: " << student.cumGPA << endl;
    		}
    
    		else
    		{
    			cout << "Registration of Spring 2008: No";
    		}
    		
    		displayDB.GetNext(student);
    
    	}
    
    }
    void UpdateGPA(StudRecType& fGPA)
    {
        int count = 0;
        int i = 0;
        float gradeA = 4.0;
        float gradeB = 3.0;
        float gradeC = 2.0;
        float gradeD = 1.0;
        float gradeF = 0.0;
    
        float heldGPA = 0.0;
        float totalGPA = 0.0;
    
        count = fGPA.noCo;
    
        int amtClass = 0;
        
        if (fGPA.noCo != 0)
        {
            for (i = 0; i < count; i++)
            {
                if (fGPA.course[i].grade == 'A')
                {
                    heldGPA =gradeA * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'B')
                {            
                    heldGPA =gradeB * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'C')
                {
                    heldGPA =gradeC * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'D')
                {            
                    heldGPA =gradeD * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'F')
                {            
                    heldGPA =gradeF * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'W')
    
                 totalGPA = (heldGPA + totalGPA);
            }
                fGPA.semGPA = (totalGPA / amtClass);
                fGPA.cumGPA = (totalGPA + fGPA.cumGPA*fGPA.totalCredit)/ (amtClass+fGPA.totalCredit);
        }
    
    }
    int main()
    {
        ifstream inStud;
        ofstream outFile;
        //int choice;
        DBType StudDB;
    	int noStud = 0;
    
        inStud.open("Stud.txt");
        
        if (!inStud)
        {
            cout <<"**Can't open input file Stud**"<< endl;
            return 1;
        }
    
        outFile.open("Echo.dat");
        if(!outFile)
        {
            cout <<"** Can't open output file **"<< endl;
            return 1;
        }
        Get_Inf(inStud, StudDB, noStud);
    
    
        cout << "Total no. of Students is "<<noStud<<endl;
        cout<<endl;
        GenerateReports(outFile, StudDB, noStud);
        return 0;
    }
    
    //*******************************************************************
    void Get_Inf(
        /* in  */ ifstream&     inF1,
        /* out */ DBType&      DB,
    	/* Number of students */ int& noStud)
    {
        int j;
        int jco;
        StudRecType SR;
    
    	noStud = 0;
        while(!inF1.eof())
        {
            inF1>>SR.ssno;
            inF1>>SR.name.last>>SR.name.first>>SR.name.middle;
            inF1>>SR.tel>>SR.gender>>SR.level;
            inF1>>SR.totalCredit>>SR.cumGPA;
            
            //Echo input data on the screen
            cout<<SR.ssno<<endl;                    
            cout<<SR.name.last<<"*"<<SR.name.first;
            cout<<"*"<<SR.name.middle<<endl;
            cout<<SR.gender<<"*"<<SR.level<<endl;
            cout<<SR.totalCredit<<"*"<<SR.cumGPA<<endl;
    
            inF1>>jco;
            SR.noCo = jco;
            for (j = 0;j < jco; j++)
            {                                        
                inF1>>SR.course[j].name;
                inF1>>SR.course[j].credits;
                inF1>>SR.course[j].grade;
                cout<<SR.course[j].name
                    <<","<<SR.course[j].credits<<","
                    <<SR.course[j].grade<<endl;
    
            }
            noStud++;
            UpdateGPA(SR);
            DB.Insert (SR);
            inF1.ignore(100,'\n');
            cin.get();
        }    
    }
    Last edited by GCNDoug; 03-05-2008 at 05:40 PM. Reason: Function Edit

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Ok I fixed the calculate GPA function so that all of the held values should be correct I'm just have trouble displaying them now.....

    EXAMPLE OUTPUT
    Example Output format for task 2:
    (NOTE: If the student didn't take any course in Spring 08 semester, just display first 5 lines.)

    SS# : 111111111
    Name: JOHNSON, JAME L
    Tel. : 7632134
    Gender: Male
    Class Level: Freshman
    Registration of Spring 2008: Yes

    Unofficial Report Card

    COURSE CREDITS GRADE
    ======= ======= ======
    COSC237 3 B
    BUSC372 3 C
    DHSC362 5 C
    CIOS100 3 W
    POSC320 2 F

    Credits for Spring 08 : 13
    Spring 08 Semester GPA : y.yy
    Total Credits: 29
    New Cumulative GPA : x.xx

    (Doesn not need to be formatted exactly like this, atm I can't get anything to print though)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Anyone... Any help at all?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you say you can't get anything to print -- nothing at all, not even the debug things like "number of students"? What are you not getting that you want to get?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    For instance I can't get "student.ssno" and others from the struct/class to print. The actual words to define the variable will print "SSN#" However nothing containing data will print in the generate report display.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you checked the result of GetNext()? I'll let you figure out why.

    I suspect you will find it returning false. Also, the first iteration of the loop in GenerateReports will write "rubbish", as the student record is uninitilized.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    The function for getnext should be right as it was given to us by the professor, I haven't touched it. Also I don't understand why the first iteration would not work. I will keep working on it thanks for the help!.

    Edit: I am actually starting to think that I don't need to call getnext within the second forloop for the printing of the courses although I am not sure.
    Last edited by GCNDoug; 03-06-2008 at 08:15 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are right that you don't need to call GetNext twice in the big loop [at least, I don't think that's right].

    And I don't mean to say that GetNext() as a function is broken, in and of itself. I believe it's the way that you use it that makes it "not work".

    However to your two objections:
    1. What is the value of "student" in the first iteration of generatereports?
    2. What value does GetNext() return? True or False?
    [It is ALWAYS a good idea to check the return value from a function, particularly if it gives you some data back that you rely on].

    I also, looking further at your code, I realize that you are using a form where you abbreviate "number" to "no". This is something that can easily be misconstrued - much better to use n, num or number - e.g. instead of noCo, use numCo. "noSomething" is easily misunderstood as "a flag to say we don't want Something".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Ok so I have still been working on this and even asked the professor he told me to call the class differently within the generatereport function. Any ideas.

    Code:
    // Example for reading records from the input files for DB.
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const int Max_Stud = 30;  //Max. no. of enrolled students
    
    struct NameType
    {
        string last;
        string first;
        char middle;
    };
    
    struct CourseType 
    {
        string name;
        char grade;
        int credits;
    
    };
    
    struct  StudRecType 
    {
        string ssno;
        NameType name;
        string tel;
        char gender;
        string level;
        int totalCredit;
        float cumGPA;
        int noCo;
        CourseType course[5];
        int semCredit;
        float semGPA;
    };
    
    typedef StudRecType StudDBType[Max_Stud];
        // Type definition for an array of records
    
    class DBType
    {
        public: 
            DBType( );
            void Insert(StudRecType );
            bool GetNext(StudRecType &);
            void Reset( );
        private:
            StudDBType Stud;
            int current;
            int nost;
    };    
    
    DBType::DBType( )
    {
        current = 0;
        nost = 0;
    }
    
    void DBType:: Insert(StudRecType  srec)
    {
        Stud[current] = srec;
        current ++;
        nost ++;
    }
    
    bool  DBType::GetNext (StudRecType & srec)
    {
        if(current > nost)
            return false;
        else
        {
            srec = Stud[current];
            current ++;
            return true;
        }
    }
    
    void DBType::Reset ( )
    {
        current = 0;
    }
    
    void Get_Inf (ifstream&, DBType&, int&);
    
    void  UpdateGPA(StudRecType&);  // Calculate Sem. GPA and Cum GPA etc…
    void  GenerateReports(ofstream&, DBType&, int&);
    
    void GenerateReports(ofstream& printFile, DBType& displayDB, int& noStud)
    {
    	StudRecType student;
    
        int i = 0;
    	displayDB.Reset(student)
    
        for (i = 1; i < noStud; i++)
        {
    		displayDB.GetNext(student)
    		cout << "SS#: " << student.ssno << endl;
    		cout << "Name(last, first, mid): " << student.name.last << " " << student.name.last << " " << student.name.middle << endl;
    		cout << "Telphon Number: " << student.tel << endl;
    		cout << "Gender: " << student.gender;
    		cout << "Class Level: " << student.level;
    		if (student.noCo > 0)
    		{
    			cout << "Registration of Spring 2008: Yes" << endl;
    			int j = 0;
    
    			for (j = 0; j < student.noCo; j++)
    			{
    				cout << "Unoffical Report Card" << endl;
    				cout << "Course: " << j << endl;
    				cout << student.course[j].name;
    				cout << "Credits: " << endl; 
    				cout << student.course[j].credits << endl;
    				cout << "Grade: " << endl;
    				cout << student.course[j].grade << endl;
    			}
    			
    			cout << "Credits for Spring: " << student.semCredit << endl;
    			cout << "Spring Semester GPA: " << student.semGPA << endl;
    			cout << "Total Credits: " << student.totalCredit + student.semCredit << endl;
    			cout << "New cumulative GPA: " << student.cumGPA << endl;
    		}
    
    		else
    		{
    			cout << "Registration of Spring 2008: No";
    		}
    		
    
    	}
    
    }
    void UpdateGPA(StudRecType& fGPA)
    {
        int count = 0;
        int i = 0;
        float gradeA = 4.0;
        float gradeB = 3.0;
        float gradeC = 2.0;
        float gradeD = 1.0;
        float gradeF = 0.0;
    
        float heldGPA = 0.0;
        float totalGPA = 0.0;
    
        count = fGPA.noCo;
    
        int amtClass = 0;
        
        if (fGPA.noCo != 0)
        {
            for (i = 0; i < count; i++)
            {
                if (fGPA.course[i].grade == 'A')
                {
                    heldGPA =gradeA * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'B')
                {            
                    heldGPA =gradeB * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'C')
                {
                    heldGPA =gradeC * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'D')
                {            
                    heldGPA =gradeD * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'F')
                {            
                    heldGPA =gradeF * fGPA.course[i].credits;
                    amtClass= amtClass + fGPA.course[i].credits;
                }
                if (fGPA.course[i].grade == 'W')
    
                 totalGPA = (heldGPA + totalGPA);
            }
                fGPA.semGPA = (totalGPA / amtClass);
                fGPA.cumGPA = (totalGPA + fGPA.cumGPA*fGPA.totalCredit)/ (amtClass+fGPA.totalCredit);
        }
    
    }
    int main()
    {
        ifstream inStud;
        ofstream outFile;
        //int choice;
        DBType StudDB;
    	int noStud = 0;
    
        inStud.open("Stud.txt");
        
        if (!inStud)
        {
            cout <<"**Can't open input file Stud**"<< endl;
            return 1;
        }
    
        outFile.open("Echo.dat");
        if(!outFile)
        {
            cout <<"** Can't open output file **"<< endl;
            return 1;
        }
        Get_Inf(inStud, StudDB, noStud);
    
    
        cout << "Total no. of Students is "<<noStud<<endl;
        cout<<endl;
        GenerateReports(outFile, StudDB, noStud);
        return 0;
    }
    
    //*******************************************************************
    void Get_Inf(
        /* in  */ ifstream&     inF1,
        /* out */ DBType&      DB,
    	/* Number of students */ int& noStud)
    {
        int j;
        int jco;
        StudRecType SR;
    
    	noStud = 0;
        while(!inF1.eof())
        {
            inF1>>SR.ssno;
            inF1>>SR.name.last>>SR.name.first>>SR.name.middle;
            inF1>>SR.tel>>SR.gender>>SR.level;
            inF1>>SR.totalCredit>>SR.cumGPA;
            
            //Echo input data on the screen
            cout<<SR.ssno<<endl;                    
            cout<<SR.name.last<<"*"<<SR.name.first;
            cout<<"*"<<SR.name.middle<<endl;
            cout<<SR.gender<<"*"<<SR.level<<endl;
            cout<<SR.totalCredit<<"*"<<SR.cumGPA<<endl;
    
            inF1>>jco;
            SR.noCo = jco;
            for (j = 0;j < jco; j++)
            {                                        
                inF1>>SR.course[j].name;
                inF1>>SR.course[j].credits;
                inF1>>SR.course[j].grade;
                cout<<SR.course[j].name
                    <<","<<SR.course[j].credits<<","
                    <<SR.course[j].grade<<endl;
    
            }
            noStud++;
            UpdateGPA(SR);
            DB.Insert (SR);
            inF1.ignore(100,'\n');
            cin.get();
        }    
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so can you PLEASE ADD A CHECK to see the return value from GetNext()?

    I'm 99% sure that's what the problem stems from [assuming you're still struggling to print something sensible].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing using DrawText() or TextOut()
    By Eversman in forum Windows Programming
    Replies: 1
    Last Post: 05-24-2004, 12:12 PM