Thread: Need Help With Basic Program (computing grades)

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    8

    Need Help With Basic Program (computing grades)

    Hello everyone,

    I am a novice programmer who created this program just to get some beginner's practice using vectors. When I compile it, I get no errors, but the homework grade is returned as 0 and after inputting the quiz grades I get a segmentation fault (core dumped). Thanks in advance for any help with syntax and/or basic programming.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main ()
    {
    	string homework_grades_filename;
    	
    	cout << "\nPlease enter the name of the student's homework grades file (include .txt): ";
    	
    	cin >> homework_grades_filename;
    	
    	fstream file;
    	
    	file.open( homework_grades_filename.c_str() );
    	
    	vector<int> homework_grades;
    
    	while(file.eof() == false)
    	{
    		string homework_grade;
    		
    		getline(file, homework_grade);
    		
    		int homework_gradeNum = atoi(homework_grade.c_str());
    		
    		homework_grades.push_back(homework_gradeNum);	
    		
    	}
    	
    	file.close();
    	
    	int total_homework_grades = 0;
    	
    	for(int i=0; i < 10; i++)
     {
         
    	 total_homework_grades += homework_grades[i];
     
     }
    	
    	float final_homework_grade = (total_homework_grades / 100) * 100 * 0.30;
    	
    	cout << final_homework_grade << endl;
    	
    	string quiz_grades_filename;
    	
    	cout << "\nPlease enter the name of the student's quiz grades file (include .txt): ";
    	
    	cin >> quiz_grades_filename;
    	
    	file.open( quiz_grades_filename.c_str() );
    	
    	vector<int> quiz_grades;
    
    	while(file.eof() == false)
    	{
    		string quiz_grade;
    		
    		getline(file, quiz_grade);
    		
    		int quiz_gradeNum = atoi(quiz_grade.c_str());
    		
    		quiz_grades.push_back(quiz_gradeNum);		
    		
    	}
    	
    	file.close();
    	
    	int total_quiz_grades = 0;
    	
    	for(int i=0; i < 5; i++)
     {
         
    	 total_quiz_grades += quiz_grades[i];
     
     }
    	
    	float final_quiz_grade = (total_quiz_grades / 250) * 100 * 0.30;
    	
    	cout << final_quiz_grade << endl;
    	
    	string test_grades_filename;
    	
    	cout << "\nPlease enter the name of the student's test grades file (include .txt): ";
    	
    	cin >> test_grades_filename;
    	
    	file.open( test_grades_filename.c_str() );
    	
    	vector<int> test_grades;
    
    	while(file.eof() == false)
    	{
    		string test_grade;
    		
    		getline(file, test_grade);
    		
    		int test_gradeNum = atoi(test_grade.c_str());
    		
    		test_grades.push_back(test_gradeNum);		
    		
    	}
    	
    	file.close();
    	
    	int total_test_grades = 0;
    	
    	for(int i=0; i < 3; i++)
     {
         
    	 total_test_grades += test_grades[i];
     
     }
    	
    	float final_test_grade = (total_test_grades / 300) * 100 * 0.40;
    	
    	cout << final_test_grade << endl;
    	
    	float final_grade = (final_homework_grade + final_quiz_grade + final_test_grade);
    	
    	string letter_grade;
    	
    	char A;
    	char B;
    	char C;
    	char D;
    	char F;
    	
    	if (95 < final_grade < 100)
    		
    		letter_grade = "A+";
    		
    	else if (90 < final_grade < 94)
    	
    		letter_grade = A;
    		
    	else if (85 < final_grade < 89)
    	
    		letter_grade = "B+";
    		
    	else if (80 < final_grade < 84)
    		
    		letter_grade = B;
    		
    	else if (75 < final_grade < 79)
    	
    		letter_grade = "C+";
    		
    	else if (70 < final_grade < 74)
    	
    		letter_grade = C;
    	
    	else if (65 < final_grade < 69)
    		
    		letter_grade = "D+";
    		
    	else if (60 < final_grade < 64)
    	
    		letter_grade = D;
    		
    	else
    	
    		letter_grade = F;
    		
    	cout << "\nStudent's final grade is: " << final_grade << letter_grade << endl;
    	
    	return 0;
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So do you know whether it crashes
    - after reading the first file
    - after computing the first set of results
    - the second....
    - the third....

    Also, read this
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Also,
    for(int i=0; i < 10; i++)
    Use the vector size(), not some random assumption as to how long the file is.

    Also,
    letter_grade = A;
    should be
    letter_grade = "A";
    and remove char A; etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    So do you know whether it crashes
    - after reading the first file
    - after computing the first set of results
    - the second....
    - the third....
    Perhaps I worded it a bit vaguely, but as I said it returns a segmentation fault after it asks me for the name of the quiz grades .txt file (i.e., after I enter it).

    Also, read this
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    Thanks. It's a little over my head for where I'm at right now. I only used feof() because that's what I learned when we did vectors in my comsci class.

    Also,
    for(int i=0; i < 10; i++)
    Use the vector size(), not some random assumption as to how long the file is.
    Sorry, should have mentioned that the length of each .txt file is pre-determined. The homework grade file has 10 values, the quiz grade file has 5 values, and the test grade file has 3 values.

    Also,
    letter_grade = A;
    should be
    letter_grade = "A";
    and remove char A; etc.
    Thank you.

    Any other ideas why the homework grade is being returned as 0 and why I'm getting a segmentation fault after I input the quiz grades?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The easiest way to find a segfault is to use a debugger. If you tell us what OS you are using, we can help with that.

    A cruder method is to do this:
    Code:
    cerr << "HERE\n";
    Insert that after the last known point where the program was running. If it prints out, you know that line has executed (and use cerr, not cout, because cout is buffered and will be misleading). Then just move/copy that line down and try again until you find the offending statement.

    However, learning to use the debugger sooner rather than later is probably good, and this is a simple task, so a good way to get introduced.

    Also: you should always check the fail bit on the fstream after you open a file. Trying to read from a file that didn't open properly will cause a problem.
    Last edited by MK27; 06-13-2011 at 09:44 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jsokol7 View Post
    Perhaps I worded it a bit vaguely, but as I said it returns a segmentation fault after it asks me for the name of the quiz grades .txt file (i.e., after I enter it).
    So, the answer is "No, I have no idea where it segfaults." You should figure that out first. Easiest way is to run it in the debugger (as mentioned) and see where you are when it all goes boom.

    You should at the least make your file an ifstream if you're going to use for input, or open it in "in" mode.

    ETA: Not anything to do with crashing, but:
    Code:
    (total_homework_grades / 100) * 100
    Really? Unless the point is to turn the last two digits to zero, this seems unnecessary.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    Quote Originally Posted by MK27
    The easiest way to find a segfault is to use a debugger. If you tell us what OS you are using, we can help with that.
    I'm on Windows Vista. I use notepad++ to write programs and cygwin to compile. Thank you for the tip on the simple debugger, I'll try it out once I get home. Any tips on debugging are very appreciated!

    Quote Originally Posted by MK27
    Also: you should always check the fail bit on the fstream after you open a file. Trying to read from a file that didn't open properly will cause a problem.
    Err...how exactly do I do that? :embarrassed face:

    Quote Originally Posted by tabstop
    So, the answer is "No, I have no idea where it segfaults." You should figure that out first. Easiest way is to run it in the debugger (as mentioned) and see where you are when it all goes boom. You should at the least make your file an ifstream if you're going to use for input, or open it in "in" mode.
    Fair enough. Is it wrong to assume that the segfault is occurring somewhere in the quiz grades section? I guess I'll find out when I test out cerr later. How do you open something in "in" mode?

    Quote Originally Posted by tabstop
    Really? Unless the point is to turn the last two digits to zero, this seems unnecessary.
    I realized that the *100 cancels out after I posted the code, but I don't mind leaving it in just to remain consistent.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jsokol7 View Post
    I'm on Windows Vista. I use notepad++ to write programs and cygwin to compile. Thank you for the tip on the simple debugger, I'll try it out once I get home. Any tips on debugging are very appreciated!
    If you're in cygwin, then I'm assuming you're using gcc to compile. You'll need to add -g to your compile line, and then type "gdb <programname>".

    Err...how exactly do I do that? :embarrassed face:


    Quote Originally Posted by jsokol7 View Post
    Fair enough. Is it wrong to assume that the segfault is occurring somewhere in the quiz grades section? I guess I'll find out when I test out cerr later. How do you open something in "in" mode?
    All you know is that it is somewhere after the cout. It could be anywhere from the cin statement on down, and you have no reason to believe it happens anywhere in particular (or at least if you do you haven't told us about it). You should look at how you open files: fstream::open - C++ Reference . Technically, you get in+out by default, which is probably okay, but still.
    Quote Originally Posted by jsokol7 View Post
    I realized that the *100 cancels out after I posted the code, but I don't mind leaving it in just to remain consistent.
    The whole point is that it doesn't cancel out. If total_homework_grades was, say, 289, then (total_homework_grades / 100) * 100 is 200, not 289.
    Last edited by tabstop; 06-13-2011 at 01:48 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    Quote Originally Posted by tabstop
    If you're in cygwin, then I'm assuming you're using gcc to compile. You'll need to add -g to your compile line, and then type "gdb <programname>".
    What exactly does this do?

    Quote Originally Posted by tabstop
    All you know is that it is somewhere after the cout. It could be anywhere from the cin statement on down, and you have no reason to believe it happens anywhere in particular (or at least if you do you haven't told us about it). You should look at how you open files: fstream:pen - C++ Reference . Technically, you get in+out by default, which is probably okay, but still.
    I only assumed it has something to do with the quiz grades section because it the segfault happens after I input the name of the quiz grades file. Thanks for the fstream reference.

    Quote Originally Posted by tabstop
    The whole point is that it doesn't cancel out. If total_homework_grades was, say, 289, then (total_homework_grades / 100) * 100 is 200, not 289.
    Why does this happen even though I set this variable to be a float? Does this go for the final quiz grade and final test grade as well? If I include everything inside of parentheses will that fix the problem?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    gdb is the debugger, so you'll want to run it there and have it tell you where it is when it dies.

    Because you don't set the variable to be a float. total_homework_grades is set to be an int.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why does this happen even though I set this variable to be a float? Does this go for the final quiz grade and final test grade as well? If I include everything inside of parentheses will that fix the problem?
    No.

    It needs to be something like

    Code:
    float total_homework_grades = 289.0f;
    float answer = (total_homework_grades / 100.0f) * 100.0f;
    Now that the entire expression is of float type, there's no doubt how it's calculated. Really, you only need to prevent integer division by making dividend or divisor floating point, but why add to the confusion by doing the minimum. And of course, the whole thing cancels.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another option for debugger is Visual Studio (Express). It's very nice since it's an IDE complete with a debugger.
    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.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    Hey all, thanks for all of the help. I ran the GDB, and this is what came up:

    http://i51.tinypic.com/k2cmr.png

    So...now what? How do I find out what is wrong about that line?

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Set a break point right before that line, and verify that all your variable values are what you think they are.
    Does quiz_grades actually contain 5 elements?
    Last edited by King Mir; 06-20-2011 at 01:00 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    Quote Originally Posted by King Mir View Post
    Set a break point right before that line, and verify that all your variable values are what you think they are.
    Does quiz_grades actually contain 5 elements?
    I know how to set up a break point, but how do I "verify my variables"? Sorry that I know so little...I really need step-by-step instructions here. :-/

    And yes, I'm 100% sure the quiz grades file has 5 values. The homework/quiz/test grade files I made personally, specifically for testing out this program.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "print".

    Also, what is in your file bears only a tenuous relation to the values that are in your program at that time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having problem with a program for Calculating Grades
    By Magmadragoon in forum C Programming
    Replies: 17
    Last Post: 02-15-2011, 08:21 AM
  2. Simple Program for grades. Little help please.
    By kordric in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2008, 12:29 AM
  3. c program on grades of a student
    By galmca in forum C Programming
    Replies: 14
    Last Post: 09-29-2004, 03:07 PM
  4. Grades program has me lost
    By adrea in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2002, 08:35 PM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM

Tags for this Thread