Results 1 to 10 of 10

Thread: getting memory errors on beginners progam

  1. #1

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10

    getting memory errors on beginners progam

    using C++
    The program compiles fine, but it will pop up a little window saying it "Generated Errors and must be closed by Windows. Also, how can i properrly pass the grade array into my function? I cant find my notes on how to do that.

    here is my program. it takes input like so:
    1 80 84 91
    where its Student ID, Test1 score, Test2, Final Exam, then outputs things like student average score and stuff to output.txt

    Code:
    // 
    // Nicholas Funnell
    // Assignment #1
    // january 29, 2003
    // -------------------
    // Takes data from input.txt, processes test scores and sends output to output.txt
    //
    
    #include <fstream>
    
    using namespace std;
    
    struct DATA
    {
    	// holds data about student that will be printed to output.txt
    	int id;
    	int average;
    	int grade;
    };
    
    void GetStudentData ( DATA &student, ifstream &fin, int grade[5] )
    {
    	// *** GetStudentData() will get one line from the file opened by fin 
    	// (we will open input.txt in main() ) Takes the values from the line and
        // puts them in a struct, and also computes the students average grade and
        // letter grade.
    
    	int letter_grade, test1, test2, testFinal;
    	
    	fin >> student.id >> test1 >> test2 >> testFinal; // read in from file.
    	student.average = int(0.20 * test1 + 0.30 * test2 + 0.50 * testFinal);
    											// compute average semester grade
    	
    	// here we decide which letter grade student got
    	if(student.average >= 90)
    		letter_grade = 4;	// grade: A
    	else if(student.average >= 80 && student.average <= 89)
    		letter_grade = 3;	// grade: B
    	else if(student.average >= 70 && student.average <= 79)
    		letter_grade = 2;	// grade: C
    	else if(student.average >= 60 && student.average <= 69)
    		letter_grade = 1;	// grade: D
    	else 
    		letter_grade = 0;	// grade: F
    	// now assign that grade to the student & update the count of that grade 
    	student.grade = letter_grade;
    	grade[letter_grade] += 1;       // keeping track of number of grades
    	// end of function
    	return;
    }
    
    void PrintStudentData ( DATA &student, ofstream &fout )
    {
    	char grade[5];	// letter grades.
    	grade[0] = 'F';
    	grade[1] = 'D';
    	grade[2] = 'C';
    	grade[3] = 'B';
    	grade[4] = 'A';
    	// print out the stuff on a student one line at a time...
    	fout << student.id << "\t\t\t" << student.average << "\t\t\t" 
    		 << grade[student.grade] << '\n' << flush;
    	
    	return;
    }
    
    
    
    int main ( void )
    {
    	DATA student[50];	// hold all student records.
    	ifstream fin;	// for input
    	ofstream fout;	// for output
    	int i=0, numstudents, grade[5];
    	float class_average;
    	// grade will hold the number of grades recived in the class.
    	// ex: grade[4] will hold the number of A's given to students.
    
    	fin.open("input.txt");	// open for input
    	fout.open("output.txt");	// open for output
    
    	// lets go get the data from input.txt
    	for(i=0; i<50; i++)
    	{
    		// the for loop will make sure we only get the first 50 lines of the file
    		if(!fin.eof())	// while were not at the end of the file...
    			GetStudentData(student[i], fin, grade);		// get the data for a line!
    		else
    			break;	// kills for loop when we reach end of file.
    	}
    
    	numstudents = i;	// holds number of students...
    	// compute class average
    	class_average = ( (4 * grade[4]) + (3 * grade[3]) + (2 * grade[2]) + (1 * grade[1]) ) / numstudents;
    
    	// now we can print it all out
    	// start will the ID, semester average, and letter grade for every student
    	fout << "Student ID: \t Semester Average: \t Letter Grade: \n" << flush;
    	for(i=0; i<=numstudents; i++)
    	{
    		PrintStudentData( student[i], fout);
    	}
    
    	// now print class average and grade distribution
    	fout << "\n\nClass Average: " << class_average << flush;
    	fout << "\n Grade Distribution: \n" 
    		 << "A \t" << grade[4]
    		 << "\nB\t" << grade[3]
    		 << "\nC\t" << grade[2]
    		 << "\nD\t" << grade[1]
    		 << "\nF\t" << grade[0] << flush;
    
    	fout << "\n\nNumber of Students: " << numstudents << flush;
    
    	// and then we die very quietly... 
    	return 0;
    }
    Thanks in advance for any help!

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    The problem you have is that DATA student[50]; is not global or static, so everytime you leave main it gets filled with junk, thus it blows up your program. Just change the dec. to static DATA student[50];

    Since I dont have the txt files to check it with I dont know what other problems you might have.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10
    DATA is global. It does change when i leave main. I've found the problem is somewhere around numstudents = i;, because it will output to a file until that point, then die.

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Hmm ok, until I made the static it would crash going to:

    PHP Code:
    fout << student.id << "\t\t\t" << student.average << "\t\t\t" 
             
    << grade[student.grade] << '\n' << flush
    because student.grade was always a bad number. If you give me a sample file I will look at it some more.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10
    sure, here is a sample input file.

    ... ok it didnt attach.

    Code:
    input.txt:
    1	68	75	84
    2	64	58	50
    3	75	84	88
    4	80	89	94
    5	66	64	94
    6	68	84	95

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I hope I didn't just do your homework for you

    Here is the code I used and it worked for me

    I noted the changes with //<-------------------- (TECH)

    PHP Code:
    // 
    // Nicholas Funnell
    // Assignment #1
    // january 29, 2003
    // -------------------
    // Takes data from input.txt, processes test scores and sends output to output.txt
    //

    #include <fstream>

    using namespace std;

    struct DATA
    {
        
    // holds data about student that will be printed to output.txt
        
    int id;
        
    int average;
        
    int grade;
    };

    void GetStudentData DATA &studentifstream &finint grade[5] )
    {
        
    // *** GetStudentData() will get one line from the file opened by fin 
        // (we will open input.txt in main() ) Takes the values from the line and
        // puts them in a struct, and also computes the students average grade and
        // letter grade.

        
    int letter_gradetest1test2testFinal;
        
        
    fin >> student.id >> test1 >> test2 >> testFinal// read in from file.
        
    student.average int(0.20 test1 0.30 test2 0.50 testFinal);
                                                
    // compute average semester grade
        
        // here we decide which letter grade student got
        
    if(student.average >= 90)
            
    letter_grade 4;    // grade: A
        
    else if(student.average >= 80 && student.average <= 89)
            
    letter_grade 3;    // grade: B
        
    else if(student.average >= 70 && student.average <= 79)
            
    letter_grade 2;    // grade: C
        
    else if(student.average >= 60 && student.average <= 69)
            
    letter_grade 1;    // grade: D
        
    else 
            
    letter_grade 0;    // grade: F
        // now assign that grade to the student & update the count of that grade 
        
    student.grade letter_grade;
        
    grade[letter_grade] += 1;       // keeping track of number of grades
        // end of function
        
    return;
    }

    void PrintStudentData DATA &studentofstream &fout )
    {
        
    char grade[5];    // letter grades.
        
    grade[0] = 'F';
        
    grade[1] = 'D';
        
    grade[2] = 'C';
        
    grade[3] = 'B';
        
    grade[4] = 'A';
        
    // print out the stuff on a student one line at a time...
        
    fout << student.id << "\t\t\t" << student.average << "\t\t\t" 
             
    << grade[student.grade] << '\n' << flush;
        
        return;
    }



    int main void )
    {
        
        static 
    DATA student[50];    // hold all student records.   //<-------------------- Changed To Static (TECH)
        
    ifstream fin;    // for input
        
    ofstream fout;    // for output
        
    static int i=0numstudentsgrade[5];   //<-------------------- Changed To Static (TECH)
        
    float class_average;
        
    // grade will hold the number of grades recived in the class.
        // ex: grade[4] will hold the number of A's given to students.

        
    fin.open("input.txt");    // open for input
        
    fout.open("output.txt");    // open for output

        // lets go get the data from input.txt
        
    for(i=0i<50i++)
        {
            
    // the for loop will make sure we only get the first 50 lines of the file
            
    if(!fin.eof())    // while were not at the end of the file...
                
    GetStudentData(student[i], fingrade);        // get the data for a line!
            
    else
                break;    
    // kills for loop when we reach end of file.
        
    }

        
    numstudents i-1;    // holds number of students...  //<-------------------- When you exit your loop above you will have looped one time to many due to the check for eof.  So you need to take off one from i (TECH)
        // compute class average
        
    class_average = ( (grade[4]) + (grade[3]) + (grade[2]) + (grade[1]) ) / numstudents;

        
    // now we can print it all out
        // start will the ID, semester average, and letter grade for every student
        
    fout << "Student ID: \t Semester Average: \t Letter Grade: \n" << flush;
        for(
    i=0i<=numstudentsi++)
        {
            
    PrintStudentDatastudent[i], fout);
        }

        
    // now print class average and grade distribution
        
    fout << "\n\nClass Average: " << class_average << flush;
        
    fout << "\n Grade Distribution: \n" 
             
    << "A \t" << grade[4]
             << 
    "\nB\t" << grade[3]
             << 
    "\nC\t" << grade[2]
             << 
    "\nD\t" << grade[1]
             << 
    "\nF\t" << grade[0] << flush;

        
    fout << "\n\nNumber of Students: " << numstudents << flush;

        
    // and then we die very quietly... 
        
    return 0;

    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10
    wow, it works. Thanks! No, you didn't do it for me, but now i need to go look up what static does so it makes sense again.

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Your welcome
    I was just messing with ya, you did all the work I just helped you fix it.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  9. #9

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10
    So, why did i need to make those variables static? Was I not passing them into the functions correctly? How should I have done that?

  10. #10
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    It really only matters to grade[] because you are not using a pointer to it when you leave main() and go to GetStudentData(). Since you didn't have it static, the memory was getting over writen. Try it an look the result you get.

    You could have fixed this by using a pointer in GetStudentData() for student. You really didn't need to make student[]. I was just getting a currupt location when I was running it without the test file. So you can take that off
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width