Thread: Class program won't run (identifier undefined and deleted function errors)

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    4

    Class program won't run (identifier undefined and deleted function errors)

    Hello, would someone mind taking a look at this as this program will not run. The input file is filled vertically with words/data. I have taken initiative to move things around, redefine them, but this stage of the program is the best I have gotten it to.

    I am getting these errors:
    1 IntelliSense: identifier "Total_Examgrade" is undefined

    2 IntelliSense: identifier "Total_Labgrade" is undefined

    3 IntelliSense: identifier "Final_grade" is undefined

    4 IntelliSense: identifier "Average_Examgrade" is undefined

    5 IntelliSense: identifier "Average_Labgrade" is undefined

    6 IntelliSense: function "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 1016 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function



    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    const int size = 20;//change here to change array size
    
    class Student//declares class type Student
    
    {
    public:
        string Firstname, Middlename, Lastname;
        float GPA;//each student has 1 GPA
        char Lettergrade;    
        int Examgrade[3];
        //holds Examgrade1, Examgrade2, Examgrade3;
        int Labgrade[4];
        //holds Labgrade1, Labgrade2, Labgrade3, Labgrade4;
    
        //function declarations
        void Initialize(Student Compstud[]);
        //function initializes all the elements
        void Read_Data(ifstream& In_Data, Student Compstud[]);
        //function read values from a file, these values will be used for calculations
        //postcondition: replaces 0 initialization with those values
        float Calc_GPA(int Examgrade[], int Labgrade[], Student Compstud[]);
        //function calculates the GPA of each of the students
        //Postcondition: Able to use the return values of this function to assign letter grade
        char LetterGrade(float GPA, Student Compstud[]);
        //Precondition: Student has a GPA calculated
        //function outputs a letter grade according to the student's calculated GPA
        //Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure
        float calc_ClassAverage(float GPA, Student Compstud[]);
        //Precondition: Each student has a GPA calculated
        //function uses all student's GPA to calculates average grade of the class
        //use pointer to point to each student's GPA and sum them up
        void Display_File(ofstream Out_Data, Student Compstud[]);
        //Postconditions: 
        //Outputs each of the 20 students' full name, 3 exam grades,
        //each of the 4 lab grades, and final grade to an output file
        //Outputs the average GPA of whole class
        void Display_Screen(Student Compstud[]);
        //Postconditions: 
        //Outputs each of the 20 students' full name, 3 exam grades,
        //each of the 4 lab grades, and final grade to the screen
        //Outputs the average GPA of whole class
    };
    
    //to "access" any student's ID number: Student.IDnumber
    //to "access" a student's 1st exam grade: Student.Examgrade[0]
    
    Student Compstud[size];
    //initializes an array within type Student to hold 'size' Compsci students
    
    ifstream In_Data;
    ofstream Out_Data;
    float ClassAverage;
    
    int main()
    {
        //function calls begin, Still getting the "identifier" is UNDEFINED
        Compstud[size].Read_Data(In_Data, Compstud);
        Compstud[size].Calc_GPA(Examgrade, Labgrade, Compstud);
        Compstud[size].LetterGrade(GPA, Compstud);
        Compstud[size].calc_ClassAverage(GPA, Compstud);
        Compstud[size].Display_File(Out_Data, Compstud);
        Compstud[size].Display_Screen(Compstud);
    }
    
    void Student:: Read_Data(ifstream& In_Data, Student Compstud[])
    {
        In_Data.open("inputlab10.txt");
        if (!In_Data.is_open())
            exit(1);
    
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            In_Data >> Compstud[i].Firstname >> Compstud[i].Middlename >> Compstud[i].Lastname;
    
            for (int j = 0; j < 3; j++)//loop and read in exam grades
            {
                In_Data >> Compstud[i].Examgrade[j];
            }
            for (int k = 0; k < 4; k++)//loop and read in lab grades
            {
                In_Data >> Compstud[i].Labgrade[k];
            }
        }
        In_Data.close();
        return;
    }
    
    float Student:: Calc_GPA(int Examgrade[], int Labgrade[], Student Compstud[])
    {
        int sum1, sum2;//holds value of sums    
        float StudGPA;
    
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            for (int j = 0; j < size; j++)
            {
                sum1 += Compstud[i].Examgrade[j];//loop and add up student's exam grades
            }
            for (int k = 0; k < size; k++)
            {
                sum2 += Compstud[i].Labgrade[k];//loop and add up student's lab grades
            }
            StudGPA = ((.60 * sum1) + (.40 * sum2) / 2);
            Compstud[i].GPA = ((.60 * sum1) + (.40 * sum2) / 2);
        } return StudGPA;//student (element's) calculated GPA
    }
    
    char Student:: LetterGrade(float GPA, Student Compstud[])
    {
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            char letter = 0;//variable to hold the letter
            if (Compstud[i].GPA > 90)
                letter = 'A';
            else if (Compstud[i].GPA > 80)
                letter = 'B';
            else if (Compstud[i].GPA > 70)
                letter = 'C';
            else if (Compstud[i].GPA > 60)
                letter = 'D';
            else
                letter = 'F';
            char Lettergrade = letter;//assign the Lettergrade value of letter.
            return Lettergrade;
        }
    }
    
    float Student:: calc_ClassAverage(float GPA, Student Compstud[])
    {
        float GPAsum;//hold all student's GPA
    
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            //sum all student's GPAs and store value in GPAsum
            GPAsum += Compstud[i].GPA;
    
            ClassAverage = (GPAsum / size);
            return ClassAverage;
        }
    }
    
    void Student:: Display_File(ofstream Out_Data, Student Compstud[])
    {
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            Out_Data.open("outputlab10.txt");
            if (!Out_Data.is_open())
                exit(1);
            //write each student's record to file
            Out_Data <<
                Compstud[i].Firstname << "   " << Compstud[i].Middlename << "   " << Compstud[i].Lastname << "   "
                << Compstud[i].GPA << "   " << Compstud[i].Lettergrade << "    ";
            for (int j = 0; j < 3; i++)//loop and output exam grades
            {
                Out_Data << Compstud[i].Examgrade[j];
            }
            for (int k = 0; k < 4; i++)//loop and output lab grades
            {
                Out_Data << Compstud[i].Labgrade[k];
            }
            Out_Data.close();
            return;
        }
    }
    
    void Student::Display_Screen(Student Compstud[])
    {
        const int width = 6;
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            cout << "Name:      " << Compstud[i].Firstname << setw(width) << Compstud[i].Middlename << setw(width)
                << Compstud[i].Lastname << endl;
            cout << "Exam Grades:      " << Compstud[i].Examgrade[3] << setw(width) << endl;
            cout << "Lab Grades:      " << Compstud[i].Labgrade[4] << setw(width) << endl;
            cout << "GPA:      " << Compstud[i].GPA << endl;
            cout << "Grade:      " << Compstud[i].Lettergrade << endl;
    
            cout << "The class grade average was" << ClassAverage << endl;
            return;
        }
    
        for (int i = 0; i < size; i++)//loop through each student in array
        {
            Out_Data.open("outputlab10.txt");
            if (!Out_Data.is_open())
                exit(1);
            //write each student's record to file
            Out_Data <<
                Compstud[i].Firstname << "   " << Compstud[i].Middlename << "   " << Compstud[i].Lastname << "   "
                << Compstud[i].GPA << "   " << Compstud[i].Lettergrade << "    ";
            for (int j = 0; j < 3; i++)//loop and output exam grades
            {
                Out_Data << Compstud[i].Examgrade[j];
            }
            for (int k = 0; k < 4; i++)//loop and output lab grades
            {
                Out_Data << Compstud[i].Labgrade[k];
            }
            Out_Data.close();
            return;
        }
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    your program is really hard to read understand , so for me , It's really hard helping you.
    few notes :
    1) why every member starts with capital letter ? there is not such convention. in C++ , classes usually start with upper camel case and member variables and functions - lower camel case .
    2) your class has no constructor . even if no special initialization is needed , it's best to declare empty constructor .
    3) you try to excess the 20th element in your array , where the array ends in the 19th element. remember that in arrays the count starts with 0 . even if the program compiles - you well get segmentation fault and you won't understand why .
    4) I don't get what "outputlab10.txt" should contain , and why simple >> should put the right value in the right variable?
    5) your member functions get array of the class they declared in as parameter , it shows you don't understand correctly the idea of classes. "fill the gas tank" is a function a Car has , a car doesn't need a row of cars in order to get filled with gas..
    6) simple , non-recursive functions like yours don't need "return" at the end of the function - the function does it automatically

    try to change your program first according to my advices , then ask again
    Last edited by Dave11; 12-22-2014 at 02:30 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Rather than rely on intellisense, please compile and tell us what errors are reported by your compiler, including line numbers.

    Your functions that work on arrays of Student objects should be non-members, and your member functions that work on a single student object should not take a Student object as an argument: it should work on the *this object. Oh, and please avoid global variables.

    Dave11: camel case is not a convention in C++ either. This is just a matter of subjective style. Likewise, defining an empty constructor is unnecessary, though one might do so in anticipation of other constructors being added in the future.

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    Thank you!

    What do you mean by "it should work on the *this object."?

    For "3) you try to excess the 20th element in your array , where the array ends in the 19th element. remember that in arrays the count starts with 0 . even if the program compiles - you well get segmentation fault and you won't understand why."

    I do understand that concept about arrays, can you point to a piece of code where you see this is a problem?

    "outputlab10.txt" should contain a list of 20 students' full names, exam grades, lab grades, GPA and lettergrade. The data from the input file is used to calculate the GPA and assign the lettergrade. Those 2 items are not in the input file but are included in the output file.

    I wanted to make my program so that the items of which there are multiples of (exam grades and labgrades) are looped through rather than writing cout << Compstud[i].Labgrade[0], Compstud[i].Labgrade[1], Compstud[i].Labgrade[2] etc....does that make sense? Is this not possible perhaps?

    I see what you mean with the member functions/parameters and changed them.

    Now the errors I get when I compile this new version below are:
    Error 5 error LNK2019: unresolved external symbol "public: __thiscall Student::Student(void)" (??0Student@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'Compstud''(void)" (??__ECompstud@@YAXXZ)
    Error 6 error LNK1120: 1 unresolved externals





















    Code:
    #include
    Code:
    <iostream>
    
     
     
     
     
    #include
     
    <fstream>
    
     
     
     
     
    #include
     
    <iomanip>
    
     
     
     
     
    #include
     
    <string>
    
    
     
     
     
     
    using
     
    namespace std;
    
    
     
     
     
     
    ifstream
     
     In_Data;
    
     
     
     
     
    ofstream
     
     Out_Data;
    
    
     
     
     
     
    const
     
    int size = 20;//change here to change array size
    
    
     
     
     
     
    class
     
    Student//declares class type Student
    
     
     
    
     
    {
    
     
     
    public
     
    :
     
     
    
     
    	
     
    string Firstname, Middlename, Lastname;
     
     
    
     
    	
     
    float GPA;//each student has 1 GPA
    
     
     
     
    	
     
    char Lettergrade;
     
     
    
     
    	
     
    int Examgrade[3];
     
     
    
     
    	
     
    //holds Examgrade1, Examgrade2, Examgrade3;
    
     
     
     
    	
     
    int Labgrade[4];
     
     
    
     
    	
     
    //holds Labgrade1, Labgrade2, Labgrade3, Labgrade4;
    
     
     
    
     
    	
     
    //member function declarations
    
     
     
     
    	Student();
     
    //empty constructor
    
     
     
     
    	
     
    void Initialize();
     
     
    
     
    	
     
    //function initializes all the elements
    
     
     
     
    	
     
    void Read_Data(ifstream& In_Data);
     
     
    
     
    	
     
    //function read values from a file, these values will be used for calculations
    
     
     
     
    	
     
    //postcondition: replaces 0 initialization with those values
    
     
     
     
    	
     
    float Calc_GPA();
     
     
    
     
    	
     
    //function calculates the GPA of each of the students
    
     
     
     
    	
     
    //Postcondition: Able to use the return values of this function to assign letter grade
    
     
     
     
    	
     
    char LetterGrade();
     
     
    
     
    	
     
    //Precondition: Student has a GPA calculated
    
     
     
     
    	
     
    //function outputs a letter grade according to the student's calculated GPA
    
     
     
     
    	
     
    //Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure
    
     
     
     
    	
     
    float calc_ClassAverage();
     
     
    
     
    	
     
    //Precondition: Each student has a GPA calculated
    
     
     
     
    	
     
    //function uses all student's GPA to calculates average grade of the class
    
     
     
     
    	
     
    //use pointer to point to each student's GPA and sum them up
    
     
     
     
    	
     
    void Display_File(ofstream& Out_Data);
     
     
    
     
    	
     
    //Postconditions: 
    
     
     
     
    	
     
    //Outputs each of the 20 students' full name, 3 exam grades,
    
     
     
     
    	
     
    //each of the 4 lab grades, and final grade to an output file
    
     
     
     
    	
     
    //Outputs the average GPA of whole class
    
     
     
     
    	
     
    void Display_Screen();
     
     
    
     
    	
     
    //Postconditions: 
    
     
     
     
    	
     
    //Outputs each of the 20 students' full name, 3 exam grades,
    
     
     
     
    	
     
    //each of the 4 lab grades, and final grade to the screen
    
     
     
     
    	
     
    //Outputs the average GPA of whole class
    
     
     
     
    };
    
    
     
     
    //to "access" any student's ID number: Student.IDnumber
    
     
    //to "access" a student's 1st exam grade: Student.Examgrade[0]
    
     
     
     
    
     
     
     
     
    Student
     
     Compstud[size];
    
     
     
     
     
    //initializes an array within type Student to hold 'size' Compsci students
    
     
     
     
    
     
     
     
     
    float
     
     ClassAverage;
    
    
     
     
     
     
    int
     
     main()
     
     
    
     
    {
    
     
    	
    //function calls begin, Still getting the "identifier" is UNDEFINED
    
     
     
     
    	Compstud[size].Read_Data(In_Data);
    
     
    	Compstud[size].Calc_GPA();
    
     
    	Compstud[size].LetterGrade();
    
     
    	Compstud[size].calc_ClassAverage();
    
     
    	Compstud[size].Display_File(Out_Data);
    
     
    	Compstud[size].Display_Screen();
    
     
    }
    
    
     
     
    void
     
    Student::Read_Data(ifstream& In_Data)
     
     
    
     
    {
    
     
    	
    In_Data.open("inputlab10.txt");
     
     
    
     
    	
     
    if (!In_Data.is_open())
     
     
    
     
    		exit(1);
    
    
     
    	
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		
    In_Data >> Compstud[i].Firstname >> Compstud[i].Middlename >> Compstud[i].Lastname;
     
     
    
    
     
    		
     
    for (int j = 0; j < 3; j++)//loop and read in exam grades
    
     
     
     
    		{
    
     
    			
    In_Data >> Compstud[i].Examgrade[j];
     
     
    
     
    		}
    
     
    		
    for (int k = 0; k < 4; k++)//loop and read in lab grades
    
     
     
     
    		{
    
     
    			
    In_Data >> Compstud[i].Labgrade[k];
     
     
    
     
    		}
    
     
    	}
    
     
    	
    In_Data.close();
     
     
    
     
    }
    
    
     
     
    float
     
    Student::Calc_GPA()
     
     
    
     
    {
    
     
    	
    int sum1 = 0, sum2 = 0;//holds value of sums	
    
     
     
     
    	
     
    float StudGPA;
     
     
    
    
     
    	
     
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		
    for (int j = 0; j < 3; j++)
     
     
    
     
    		{
    
     
    			sum1 += Compstud[i].Examgrade[j];
    //loop and add up student's exam grades
    
     
     
     
    		}
    
     
    		
    for (int k = 0; k < 4; k++)
     
     
    
     
    		{
    
     
    			sum2 += Compstud[i].Labgrade[k];
    //loop and add up student's lab grades
    
     
     
     
    		}
    
     
    		StudGPA = ((.60 * sum1) + (.40 * sum2) / 2);
    
     
    		Compstud[i].GPA = ((.60 * sum1) + (.40 * sum2) / 2);
    
     
    	} 
    return StudGPA;//student (element's) calculated GPA
    
     
     
     
    }
    
    
     
     
    char
     
    Student::LetterGrade()
     
     
    
     
    {
    
     
    	
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		
    char letter = 0;//variable to hold the letter
    
     
     
     
    		
     
    if (Compstud[i].GPA > 90)
     
     
    
     
    			letter = 
     
    'A';
     
     
    
     
    		
     
    elseif (Compstud[i].GPA > 80)
     
     
    
     
    			letter = 
     
    'B';
     
     
    
     
    		
     
    elseif (Compstud[i].GPA > 70)
     
     
    
     
    			letter = 
     
    'C';
     
     
    
     
    		
     
    elseif (Compstud[i].GPA > 60)
     
     
    
     
    			letter = 
     
    'D';
     
     
    
     
    		
     
    else
    
     
     
     
    			letter = 
     
    'F';
     
     
    
     
    		
     
    char Lettergrade = letter;//assign the Lettergrade value of letter.
    
     
     
     
    		
     
    return Lettergrade;
     
     
    
     
    	}
    
     
    }
    
    
     
     
    float
     
    Student::calc_ClassAverage()
     
     
    
     
    {
    
     
    	
    float GPAsum = 0;//hold all student's GPA
    
     
     
    
     
    	
     
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		
    //sum all student's GPAs and store value in GPAsum
    
     
     
     
    		GPAsum += Compstud[i].GPA;
    
    
     
    		ClassAverage = (GPAsum / size);
    
     
    		
    return ClassAverage;
     
     
    
     
    	}
    
     
    }
    
    
     
     
    void
     
    Student::Display_File(ofstream& Out_Data)
     
     
    
     
    {
    
     
    	
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		
    Out_Data.open("outputlab10.txt");
     
     
    
     
    		
     
    if (!Out_Data.is_open())
     
     
    
     
    			exit(1);
    
     
    		
    //write each student's record to file
    
     
     
     
    		
     
    Out_Data <<
     
     
    
     
    			Compstud[i].Firstname << 
     
    "   " << Compstud[i].Middlename << "   " << Compstud[i].Lastname << "   "
    
     
     
     
    			<< Compstud[i].GPA << 
     
    "   " << Compstud[i].Lettergrade << "    ";
     
     
    
     
    		
     
    for (int j = 0; j < 3; i++)//loop and output exam grades
    
     
     
     
    		{
    
     
    			
    Out_Data << Compstud[i].Examgrade[j];
     
     
    
     
    		}
    
     
    		
    for (int k = 0; k < 4; i++)//loop and output lab grades
    
     
     
     
    		{
    
     
    			
    Out_Data << Compstud[i].Labgrade[k];
     
     
    
     
    		}
    
     
    		
    Out_Data.close();
     
     
    
     
    		}
    
     
    }
    
    
     
     
    void
     
    Student::Display_Screen()
     
     
    
     
    {
    
     
    	
    constint width = 6;
     
     
    
     
    	
     
    for (int i = 0; i < size; i++)//loop through each student in array
    
     
     
     
    	{
    
     
    		cout << 
    "Name:      " << Compstud[i].Firstname << setw(width) << Compstud[i].Middlename << setw(width)
     
     
    
     
    			<< Compstud[i].Lastname << endl;
    
     
    		
    for (int j = 0; j < 3; i++)//loop and output exam grades
    
     
     
     
    		{
    
     
    			cout << Compstud[i].Examgrade[j];
    
     
    		}
    
     
    		
    for (int k = 0; k < 4; i++)//loop and output lab grades
    
     
     
     
    		{
    
     
    			cout << Compstud[i].Labgrade[k];
    
     
    		}
    
     
    		cout << 
    "GPA:      " << Compstud[i].GPA << endl;
     
     
    
     
    		cout << 
     
    "Grade:      " << Compstud[i].Lettergrade << endl;
     
     
    
     
    	}
    
     
    		cout << 
    "The class grade average was" << ClassAverage << endl;
     
     
    
     
    }
    

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JoJodoggy1
    What do you mean by "it should work on the *this object."?
    Consider:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        explicit X(int n_) : n(n_) {}
    
        void print(std::ostream& out) const
        {
            out << this->n << '\n';
        }
    private:
        int n;
    };
    
    int main()
    {
        X x(123);
        x.print(std::cout);
    }
    Notice that instead of working on some other X object, the print member function operates on the current X object by accessing it through the this pointer, i.e., by printing this->n (or equivalently, (*this).n). Normally, the this pointer is assumed in the member function's context, so we could have written just n instead of this->n.

    You might want to post your code again as you seem to have had some kind of formatting problem (probably due to the copying process) that makes it unreadable compared to your earlier post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    when I answer a question , I try to talk generally about the code , not only on what specifically doesn't work.
    writing something that works is a minimal request . writing something that works and is written good is a different thing.
    all of these small stuff - conventions , best practices etc. - these stuff make the program better. let's say he'll add another constructor in the future - any object that used default constructor in the past will cease to work and it will take few minutes in the good case , and few hours in the worst case to figure out what went wrong.
    also , camel case is not mandatory thing , but it is common practice. why not style your code in a common way and make your code more readable in the future for your self?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave11
    when I answer a question , I try to talk generally about the code , not only on what specifically doesn't work.
    writing something that works is a minimal request . writing something that works and is written good is a different thing.
    all of these small stuff - conventions , best practices etc. - these stuff make the program better.
    Agreed, and that is a good way to go about giving help. However, there is a difference between subjective style that is merely personal taste (such as precise naming conventions, the particular indent style) and style that is recognised as best practice (such as consistent use of reasonable naming conventions, consistent use of a justifiable indent style).

    Quote Originally Posted by Dave11
    let's say he'll add another constructor in the future - any object that used default constructor in the past will cease to work and it will take few minutes in the good case , and few hours in the worst case to figure out what went wrong.
    That's what I mean by "one might do so in anticipation of other constructors being added in the future". It is not correct to say that the class has "no constructor": the compiler will provide one in this case. Furthermore, in many cases it can be reasonable not to bother with providing an empty default constructor (or since C++11, a defaulted default constructor) simply because no additional constructors are anticipated.

    For this Student class, however, it is reasonable to expect additional constructors. In fact, I would argue that if not for the use of arrays, the default constructor might not make sense to begin with since the notion of a "default student" sounds strange.

    Quote Originally Posted by Dave11
    camel case is not mandatory thing , but it is common practice. why not style your code in a common way and make your code more readable in the future for your self?
    Camel case is a common practice. It is not true that "in C++ , classes usually start with upper camel case and member variables and functions - lower camel case". In fact, the conventions used by the C++ standard library are an obvious counterexample. What's problematic about JoJodoggy1's code with respect to naming conventions has to do with inconsistency, e.g., underscore separated variable names like In_Data alongside camel case names like ClassAverage, or a member function named calc_ClassAverage when other member functions like Calc_GPA start with an uppercase letter. If these were made consistent, then realistically the code would be readable, and a person wanting to write a name would not have to guess as to which naming scheme was used for that particular name.
    Last edited by laserlight; 12-23-2014 at 09:15 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I do understand that concept about arrays, can you point to a piece of code where you see this is a problem?
    Yes, the very first line inside your main function (and every one thereafter).

    Code:
    Student(); //empty constructor
    Nope, that's not an empty constructor; that's the prototype for the default constructor which tells the compiler "I'm going to provide an actual constructor elsewhere". You did not, hence the error.

  9. #9
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    Thanks for the feedback, I realize that the code I placed earlier is wonky. This version with edits is just crashing with these messages:
    "
    An unhandled win32 exception occurred in ConsoleApplication1.exe [2592]
    Unhandled exception at 0x00A578AD in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00A6F074.
    "
    Again, I created those arrays b/c I wanted to make my program so that the items of which there are multiples of (exam grades and labgrades) are looped through rather than writing cout << Compstud[i].Labgrade[0], Compstud[i].Labgrade[1], Compstud[i].Labgrade[2] etc....does that make sense? Is this not possible perhaps?

    You said "Yes, the very first line inside your main function (and every one thereafter)." I'm checking my book and examples but this looks like how it's supposed to be set up...

    At this point, I'm just trying to get it running and outputting the data. There is something that's preventing even that from happening.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    ifstream In_Data;
    ofstream Out_Data;
    
    const int size = 20;//change here to change array size
    
    class Student//declares class type Student
    {
    public:
     string Firstname, Middlename, Lastname;
     float GPA;//each student has 1 GPA
     char Lettergrade;
     int Examgrade[3];
     //holds Examgrade1, Examgrade2, Examgrade3;
     int Labgrade[4];
     //holds Labgrade1, Labgrade2, Labgrade3, Labgrade4;
     Student();//empty constructor
     //member function declarations
     void Read_Data(ifstream& In_Data);
     //function read values from a file, these values will be used for calculations
     float Calc_GPA();
     //function calculates the GPA of each of the students
     //Postcondition: Able to use the return values of this function to assign letter grade
     char LetterGrade();
     //Precondition: Student has a GPA calculated
     //function outputs a letter grade according to the student's calculated GPA
     //Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure
     float calc_ClassAverage();
     //Precondition: Each student has a GPA calculated
     //function uses all student's GPA to calculates average grade of the class
     void Display_File(ofstream& Out_Data);
     //Postconditions: 
     //Outputs each of the 20 students' full name, 3 exam grades,
     //each of the 4 lab grades, and final grade to an output file
     //Outputs the average GPA of whole class
     void Display_Screen();
     //Postconditions: 
     //Outputs each of the 20 students' full name, 3 exam grades,
     //each of the 4 lab grades, and final grade to the screen
     //Outputs the average GPA of whole class
    };
    //to "access" any student's ID number: Student.IDnumber
    //to "access" a student's 1st exam grade: Student.Examgrade[0]
    
    Student Compstud[size];
    //initializes an array within type Student to hold 'size' Compsci students
    
    float ClassAverage;
    
    int main()
    {
     //function calls begin
     Compstud[size].Read_Data(In_Data);
     Compstud[size].Calc_GPA();
     Compstud[size].LetterGrade();
     Compstud[size].calc_ClassAverage();
     Compstud[size].Display_File(Out_Data);
     Compstud[size].Display_Screen();
    }
    
    Student::Student()
    {
    }
    
    void Student::Read_Data(ifstream& In_Data)
    {
     In_Data.open("inputfile.txt");
     if (!In_Data.is_open())
      exit(1);
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      In_Data >> Compstud[i].Firstname >> Compstud[i].Middlename >> Compstud[i].Lastname;
      for (int j = 0; j < 3; j++)//loop and read in exam grades
      {
       In_Data >> Compstud[i].Examgrade[j];
      }
      for (int k = 0; k < 4; k++)//loop and read in lab grades
      {
       In_Data >> Compstud[i].Labgrade[k];
      }
     }
     In_Data.close();
    }
    
    float Student::Calc_GPA()
    {
     int sum1 = 0, sum2 = 0;//holds value of sums 
     float StudGPA;
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      for (int j = 0; j < 3; j++)
      {
       sum1 += Compstud[i].Examgrade[j];//loop and add up student's exam grades
      }
      for (int k = 0; k < 4; k++)
      {
       sum2 += Compstud[i].Labgrade[k];//loop and add up student's lab grades
      }
      StudGPA = ((.60 * sum1) + (.40 * sum2) / 2);
      Compstud[i].GPA = ((.60 * sum1) + (.40 * sum2) / 2);
     } return StudGPA;//student (element's) calculated GPA
    }
    
    char Student::LetterGrade()
    {
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      char letter = 0;//variable to hold the letter
      if (Compstud[i].GPA > 90)
       letter = 'A';
      else if (Compstud[i].GPA > 80)
       letter = 'B';
      else if (Compstud[i].GPA > 70)
       letter = 'C';
      else if (Compstud[i].GPA > 60)
       letter = 'D';
      else
       letter = 'F';
      char Lettergrade = letter;//assign the Lettergrade value of letter.
      return Lettergrade;
     }
    }
    
    float Student::calc_ClassAverage()
    {
     float GPAsum = 0;//hold all student's GPA
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      //sum all student's GPAs and store value in GPAsum
      GPAsum += Compstud[i].GPA;
      ClassAverage = (GPAsum / size);
      return ClassAverage;
     }
    }
    
    void Student::Display_File(ofstream& Out_Data)
    {
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      Out_Data.open("outputfile.txt");
      if (!Out_Data.is_open())
       exit(1);
      //write each student's record to file
      Out_Data <<
       Compstud[i].Firstname << "   " << Compstud[i].Middlename << "   " << Compstud[i].Lastname << "   "
       << Compstud[i].GPA << "   " << Compstud[i].Lettergrade << "   ";
      for (int j = 0; j < 3; i++)//loop and output exam grades
      {
       Out_Data << Compstud[i].Examgrade[j];
      }
      for (int k = 0; k < 4; i++)//loop and output lab grades
      {
       Out_Data << Compstud[i].Labgrade[k];
      }
      Out_Data.close();
     }
    }
    
    void Student::Display_Screen()
    {
     const int width = 6;
     for (int i = 0; i < size; i++)//loop through each student in array
     {
      cout << "Name:      " << Compstud[i].Firstname << setw(width) << Compstud[i].Middlename << setw(width)
       << Compstud[i].Lastname << endl;
      for (int j = 0; j < 3; i++)//loop and output exam grades
      {
       cout << Compstud[i].Examgrade[j];
      }
      for (int k = 0; k < 4; i++)//loop and output lab grades
      {
       cout << Compstud[i].Labgrade[k];
      }
      cout << "GPA:      " << Compstud[i].GPA << endl;
      cout << "Grade:      " << Compstud[i].Lettergrade << endl;
     }
     cout << "The class grade average was" << ClassAverage << endl;
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why all the global variables?

    You may want to start by fixing these issues:

    ||=== c++homework, Debug ===|
    main.cpp||In constructor ‘Student::Student()’:|
    main.cpp|66|warning: ‘Student::Firstname’ should be initialized in the member initialization list [-Weffc++]|
    main.cpp|66|warning: ‘Student::Middlename’ should be initialized in the member initialization list [-Weffc++]|
    main.cpp|66|warning: ‘Student::Lastname’ should be initialized in the member initialization list [-Weffc++]|
    main.cpp|66|warning: ‘Student::GPA’ should be initialized in the member initialization list [-Weffc++]|
    main.cpp|66|warning: ‘Student::Lettergrade’ should be initialized in the member initialization list [-Weffc++]|
    main.cpp||In member function ‘void Student::Read_Data(std::ifstream&)’:|
    main.cpp|70|warning: declaration of ‘In_Data’ shadows a global declaration [-Wshadow]|
    main.cpp|7|warning: shadowed declaration is here [-Wshadow]|
    main.cpp|74|error: ‘exit’ was not declared in this scope|
    main.cpp||In member function ‘char Student::LetterGrade()’:|
    main.cpp|124|warning: declaration of ‘Lettergrade’ shadows a member of 'this' [-Wshadow]|
    main.cpp||In member function ‘void Student:isplay_File(std:fstream&)’:|
    main.cpp|141|warning: declaration of ‘Out_Data’ shadows a global declaration [-Wshadow]|
    main.cpp|8|warning: shadowed declaration is here [-Wshadow]|
    main.cpp|147|error: ‘exit’ was not declared in this scope|
    main.cpp||In member function ‘char Student::LetterGrade()’:|
    main.cpp|127|warning: control reaches end of non-void function [-Wreturn-type]|
    main.cpp||In member function ‘float Student::calc_ClassAverage()’:|
    main.cpp|139|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 2 errors, 12 warnings (0 minutes, 1 seconds) ===|
    Why all the public member variables?


    Jim

  11. #11
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    I might want to start by killing myself. Nevermind.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    also , camel case is not mandatory thing , but it is common practice. why not style your code in a common way and make your code more readable in the future for your self?
    You are assuming that one is familiar with and comfortable with camel style now and in the future. This is not always the case.
    Furthermore, if anyone would be unable to adapt to different coding styles, then one would be unable to read 99% of the code out there.
    No, the real advice is to pick a style one is comfortable with and stick with it. As long as it is readable, then it is fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined symbol errors in MEX compilation
    By in_ship in forum C Programming
    Replies: 10
    Last Post: 12-07-2012, 01:27 PM
  2. undefined reference to class function
    By bobknows in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2012, 04:24 PM
  3. undefined reference to function in class error
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2005, 09:06 AM
  4. Class definition Identifier problems
    By bladerunner627 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2005, 06:03 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM

Tags for this Thread