please help me fix my code!

This is a discussion on please help me fix my code! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone - I'm new to programming and have been obsessing over this code to calculate the max, min and ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    please help me fix my code!

    Hi everyone - I'm new to programming and have been obsessing over this code to calculate the max, min and sum of 3 varibale for 2 days straight but can't figure out why I keep getting compiler errors!! I know it's probably something really simple but if you can please help me I'd really appreciate it!!

    errors: calculate_max: local function definitions are illegal
    (53) line contains a brace which has not been matched
    fatal error: end of file found before the left brace

    ***************************

    #include <iostream>

    using namespace std ;

    float calculate_min (float a, float b, float c);
    float calculate_max (float a, float b, float c);
    float sum (float a, float b, float c);
    void increaseBy10 (float k, float& result);

    int main()
    {
    float x;
    float y;
    float z;
    float xx;
    float yy;
    float zz;

    float min_number;
    float max_number;
    float sum_of;

    cout << "please input 3 numbers: " << endl;
    cin >> x;
    cin >> y;
    cin >> z;

    min_number = calculate_min(x,y,z);
    max_number = calculate_max(xx,yy,zz);
    sum_of = sum(x,y,z);

    increaseBy10( x, xx );
    increaseBy10( y, yy );
    increaseBy10( z, zz );
    cout << endl;

    cout << "The lowest value: " << min_number << endl;
    cout << "The highest value: " << max_number << endl;
    cout << "The total value of the three numbers: " << sum_of << endl;
    cout << "The value of the three numbers if each is increased by 10 is listed:" << endl;
    cout << " " << xx << endl;
    cout << " " << yy << endl;
    cout << " " << zz << endl;

    return (0);
    }

    float calculate_min (float x, float y, float z)
    {
    float min_number;
    {
    if((x < y) && (x < z))
    {
    min_number = x;

    if ((y < z) && (y < z))
    {
    min_number = y;

    if ((z < x) && (z < y))
    {
    min_number = z;
    }
    return min_number;
    }

    float calculate_max (float x, float y, float z)
    {
    float max_number;
    {
    if((x > y) && (x > z))
    {
    max_number = x;
    }
    if ((y > z) && (y > z))
    {
    max_number = y;
    }
    if ((z > x) && (z > y))
    {
    max_number = z;
    }

    return max_number;
    }

    float sum (float x, float y, float z)
    {
    float sum;

    {

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Code:
    #include <iostream>
    
    using namespace std ;
    
    float calculate_min (float a, float b, float c);
    float calculate_max (float a, float b, float c);
    float sum (float a, float b, float c);
    void  increaseBy10 (float k, float& result);
    
    int main() 
    {
    float x;
    float y;
    float z;
    float xx;
    float yy;
    float zz;
    
    float min_number;
    float max_number;
    float sum_of;
    
    cout << "please input 3 numbers: " << endl;
    	cin >> x;
    	cin >> y;
    	cin >> z;
    
    	min_number = calculate_min(x,y,z);
    	max_number = calculate_max(xx,yy,zz);
    	sum_of     = sum(x,y,z);
    
    	increaseBy10( x, xx );
    	increaseBy10( y, yy );
    	increaseBy10( z, zz );
    	cout << endl;
    
    	cout << "The lowest value: " << min_number << endl;
    	cout << "The highest value: " << max_number << endl;
    	cout << "The total value of the three numbers: " << sum_of << endl;
    	cout << "The value of the three numbers if each is increased by 10 is listed:" << endl;
    	cout << " " << xx << endl;
    	cout << " " << yy << endl;
    	cout << " " << zz << endl;
         
    	return (0);  
    }
    
    float calculate_min (float x, float y, float z)
    {
    	float min_number;
    	{
    		if((x < y) && (x < z))
    		{
    			min_number = x;
    		
    		if ((y < z) && (y < z))
    		{
    			min_number = y;
    		
    		if ((z < x) && (z < y))
    		{
    			min_number = z;
    		}
    return min_number; 
    }
    
    float calculate_max (float x, float y, float z)
    {
    	float max_number;
    	{
    		if((x > y) && (x > z))
    		{
    			max_number = x;
    		}
    		if ((y > z) && (y > z))
    		{
    			max_number = y;
    		}
    		if ((z > x) && (z > y))
    		{
    			max_number = z;
    		}
    
    return max_number;
    }
    
    float sum (float x, float y, float z)
    {
    	float sum;
    
    	{

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    As far as i can see...there are two closing braces missing in calculate_min
    Code:
    float calculate_min (float x, float y, float z)
    {
    	float min_number;
    	{//remove this if u wanna retain them add a  } after return so as to close with correct no of //braces check this for calculate_max also its missing a closing brace
    		if((x < y) && (x < z))
    		{
    			min_number = x;
    		//add } here
    		if ((y < z) && (y < z))
    		{
    			min_number = y;
    		//add } here
    		if ((z < x) && (z < y))
    		{
    			min_number = z;
    		}
    return min_number; 
    }
    ...please check them

    and one more thing u r passing the wrong parameters to calculate_max while calling....it should not be (xx,yy,zz) it should be (x,y,z)..
    and please do check about pointers for the increase by10 function....
    i guess this is the program u want
    Code:
    #include <iostream>
    
    using namespace std ;
    
    float calculate_min (float a, float b, float c);
    float calculate_max (float a, float b, float c);
    float sum (float a, float b, float c);
    void  increaseBy10 (float k, float *result);
    
    int main() 
    {
    float x;
    float y;
    float z;
    float xx;
    float yy;
    float zz;
    
    float min_number;
    float max_number;
    float sum_of;
    
    cout << "please input 3 numbers: " << endl;
    	cin >> x;
    	cin >> y;
    	cin >> z;
    
    	min_number = calculate_min(x,y,z);
    	max_number = calculate_max(x,y,z);
    	sum_of     = sum(x,y,z);
    
    	increaseBy10( x,&xx );
    	increaseBy10( y,&yy );
    	increaseBy10( z,&zz );
    	cout << endl;
    
    	cout << "The lowest value: " << min_number << endl;
    	cout << "The highest value: " << max_number << endl;
    	cout << "The total value of the three numbers: " << sum_of << endl;
    	cout << "The value of the three numbers if each is increased by 10 is listed:" << endl;
    	cout << " " << xx << endl;
    	cout << " " << yy << endl;
    	cout << " " << zz << endl;
         
    	return (0);  
    }
    
    float calculate_min (float x, float y, float z)
    {
    	float min_number;
    	{//unnecessary dont need scoping for a variable in a function..
    		if((x < y) && (x < z))
    		{
    			min_number = x;
    		}
    		if ((y < z) && (y < z))
    		{
    			min_number = y;
    		}
    		if ((z < x) && (z < y))
    		{
    			min_number = z;
    		}
    	return min_number;
    	} 
    }
    
    float calculate_max (float x, float y, float z)
    {
    	float max_number;
    	{
    		if((x > y) && (x > z))
    		{
    			max_number = x;
    		}
    		if ((y > z) && (y > z))
    		{
    			max_number = y;
    		}
    		if ((z > x) && (z > y))
    		{
    			max_number = z;
    		}
    
    	return max_number;
    	}
    }
    
    float sum (float x, float y, float z)
    {
    	float sum;
    	{
    		sum=x+y+z;
    		return sum;
    	}
    }
    
    void  increaseBy10 (float k, float *result)
    {
    	(*result)=k+10;
    }
    Last edited by subhash; 04-10-2011 at 03:15 PM. Reason: adding

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Aside from the missing braces, your sum() function has barely begun.

    Also, take a read through this
    A development process
    Lots of small steps when you're learning - compiling takes seconds at most. Fixing a few lines at a time is a lot easier than fixing a whole new program.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Horray!! THANK YOU!! The compiler liked that much better but when I went to run the program, I got 'run time' errors after typing in the 3 numbers...

    Debug Error!
    Run-Time Check Failure #3 - The variable 'min number' and 'max number' is being used without being initialized

    What the heck am I missing? ... besides a well rested brain :/

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    I didn't notice that about the Sum function thanks Salem. I'll definitely check that out the link!

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Wow, I really need sleep... I just noticed the rest of the first reply.. let me give that a whirl and see if this works! THANK YOU THANK YOU

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Even after trying the code written in the first reply if u get the run time error...try initialising the min and max variables and recompile the code....

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    sweet jesus it worked! after i initialized the min and max variables and made the other corrections... it compiled w/out errors and ran like a charm!!
    i have learned greatly by this experience

    thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 03:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 08:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21