Thread: Can someone please take a look at this

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    Can someone please take a look at this

    Code:
    #include <iostream>
    #include <cassert>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    //Function Prototypes.
    void openFiles(ifstream& infile);
    void getData(ifstream& infile,int scores []);
    double calcScore(const int scores [], double average);
    void display(ifstream& infile, const int [], double average);
    
    const int arraySize = 11;
    
    int main ()
    {
    	int scores[arraySize];
    	ifstream infile;
    	double average;
    	
    	openFiles(infile);
    	getData(infile, scores);
    	average = calcScore(scores, average);
    	display(infile, scores, average);
    
    	return 0;
    }
    
    //Asking user to enter file name. Then opening the file.
    void openFiles(ifstream& infile)
    {
    	
    	string infilename;
    		
    	cout <<"Enter the file name to open"<< endl;
    	cin >> infilename;
    	infile.open(infilename.c_str());
    	assert(infile);
    }
    
    //Getting scores from the file they put in.
    void getData(ifstream& infile, int scores[])
    {
    
    	int i=0;
    	while (infile)
    	
    	{
    		infile >> scores[i];
    			i++;
    	
    	}
    
    }
    
    //Calculating the scores to keep in.
    double calcScore(const int scores [])
    {
    	int sum = 0;
    	
    	for (int i = 0; i < scores; i++)
    		sum += scores[i];
    	
    	return sum / static_cast<double>();
    
    }
    
    //Displaying the points reveived by contestant.
    void display(const int scores[])
    {
    
    	 cout << fixed << showpoint << setprecision(2);
    	 cout << endl << "Scores:" << endl;
    	
    	for (int i = 0; i < scores; i++)
    		cout << setw(7) << scores[i] << endl;
    	
    	cout << endl << "Average: " << average << endl;
    
    
    }
    I need some serious help.. I can't get it to work..
    The objective of the project is to be able to use declare, use and manipulate arrays. In a gymnastics or diving competition, each contestant's score is calculatede by dropping the lowest and highest scores and then addign the remaining scores. A judge awards point between 1 and 10, with 1 being the lowest and 10 being the highest. im supposed to write a program that read in the judges scores from an input file(their will be between 5 and 10 scores) and output the points received by the contestant, formatted with two decimal places.

    Help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > getData(infile, scores);
    You don't know how many scores you read.
    Returning the value of i in your loop would be a good bet, so you can do say
    numScores = getData(infile, scores);

    Then the next bit becomes a lot easier to manage as well, with
    average = calcScore(scores, numScores); // no point passing average as an uninitialised input

    Passing numScores to the display function would be a good idea as well.

    > for (int i = 0; i < scores; i++)
    Use the numScores value you're going to be passing as a parameter.
    You're basically comparing i with the base address of the array here (and gettting an int/pointer mismatch warning in the process).

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    can you show me how its supposed to look then? im still kinda lost

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    bump

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I can't get it to work..
    can you show me how its supposed to look then? im still kinda lost
    How about you ask a specific question about a specific section of code? Or, post what errors you are getting from the compiler AND on what line they are occurring.

    And before we get started, to see where you are at: can you write a for-loop to put the numbers 0-9 into an array, and then display them?

    Also, when you write a program, you should write it one function at a time. It's not until you get the first function working correctly that you should move onto writing the second function, and so on.

    Some tips:

    1) Don't ever declare a global variable, i.e. one outside any function:
    Code:
    const int arraySize = 11;
    Move that declaration into main() and if a function needs that value, then pass it to the function. Hint: when you pass an array to a function, you almost always have to pass its size as well(the exception is for char arrays).

    2) When you read from a file, the read statement should be the while conditional, e.g.
    Code:
    while(infile>>someVar)
    {
    
    }
    However, that will read in all the space-separated-values in an entire file, which may be more numbers than the size of your array.

    3)
    Code:
    double calcScore(const int scores [])
    {
    	int sum = 0;
    	
    	for (int i = 0; i < scores;
    An array name is actually the address in memory of the first element of the array, and you are not allowed to compare it to an int. Addresses in memory and ints are incompatible types.
    Last edited by 7stud; 02-19-2006 at 09:43 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > bump
    Closed. rule 5

Popular pages Recent additions subscribe to a feed