Thread: Error in my program.

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Error in my program.

    Greetings,
    I have this code that when i input 5 scores it should average them and give me a grade instead of its percentage. Instaed of me summing say 5, 100% scores togther my grade should dispolay an A but the grade i always get is an F, go figure... anyways can anyone help me figure out why this is happening.



    Code:
    #include <iostream>
    #include <string>		//cin, cout, <<, >>
    using namespace std;
    
    int main()
    {
    	double score1, score2, score3,
    			score4, score5, percentage;
    	char grade;
    
    	cout << "Input five test scores: ";
    	cin >> score1, score2, score3,
    		   score4, score5;
    
    	percentage = 5 / (score1 + score2 + score3 + score4 + score5);
    
    	if (percentage >= 90)
    		grade  = 'A';	//grade must be 90 or better for an A
    
    	else if (percentage >= 80)	//grade must be 80 or better for an B
    		grade  = 'B';
    
    	else if (percentage >= 70)	//grade must be 70 or better for an C
    		grade  = 'C';
    
    	else if (percentage >= 60)	//grade must be 60 or better for an D
    		grade  = 'D';
    
    	else
    		grade  = 'F';	//anything less than 60 % is failing
    	cout << "\nYour letter grade is " << grade << "\n";
    	return percentage;
    
    }
    Last edited by correlcj; 10-03-2002 at 03:39 PM.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Two problems:
    cin >> score1, score2, score3,
    score4, score5;
    Use >> instead of ,
    Your compiler should've given you warnings. If you ignore them, you may see more F's

    percentage = 5 / (score1 + score2 + score3 + score4 + score5);
    should be
    (score1 + score2 + score3 + score4 + score5) / 5;
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cin >> score1, score2, score3, score4, score5;
    Uh, no. This won't work as you expect, try this instead:

    cin >> score1 >> score2 >> score3 >> score4 >> score5;

    >percentage = 5 / (score1 + score2 + score3 + score4 + score5);
    This is your problem, the additions are performed first, but you still divide 5 by the total. It should be the other way around:

    percentage = (score1 + score2 + score3 + score4 + score5) / 5;

    -Prelude
    My best code is written with the delete key.

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Master Yoda!

    Thanks!
    Youo wouldn't believe me if I told you so but I am embarrased to say I looked that over for a long time last night. For the life of me I could not see it until now... THANK YOU!

    No more F's now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM