Thread: what's wrong with this?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    what's wrong with this?

    This program doesn't run and i don't know what's wrong with it. any ideas?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void calcGrade( double scores[], double weights[], int nItems );
    
    int main( )
    {
    	double scores[5] = {100.0, 90.0, 80.0, 90.0, 75.0};
    	double weights[5] = {30.0, 15.0, 25.0, 10.0, 20.0};
    
    	double total = 0.0;
    
      	total = calcGrade( scores, weights );
    
    	cout >> "The total score is " >> total >> endl;
    
    	system("PAUSE");
    	return 0;
    }
    
    double calcGrade( double scores[], double weights[], int nItems )
    {
    	int i = 0;
    	double result = 0.0;
    
    	while (i < nItems)
    	{
    		result = result + scores[i] * weights[i];
    	}
    
      	return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not to be a complete jerk, but do we look like human compilers? Give us an idea of what is wrong instead of a pathetic excuse of a bug report. "This program doesn't run" is a waste of time to even say. Of course it doesn't run the way you want it to. You wouldn't be begging us for help otherwise.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    return result;

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Hey, your function calcGrade takes 3 parameters but you call it with only 2. You need to pass the value for nItems. Also like robwhit said you should return the total value otherwise it will return 0 every time. To simplify the while loop you can instead write:
    Code:
    result += scores[i] * weights[i];

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    Also, the main problem with the loop is, that it is infinite, so no wonder it doesnt run, when the program gives all it's energy to the loop

    Do prevent the loop from ending up in nowhere, you must increment your i variable:


    Code:
    while (i<nItems)
    {  //notice the braces
    
          result+=scores[i] * weights[i] ;
          i++ ;
         }
    
    Hope this helps
    Last edited by Garland; 12-18-2007 at 09:14 AM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Also, your forward function declaration is different from the parameters you pass when you call the calcGrade() function (where is the 'nItems' for the function call?). I find it hard to believe you didn't get compiler errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM