Can't compile...need help

This is a discussion on Can't compile...need help within the C++ Programming forums, part of the General Programming Boards category; I debug it over and over again. It still has errors. It said: error C2601: 'main' : local function definitions ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Unhappy Can't compile...need help

    I debug it over and over again. It still has errors. It said:

    error C2601: 'main' : local function definitions are illegal
    fatal error C1004: unexpected end of file found

    I can't get it.What's wrong with my code?
    Thx in advance!

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    void getInputs(double& a, double& b, double& c)
    {
    	cout<<"Please input coefficient a: ";
    	cin>>a;
    	cout<<"Please input coefficient b: ";
    	cin>>b;
    	cout<<"Please input the constant c: ";
    	cin>>c;
    }
    
    int quadraticSolver(double a, double b, double c, double& root1, double& root2)
    {
    	if ((a!=0) && ((b*b-4*a*c)>0))
    	{
    	root1=(-b+sqrt(b*b-4*a*c))/2*a;
    	root2=(-b-sqrt(b*b-4*a*c))/2*a;
    	}
    
    	if ((a!=0) && ((b*b-4*a*c)==0))
    	{
    	root1=(-b)/(2*a);
    	}
    
    }
    
    void displayResult(double x1, double x2, int nr_roots)
    {	
    	switch(nr_roots)
    	{
    	case 0: cout<<"The root is imaginary."; break;
    	case 1: cout<<"The root is x = "<<x1; break;
    	case 2: cout<<"The roots are x = "<<x1<<" and x = "<<x2; break;
    	}
    	
    int main()
    {
    	double x,y,z,r1,r2;
    	int no_of_root;
    	getInputs(x,y,z);
    	quadraticSolver(x,y,z,r1,r2);
    	if (b*b-4*a*c)>0)
    		no_of_root=2;
    	if (b*b-4*a*c)==0)
    		no_of_root=1;
    	if (b*b-4*a*c)<0)
    		no_of_root=0;
    	displayResult(r1,r2,no_of_root);
    	return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Code:
    void displayResult(double x1, double x2, int nr_roots)
    {	
    	switch(nr_roots)
    	{
    	case 0: cout<<"The root is imaginary."; break;
    	case 1: cout<<"The root is x = "<<x1; break;
    	case 2: cout<<"The roots are x = "<<x1<<" and x = "<<x2; break;
    	}
    }

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    #include <iostream>
    #include <math>
    using namespace std;
    
    // Same as yours
    void getInputs( double& a, double& b, double& c )
    {
    	cout<<"Please input coefficient a: ";
    	cin>>a;
    
    	cout<<"Please input coefficient b: ";
    	cin>>b;
    
    	cout<<"Please input the constant c: ";
    	cin>>c;
    }
    
    // I changed this to return a value which defines the type of root
    // Imaginary, real or zero. Just copied from main
    int quadraticSolver(double a, double b, double c, double& root1, double& root2)
    {
    	if ((a!=0) && ((b*b-4*a*c)>0))
    	{
    		root1=(-b+sqrt(b*b-4*a*c))/2*a;
    		root2=(-b-sqrt(b*b-4*a*c))/2*a;
    	}
    
    	if ((a!=0) && ((b*b-4*a*c)==0))
    	{
    		root1=(-b)/(2*a);
    	}
    
    	int no_of_root = 0;
    
    	if ( (b*b-4*a*c)>0 )
    		no_of_root=2;
    	if ( (b*b-4*a*c)==0 )
    		no_of_root=1;
    	if ( (b*b-4*a*c)<0 )
    		no_of_root=0;
    
    	return no_of_root;
    
    }
    
    
    // Same as yours
    void displayResult(double x1, double x2, int nr_roots)
    {	
    	switch(nr_roots)
    	{
    		case 0: cout<<"The root is imaginary."; break;
    		case 1: cout<<"The root is x = "<<x1; break;
    		case 2: cout<<"The roots are x = "<<x1<<" and x = "<<x2; break;
    	}
    }
    	
    int main( void )
    {
    	double a, b, c, r1, r2;
    	int no_of_root;
    
    	getInputs( a, b, c );
    	no_of_root = quadraticSolver( a, b, c, r1 ,r2 );
    
    	displayResult( r1, r2, no_of_root );
    	return 0;
    }
    Just made some changes. You also would have gotten errors about not having a, b, and c defined in main, and you using them. They weren't global, so you don't get access to them from functions.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Psst, it's called <cmath>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you,guys.Compilation succeeded.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

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