Thread: Programming Challenge # 10 (Lowest Score Drop)

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Gainesville, Florida, United States
    Posts
    2

    Programming Challenge # 10 (Lowest Score Drop)

    Hello everyone! I am taking a course on programming. We are using the "Starting out with C++ by Tony Gaddis" text book. I am working on a programming problem to which I am having trouble with.

    Lowest Score Drop

    Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions.

    void getScore() should ask the user for a test score, store it into a parameter variable, and validate it. This funtion should be called by the main once for each of the five test scores.
    • The void getScore() for our class has been changed. It should read a score from the file and store it in reference parameter variable.


    void calcAverage() should calculate and display the average of the four highest test scores. This function should be called once by the main and should be passed the five test scores.

    int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage, which uses the function to determine which of the five test scores to drop.

    Input Validation: Do not accept test scores lower than 0 or higher than 100.

    I have worked on this all by myself.

    Here is the program I have coded up to this point

    If anyone has any input or ideas as to what I am doing wrong, I would appreciate the feed back.

    Thanks!


    Code:
    //COP2000.C01
    //Module 6 Project 5 Programming Challenge 10
    
    
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    
    //prototypes go here
    void openFile (ifstream& inFile);
    void testFile (ifstream& inFile);
    void getScore(ifstream& inFile);
    void calcAverage(int test1, int test2, int test3, int test4, int test5);
    int findLowest(int test1, int test2, int test3, int test4, int test5);
    
    
    int main()
    {
        //Open the file
        //Test if file opened properly
        //Read the test scores from the file
        //Calculate the average
        //Find the lowest test score
        //Display the average of the four highest test scores
        //Display the lowest of the five test scores
    
    
        //Variables
        ifstream inFile;            //input file
        int test1;                    //First test score input from the file
        int test2;                    //Second test score input from the file
        int test3;                    //Third test score input from the file
        int test4;                    //Forth test score input from the file
        int test5;                    //Fifth test score input from the file
        int low;                    //Lowest of all test scores
        int sum;                    //Calculated total of all test scores
        float avg;                    //Calculated average of the four highest test score
        
    
    
        //open the file
        openFile(inFile);
    
    
        //test if file opened properly
        testFile(inFile);
    
    
        //Read the test scores from the file
        getScore(inFile);
        test1 = getScore(inFile);
        test2 = getScore(inFile);
        test3 = getScore(inFile);
        test4 = getScore(inFile);
        test5 = getScore(inFile);
    
    
        //Calculate the average
        calcAverage(test1, test2, test3, test4, test5);
    
    
        //Find the lowest test score
        findLowest(test1, test2, test3, test4, test5);
    
    
        //Display the computed and sorted scores
        
    
    
    
    
        return 0;
    }
    
    
    
    
    // FUNCTION DEFINITIONS
    
    
    
    
    /*************************
    **openFile will open
    **the file
    **************************/
        void openFile (ifstream& inFile)
        {
            inFile.open ("grades.txt");
        }
    
    
    
    
    
    
    /**********************************
    **testFile will ascertain that
    **the file opened properly
    ***********************************/
        void testFile (ifstream& inFile)
        {
            if (!inFile)
            {
                cerr << "Error opening input file" << endl;
                exit (2000);                //Stops the program
            }
        }
    
    
    
    
    
    
    /*************************************
    **getScore will read one test score
    **from the input file
    **************************************/
        void getScore(ifstream& inFile);
        {
            int test;
                inFile >> test;
                return test;
        }
    
    
    
    
    
    
    /***********************************
    **calcAverage will calculate the 
    **average of the four test scores
    ***********************************/
        void calcAverage(int test1, int test2, int test3, int test4, int test5);
    
    
        int sum;                    //Calculated total of all test scores
        int low;                    //Lowest of all test scores
        float avg;                    //Calculated average of the four highest test scores
        {
        low = findLowest(test1, test2, test3, test4, test5);
    
    
        sum = test1 + test2 + test3 + test4 + test5 - low;
    
    
        avg = sum/4.0;
        
        cout << "The average of the four highest scores is: " << avg << endl;
        
        }
    
    
    
    
    
    
    /***************************
    **findLowest will sort out the
    **lowest test score
    ****************************/
        int findLowest(int test1, int test2, int test3, int test4, int test5)
    {
        int low = test1;
        
        if (test2 < low)
        {
            low = test2;
        }
        else if (test3 < low)
        {
            low = test3;
        }
        else if (test4 < low)
        {
            low = test4;
        }
        else if (test5 < low)
        {
            low = test5;
        }
    
    
        cout << "The lowest score is: " << low << endl;
        
    
    
        return low;
    }
    
    
    Here are the build errors I am getting 1>------ Build started: Project: P5hh, Configuration: Debug Win32 ------ 1> P5hh.cpp 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(46): error C2440: '=' : cannot convert from 'void' to 'int' 1> Expressions of type void cannot be converted to other types 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47): error C2440: '=' : cannot convert from 'void' to 'int' 1> Expressions of type void cannot be converted to other types 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(48): error C2440: '=' : cannot convert from 'void' to 'int' 1> Expressions of type void cannot be converted to other types 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(49): error C2440: '=' : cannot convert from 'void' to 'int' 1> Expressions of type void cannot be converted to other types 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(50): error C2440: '=' : cannot convert from 'void' to 'int' 1> Expressions of type void cannot be converted to other types 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(100): error C2447: '{' : missing function header (old-style formal list?) 1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(117): error C2447: '{' : missing function header (old-style formal list?) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Attached Files Attached Files

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    At a glance, I can see these errors:
    - GetScore returns void. That means it returns nothing. You can't return anything from the function and you cannot assign the return value from the function to a variable. Presumably, you meant for it to return int.
    - Separate function declaration and function definition. Function declarations ends with a semicolon, but function definitions do not. GetScore and CalcAverage has this error.
    - You declare variables before the actual function body of CalcAverage. All code in a function must come AFTER the opening brace.
    - Consider passing the file as const reference to functions that do not modify the argument, like TestFile.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Gainesville, Florida, United States
    Posts
    2
    Ok, went over my class notes and your reply is correct. I think I followed through correctly, but then came up with these errors when trying to compile.

    Also, here's the way I re-coded it.

    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    
    //prototypes go here
    void openFile (ifstream& inFile);
    void testFile (ifstream& inFile);
    void getScore(ifstream& inFile);
    void calcAverage(int test1, int test2, int test3, int test4, int test5);
    int findLowest(int test1, int test2, int test3, int test4, int test5);
    
    
    int main()
    {
    	//Open the file
    	//Test if file opened properly
    	//Read the test scores from the file
    	//Calculate the average
    	//Find the lowest test score
    	//Display the average of the four highest test scores
    	//Display the lowest of the five test scores
    
    
    	//Variables
    	ifstream inFile;			//input file
    	int test1;					//First test score input from the file
    	int test2;					//Second test score input from the file
    	int test3;					//Third test score input from the file
    	int test4;					//Forth test score input from the file
    	int test5;					//Fifth test score input from the file
    	int low;					//Lowest of all test scores
    	int sum;					//Calculated total of all test scores
    	float avg;					//Calculated average of the four highest test score
    	
    
    
    	//open the file
    	openFile(inFile);
    
    
    	//test if file opened properly
    	testFile(inFile);
    
    
    	//Read the test scores from the file
    	getScore(inFile);
    	
    	int test = test1;
    	int test = test2;
    	int test = test3;
    	int test = test4;
    	int test = test5;
    	
    	//Calculate the average
    	calcAverage(test1, test2, test3, test4, test5);
    
    
    	//Find the lowest test score
    	findLowest(test1, test2, test3, test4, test5);
    
    
    	//Display the computed and sorted scores
    	
    
    
    
    
    	return 0;
    }
    
    
    
    
    // FUNCTION DEFINITIONS
    
    
    
    
    /*************************
    **openFile will open
    **the file
    **************************/
    	void openFile (ifstream& inFile)
    	{
    		inFile.open ("grades.txt");
    	}
    
    
    
    
    
    
    /**********************************
    **testFile will ascertain that
    **the file opened properly
    ***********************************/
    	void testFile (ifstream& inFile)
    	{
    		if (!inFile)
    		{
    			cerr << "Error opening input file" << endl;
    			exit (2000);				//Stops the program
    		}
    	}
    
    
    
    
    
    
    /*************************************
    **getScore will read one test score
    **from the input file
    **************************************/
    	void getScore(ifstream& inFile)
    	{
    		int test;
    			inFile >> test;
    	}
    
    
    
    
    
    
    /***********************************
    **calcAverage will calculate the 
    **average of the four test scores
    ***********************************/
    	void calcAverage(int test1, int test2, int test3, int test4, int test5)
    	
    	{
    	int low = findLowest(test1, test2, test3, test4, test5);
    
    
    	int sum = test1 + test2 + test3 + test4 + test5 - low;
    
    
    	float avg = sum/4.0;
    	
    	cout << "The average of the four highest scores is: " << avg << endl;
    	
    	}
    	
    
    
    
    
    /***************************
    **findLowest will sort out the
    **lowest test score
    ****************************/
    	int findLowest(int test1, int test2, int test3, int test4, int test5)
    {
    	int low = test1;
    	
    	if (test2 < low)
    	{
    		low = test2;
    	}
    	else if (test3 < low)
    	{
    		low = test3;
    	}
    	else if (test4 < low)
    	{
    		low = test4;
    	}
    	else if (test5 < low)
    	{
    		low = test5;
    	}
    
    
    	cout << "The lowest score is: " << low << endl;
    	
    
    
    	return low;
    }

    ERRORS


    Code:
    1>------ Build started: Project: P5hh, Configuration: Debug Win32 ------
    1>  P5hh.cpp
    1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(48): error C2374: 'test' : redefinition; multiple initialization
    1>          i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
    1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(49): error C2374: 'test' : redefinition; multiple initialization
    1>          i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
    1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(50): error C2374: 'test' : redefinition; multiple initialization
    1>          i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
    1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(51): error C2374: 'test' : redefinition; multiple initialization
    1>          i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
    1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(119): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Quote Originally Posted by Elysia View Post
    At a glance, I can see these errors:
    - GetScore returns void. That means it returns nothing. You can't return anything from the function and you cannot assign the return value from the function to a variable. Presumably, you meant for it to return int.
    - Separate function declaration and function definition. Function declarations ends with a semicolon, but function definitions do not. GetScore and CalcAverage has this error.
    - You declare variables before the actual function body of CalcAverage. All code in a function must come AFTER the opening brace.
    - Consider passing the file as const reference to functions that do not modify the argument, like TestFile.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should just delete these lines and try again.
    Code:
        int test = test1;
        int test = test2;
        int test = test3;
        int test = test4;
        int test = test5;
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is a rule that says you can only define one variable of the name in the same scope. You have 5 variables of the same name in the same scope.
    Consider reading about arrays, too (and if you do, don't forget std::array and std::vector).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lowest score drop assignment
    By inobProgram in forum C++ Programming
    Replies: 7
    Last Post: 07-04-2012, 04:52 PM
  2. Help with Lowest Score drop program.
    By soupcan in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2012, 09:59 PM
  3. Drop lowest score?
    By unknownnoob in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 02:27 PM
  4. Lowest Score Drop - C++
    By getName(C-Dub) in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2008, 07:02 PM
  5. Lowest Score Drop
    By naya22 in forum C++ Programming
    Replies: 16
    Last Post: 04-29-2007, 12:48 AM