Thread: g++ giving me BIG troubles!!!

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    Question g++ giving me BIG troubles!!!

    I wrote this program and it compiles fine with Visual Studio. Problem is my teacher wants it compiled with g++ and I cant get it to compile. It says, "Main must return 'int'" Heres the entire program. I would appreciate any help/suggestions anyone can provide.
    Code:
    // '***' Represents start of new 'sub-function' 
    
    //   <<Got rid of header files and conio>>
    #include <iomanip>
    #include <iostream>
    
    
    
    using std::cout;
    using std::cin;
    using std::setw;
    
    
    //max int of 15.
    const unsigned x = 15;
    
    
    //define matrix as x+1 by x+1.
    typedef unsigned MatrixType[x+1][x+1];
    
    
    //declare y as user defined dimension size (odd numbers only)
    unsigned   Y;
    
    MatrixType Matrix;
    
    
    //"sub-function" definitions.
    void Description();
    void Dimensions();
    void Generate();
    void Print();
    void Exit();
    
    
    //MAIN FUNCTION--include "sub-functions" in main function.
    void main()
    {
    	    	
    	Description();
    	Dimensions();
    	Generate(); 
    	Print();
    	Exit();
    	}
    
    
    
    //***Information screen using cout statements.
    void Description()
    {
    	cout<<"????????????????????????????????????????????????????????????????????????????\n";
    	cout<<"?                                                                          ?\n";
    	cout<<"?                       MAGIC SQUARE PROGRAM                               ?\n";
    	cout<<"?                                                                          ?\n";
    	cout<<"?                                                                          ?\n";
    	cout<<"?      This is a program that creates a magic square that                  ?\n";
    	cout<<"?      will be filled in by you. A magic square is an x by x matrix        ?\n";
    	cout<<"?      that is filled with numbers so that the rows, columns and           ?\n";
    	cout<<"?      diagnol lines add up to be the same number.                         ?\n";	
    	cout<<"?                                                                          ?\n";
    	cout<<"?                                                                          ?\n";
    	cout<<"????????????????????????????????????????????????????????????????????????????\n";
    	cout<<"\n";
    	cout<<"\n";
    	cout<<"\n";
    	cout<<"\n";
    }
    
    
    //***function for getting the dimensions from keyboard.
    void Dimensions()
    {
    	cout<<"Get ready to make your Magic Square!\n";   
    	cout<<"Please enter an ODD number (3 through 15).\n";
    	cout<<"After that hit Enter.\n\n\n\n";
    	cin>>  Y;
    }
    
    
    	
    
    //***function for generating magic square with user defined input.
    void Generate()
    {
    
        
    	unsigned i;
    	unsigned Dsquare;
    	unsigned Row;
    	unsigned Column;
    	
    	//arrange matrix so that Magic Square will work.
    	Row = 1;                
    	Column = (Y+1)/2;
    	Matrix[Row][Column] = 1;   
    	Dsquare = Y*Y;
    	
    	for (i=2; i<=Dsquare; i++)
    	{
    		if ((i-1)%Y==0)
    			Row++;
    		else
    		{
    			Row--;   
    			Column++;
    		
    			if (Row==0)
    				Row = Y;
    		
    			else if (Column==(Y+1))
    				Column = 1;
    		}
    		Matrix[Row][Column] = i;
    	}
    }
    
    
    //***function for printing results.
    void Print()
    {
    
    	
    	unsigned Row;
    	unsigned Column;
    	
    	//creating lined box around numbers.
    	cout<<"\n\n\n\n\n\n\n\n"<<setw((4*Y-10)/2)
    		<<""<<setw(2)
    		<<Y<<" x "
    		<<setw(2)<<Y<<" Square\n";
    	
    	cout<<"ÚÄ";
    	
    	//Creates top of box (margin spacing).
    	for (Column=1; Column<=Y; Column++)
    		cout<<"ÄÄÄÄ";
    	    cout<<"¿\n";
    	
    	for (Row=1; Row<=Y; Row++)
    	{
    		//Creates 'sides' of box.
    		cout<<"³ ";
    		
    		for (Column=1; Column<=Y; Column++)
    			cout<<setw(3)<<Matrix[Row][Column]<<" ";
    		    cout<<"³\n";
    	}
    	cout<<"ÀÄ";
    	
        //Creates bottom of box (margin spacing).
    	for (Column=1; Column<=Y; Column++)
    		cout<<"ÄÄÄÄ";
     	    cout<<"Ù\n";
    }
    
    
    //***function for exit screen.
    void Exit()
    {
    	cout<<"This is a Magic Square!!!\n";
    	cout<<"\nThank you for using this program...\n";
    	cout<<"Please press any key to exit!\n\n\n\n";
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    make main return int by writing int main() { } or int main() { return 0; }

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>make main return int by writing int main() { } or int main() { return 0; }

    no, then the compiler will complain that there's two mains... and then when he takes out the other main, his program will do nothing at all.

    change this section of your code:
    Code:
    //MAIN FUNCTION--include "sub-functions" in main function.
    int main()
    {
    	    	
    	Description();
    	Dimensions();
    	Generate(); 
    	Print();
    	Return 0;
    }
    Last edited by major_small; 05-14-2004 at 10:03 AM. Reason: bad information from last poster
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Or maybe:
    Code:
    //MAIN FUNCTION--include "sub-functions" in main function.
    int main()
    {
    	    	
    	Description();
    	Dimensions();
    	Generate(); 
    	Print();
    	return 0;
    }


    Of course the return 0; part isn't necessary, only the change from void to int:
    Code:
    //MAIN FUNCTION--include "sub-functions" in main function.
    int main()
    {
    	    	
    	Description();
    	Dimensions();
    	Generate(); 
    	Print();
    }

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    heh heh... oops... nice catch...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    4
    Thanks guys, worked like a charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Big is Too Big
    By kpreston in forum C Programming
    Replies: 4
    Last Post: 10-25-2008, 10:16 AM
  2. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  3. creating big projects
    By devil@work in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2003, 04:17 PM
  4. Looking for some big C/C++ projects
    By C-Dumbie in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 12:18 AM
  5. trying to set up allegro but dos is giving me troubles
    By Klinerr1 in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2002, 07:34 PM