Thread: Float Function Issues

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Float Function Issues

    I'm working on this problem for a project and have come across these errors. I'm guessing I can't use floats with functions like I could integers.... I was also wondering if for my last function there is an easier way to sort the numbers!

    error C2601: 'Do_Sum' : local function definitions are illegal
    (18): this line contains a '{' which has not yet been matched
    (91) : error C2601: 'Do_Profuct' : local function definitions are illegal
    (18): this line contains a '{' which has not yet been matched
    (102) : error C2601: 'Do_Average' : local function definitions are illegal
    (18): this line contains a '{' which has not yet been matched
    (112) : error C2601: 'Do_Sort' : local function definitions are illegal
    18): this line contains a '{' which has not yet been matched
    (154) : fatal error C1075: end of file found before the left brace '{' at

    This was the original problem!
    o Create a menu that gives the user the following choices:

    Options:
    ----------
    1. Sum
    2. Product
    3. Average
    4. Sort
    5. Exit

    o Input 3 float numbers. The program should include a loop (for more than one choice) and a switch statement (for options). Your program has to use modularization: each option should call a function

    Here is my code! Any help would be appreciated!

    Code:
    // ***************************************
    //Doug Miller
    //Lab 3, Part 3
    //Working with numbers; to compute average!
    //****************************************
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    float Do_Sum (float, float, float);
    float Do_Product (float, float, float);
    float Do_Average (float, float, float);
    void Do_Sort (float&, float&, float&);
    
    int main()
    {
    	int option = 0;
    	float v_1 = 0;
    	float v_2 = 0;
    	float v_3 =0;
    
    	while (option != 5)
    	{	
    		cout << "Options" << endl;
    		cout << "-------" << endl;
    		cout << "\n\n";
    		cout << "1.) Sum" << endl;
    		cout << "2.) Product" << endl;
    		cout << "3.) Average" << endl;
    		cout << "4.) Sort" << endl;
    		cout << "5.) Exit" <<endl;
    		cout << "\n\n";
    		cout << "Please enter your choice: " << endl;
    		cin >> option;
    		cout << "Please enter three values: " << endl;
    		cin >> v_1 >> v_2 >> v_3;
    		
    		switch (option)
    		{
    			case 1:
    				{
    					cout << "The sum of these numbers is" << Do_Sum (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    				}
    			case 2:
    				{
    					cout << "The product of these numbers is" << Do_Product (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    			case 3:
    				{
    					cout << "The average of these numbers is" << Do_Average (v_1, v_2, v_3);
    					cout << "\n\n";
    					break;
    				}
    			case 4:
    				{
    					Do_Sort (v_1, v_2, v_3);
    					cout << v_1, v_2, v_3;
    					cout << "\n\n";
    					break;
    				}
    			case 5:
    				{
    					option = 5;
    					break;
    				}
    			default:
    				{
    					cout << "Please renter option: " << endl;
    					cin >> option;
    				}
    		}
    	}	
    
    	
    }//END OF MAIN
    
    float Do_Sum (float v_1, float v_2, float v_3)
    {
    	float total = 0;
    
    	total = (v_1 + v_2 + v_3);
    
    	return total;
    
    }//END OF DO SUM
    
    float Do_Profuct (float v_1, float v_2, float v_3)
    {
    
    	float product = 0;
    
    	product = (v_1 * v_2 * v_3);
    
    	return product;
    
    }//END OF DO PRODUCT
    
    float Do_Average (float v_1, float v_2, float v_3)
    {
    	float average = 0;
    
    	average = ((v_1 + v_2 + v_3) / 3);
    
    	return average;
    
    }//END OF DO AVERAGE
    
    void Do_Sort (float& v_1, float& v_2, float& v_3)
    {
    
    	float temp = 0;
    
    	if (v_1 > v_2 && v_1 > v_3)
    	{
    		v_1 = v_1;
    
    		if (v_2 > v_3)
    		{
    			v_2 = v_2;
    			v_3 = v_3;
    		}
    		else
    		{
    			temp = v_2;
    			v_2 = v_3;
    			v_3 = temp;
    		}
    	}
    	else if (v_2 > v_1 && v_2 > v_3)
    	{
    		temo = v_1
    		v_1 = v_2
    
    		if (v_1 > v_3)
    		{
    			v_2 = v_1;
    			v_3 = v_3;
    		}
    		else
    		{
    			v_2 = v_3;
    			v_3 = temp;
    		}
    	else 
    	{
    		v_1 = v_1;
    		v_2 = v_2;
    		v_3 = v_3;
    	}
    }//END OF SORT

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> this line contains a '{' which has not yet been matched

    I don't think it's that line, but you do have an unmatched brace. Find it and fix it.Then compile again and look at the errors. There is at least one more typo in your function definitions.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >			case 2:
    >				{
    >					cout << "The product of these numbers is" << Do_Product (v_1, v_2, v_3);
    >					cout << "\n\n";
    >					break;
    >			case 3:
    You're missing at the end of case 2. I think you can omit the braces entirely, since it's within a case.
    Code:
    >		temo = v_1
    >		v_1 = v_2
    You're missing semicolons here, and temp is misspelled. As for an easier way to sort the numbers, if you can use arrays, you could use the sort() function from the standard library.
    Last edited by swoopy; 10-29-2007 at 03:08 PM. Reason: typo corrected

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    One is not supposed to use braces
    Code:
     { }
    inside one's case statements.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> One is not supposed to use braces inside one's case statements.
    Why not? It is necessary if you declare a variable within the case. If you don't, then the braces don't hurt.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by abachler View Post
    One is not supposed to use braces
    Code:
     { }
    inside one's case statements.
    Why not? It's helps organize the code. They are optional, but they don't hurt.

    Braces can be put anywhere in a function body to separate out a section of code. This has the other effect that all local variables declared within the block are destroyed at the end of the block. If there are no local variables, then the braces serve only as a visual que.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM