Thread: C++ program problems; please help.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Arrow C++ program problems; please help.

    I'm writing a code that asks the user for 5 judges scores, and then takes away the highest score and the lowest score, and averages the 3 middle scores and displays that as the contestants score. I'm getting errors that my "local function definitions are illegal" for all my different functions. And that "answer", "lowest", and "highest" and undeclared identifiers. At first I had all of these in the beginning of the main, but then I moved "lowest" and "highest" to the function that they were being used in and left "answer" in the beginning of the main. Is that where all these should be?












    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    void getJudgeData (double &);
    void calcScore (double, double, double, double, double);
    double findLowest (double, double, double, double, double);
    double findHighest( double, double, double, double, double);
    
    int main ()
    
    {
    
    do
    {
    	double score1, score2, score3, score4, score5;
    	char answer = 'y';
    
    
    	cout << "Welcome to Star Search" << endl << "----------------------"; 
    
    	void getJudgeData (double &score)
    	{
    		int  counter = 1; 
    		while (counter <= 5)
    		{
    	cout << "Enter score between 0 and 10 "; 
    	cin >> score;
    		if ((score < 0) || (score > 10))
    		{
    			cout << "Score must be in the range of 0-10. Please re-enter score. ";
    			cin >> score;
    		}
    
    		counter++;
    		}
    	
    	}
    
    
    		findLowest (score1, score2, score3, score4, score5);
    		findHighest (score1, score2, score3, score4, score5);
    		calcScore (score1, score2, score3, score4, score5);
    
    
    
    
    	double findLowest (double s1, double s2, double s3, double s4, double s5)
    	{
    		double lowest;
    		
    		if ((s1 < s2) && (s1 < s3) && (s1 < s4) && (s1 < s5))
    		{
    			lowest = s1; 
    		}
    		
    		else if ((s2 < s1) && (s2 < s3) && (s2 < s4) && (s2 < s5))
    		{
    			lowest = s2; 
    		}
    
    		else if ((s3 < s1) && (s3 < s2) && (s2 < s4) && (s2 < s5))
    		{
    			lowest = s3; 
    		}
    
    		else if ((s4 < s1) && (s4 < s2) && (s4 < s3) && (s2 < s5))
    		{
    			lowest = s4;
    		}
    
    		else if ((s5 < s1) && (s5 < s2) && (s5 < s3) && (s5 < s4))
    		{
    			lowest = s5; 
    		}
    	
    
    	}
    
    
    	double findHighest (double s1, double s2, double s3, double s4, double s5)
    	{
    		double highest;
    		
    		if ((s1 > s2) && (s1 > s3) && (s1 > s4) && (s1 > s5))
    		{
    			highest = s1; 
    		}
    		
    		else if ((s2 > s1) && (s2 > s3) && (s2 > s4) && (s2 > s5))
    		{
    			highest = s2; 
    		}
    
    		else if ((s3 > s1) && (s3 > s2) && (s2 > s4) && (s2 > s5))
    		{
    			highest = s3; 
    		}
    
    		else if ((s4 > s1) && (s4 > s2) && (s4 > s3) && (s2 > s5))
    		{
    			highest = s4;
    		}
    
    		else if ((s5 > s1) && (s5 > s2) && (s5 > s3) && (s5 > s4))
    		{
    			highest = s5; 
    		}
    
    
    	}
    	
    
    	void calcScore (double s1, double s2, double s3, double s4, double s5)
    	{
    		double middle; 
    		double finalScore;
    		
    		middle = s1 + s2 + s3 + s4 + s5 - lowest - highest;
    		finalScore = middle/3.0;
    		cout << "Low score is " << lowest << endl;
    		cout << "High score is " << highest << endl;
    		cout << "The contestants talent score is " << finalScore << endl;
    		
    
    	}
    
    	
    
    cout << "Do you want to try again? (y/n) ";
    cin >> answer; 
    }
    
    while ((answer = 'y') || (answer = 'Y'));
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    These:
    Code:
    void getJudgeData (double &score)
    	{
    		int  counter = 1; 
    		while (counter <= 5)
    		{
    	cout << "Enter score between 0 and 10 "; 
    	cin >> score;
    		if ((score < 0) || (score > 10))
    		{
    			cout << "Score must be in the range of 0-10. Please re-enter score. ";
    			cin >> score;
    		}
    
    		counter++;
    		}
    	
    	}
    
    
    	double findLowest (double s1, double s2, double s3, double s4, double s5)
    	{
    		double lowest;
    		
    		if ((s1 < s2) && (s1 < s3) && (s1 < s4) && (s1 < s5))
    		{
    			lowest = s1; 
    		}
    		
    		else if ((s2 < s1) && (s2 < s3) && (s2 < s4) && (s2 < s5))
    		{
    			lowest = s2; 
    		}
    
    		else if ((s3 < s1) && (s3 < s2) && (s2 < s4) && (s2 < s5))
    		{
    			lowest = s3; 
    		}
    
    		else if ((s4 < s1) && (s4 < s2) && (s4 < s3) && (s2 < s5))
    		{
    			lowest = s4;
    		}
    
    		else if ((s5 < s1) && (s5 < s2) && (s5 < s3) && (s5 < s4))
    		{
    			lowest = s5; 
    		}
    	
    
    	}
    
    
    	double findHighest (double s1, double s2, double s3, double s4, double s5)
    	{
    		double highest;
    		
    		if ((s1 > s2) && (s1 > s3) && (s1 > s4) && (s1 > s5))
    		{
    			highest = s1; 
    		}
    		
    		else if ((s2 > s1) && (s2 > s3) && (s2 > s4) && (s2 > s5))
    		{
    			highest = s2; 
    		}
    
    		else if ((s3 > s1) && (s3 > s2) && (s2 > s4) && (s2 > s5))
    		{
    			highest = s3; 
    		}
    
    		else if ((s4 > s1) && (s4 > s2) && (s4 > s3) && (s2 > s5))
    		{
    			highest = s4;
    		}
    
    		else if ((s5 > s1) && (s5 > s2) && (s5 > s3) && (s5 > s4))
    		{
    			highest = s5; 
    		}
    
    
    	}
    	
    
    	void calcScore (double s1, double s2, double s3, double s4, double s5)
    	{
    		double middle; 
    		double finalScore;
    		
    		middle = s1 + s2 + s3 + s4 + s5 - lowest - highest;
    		finalScore = middle/3.0;
    		cout << "Low score is " << lowest << endl;
    		cout << "High score is " << highest << endl;
    		cout << "The contestants talent score is " << finalScore << endl;
    		
    
    	}
    All need to be taken out of main and put somewhere else (maybe below main). You don't define a function within another function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't define another function inside main(). Close main off, then define your functions. Inside main, you want to call the functions at the appropriate times.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should also return values from the functions. Also, suggest testing if the highest and lowest functions work if the values are equal.

    And some food for thought. The <algorithm> header already contains functions min and max to get the smallest and largest of two values (and surely you could write one yourself). Making use of those, this is how you might find the smallest of three values:

    Code:
    double smallest(double a, double b, double c)
    {
         return std::min(a, std::min(b, c));
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Thanks! I forgot about that. Okay so I moved all the functions to outside my main. But now since lowest and highest are defined in findLowest and findHighest above this, I'm not able to use them in calcScore below. Should i define them again inside this function? That doesn't seem like it would work since I've already assigned a value to lowest/highest in the other functions and I need it to carry over and be the same value in this function.



    Code:
    void calcScore (double s1, double s2, double s3, double s4, double s5)
    	{
    		double middle; 
    		double finalScore;
    	
    		middle = s1 + s2 + s3 + s4 + s5 - lowest - highest;
    		finalScore = middle/3.0;
    		cout << "Low score is " << lowest << endl;
    		cout << "High score is " << highest << endl;
    		cout << "The contestants talent score is " << finalScore << endl;
    		
    
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hannah_banana10 View Post
    Thanks! I forgot about that. Okay so I moved all the functions to outside my main. But now since lowest and highest are defined in findLowest and findHighest above this, I'm not able to use them in calcScore below. Should i define them again inside this function? That doesn't seem like it would work since I've already assigned a value to lowest/highest in the other functions and I need it to carry over and be the same value in this function.
    If you want the smallest value, that's why you call the function findLowest in the first place, right? So do so. (In other words: there's no reason for main to call findLowest, since it doesn't care what the lowest score is. But calcScore cares, so it needs to call.)

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Okay so I call findLowest and findHighest inside calcScore, but where do I call calcScore? I keep moving it to different places and nothing seems to work.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hannah_banana10 View Post
    Okay so I call findLowest and findHighest inside calcScore, but where do I call calcScore? I keep moving it to different places and nothing seems to work.
    You should call calcScore at the point where you want to calculate the score (so presumably after you input the numbers, and at or before the point where you print the score).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM