Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Odd C++ Main Method errors

    Code:
     #define USAGE "ether N"
    //simulate N ethernet stations, all trying to transmit at once
    //returns avg number of slot times until one station succeeds
    //command line includes number of stations as arg[1]
    #include <iostream.h>
    #include <stdlib.h>
    #include <assert.h>
    
    #define MAX 1000 /* max # stations */
    
    class station {
    
    	public:
    	//RESET
    	void reset() 
    	{
    		_NextAttempt = _CollisionCount = 0;
    	}
    	//TRANSMITS
    	bool transmits(int T) 
    	{
    		return _NextAttempt == T;
    	}
    	//COLLIDE
    	void collide() { //updates a station after a collision
    		_CollisionCount++;
    		_NextAttempt += 1 + backoff(_CollisionCount);
    	//the '1' is for the current slot
    	}
    	
    	private:
    
    	int _NextAttempt;
    	int _CollisionCount;
    
    	//BACKOFF
    	static int backoff(int k) {
    		//choose random # between 0..2^k-1; i.e., choose k random bits
    		unsigned short r = rand();
    		unsigned short mask = 0xFFFF >> (16 - k); //mask = 2^k - 1
    		return int(r & mask);
    	}
    	};
    	
    	station S[MAX];
    //run does a single simulation, returning time at which some
    //entrant transmits
    	int run(int N) {
    		int time = 0;
    		int i;
    		for(i = 0; i < N; i++) 
    		{
    			S[i].reset();
    		}
    		while(1) {
    		int count = 0; //# of attempts at this time
    		int j = -1; //count # of attempts; save j as index of one of these
    		for(i = 0; i < N; i++) 
    			{
    	
    			if (S[i].transmits(time)) {
    				j = i;
    				++count;
    				}
    			}
    		if(count == 1) { //we are done
    			return time;
    		}
    		else if(count > 1) { //collisions occurred
    		for(i = 0; i < N; i++) {
    			if(S[i].transmits(time)) {
    			S[i].collide();
    			}
    		} //for
    			++time;
    		} //while
    	} //run
    
    	int RUNCOUNT = 10000;
    
    
    	//MAIN METHOD
     void main(int argc, char *argv[])
    	{
    
    		int N,i,runsum = 0;
    		
    		assert(argc == 2);
    		
    		N = atoi(argv[1]);
    		
    		assert(N<MAX);
    
    		for(i = 0; i < RUNCOUNT; i++) 
    		{
    			runsum += run(N);
    		}
    		cout << "runsum = " << runsum << endl;
    		cout << "RUNCOUNT = " << RUNCOUNT << endl;
    		cout << "average: " << ((double)runsum)/RUNCOUNT << endl;
    
    		return;
    	}
    When I compile this in Viz Studio 6 I get the following errors:

    --------------------Configuration: station - Win32 Debug--------------------
    Compiling...
    station.cpp
    C:\Documents and Settings\CJ\Desktop\station.cpp(84) : error C2601: 'main' : local function definitions are illegal
    C:\Documents and Settings\CJ\Desktop\station.cpp(104) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    station.exe - 2 error(s), 0 warning(s)

    I know they are simple errors but I cannot find the error in the code!

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That error usually means there are missing/misplaced braces in the code before the place the compiler complains at.

    I'd bet if you formatted your code correctly, you'd easily see the missing closing brace for your 'else if' block.

    Code:
    int run(int N)
    {
    	int time = 0;
    	int i;
    
    	for(i = 0; i < N; i++) 
    	{
    		S[i].reset();
    	}
    
    	while(1)
    	{
    		int count = 0; //# of attempts at this time
    		int j = -1; //count # of attempts; save j as index of one of these
    
    		for(i = 0; i < N; i++) 
    		{
    			if (S[i].transmits(time))
    			{
    				j = i;
    				++count;
    			}
    		}
    
    		if(count == 1) //we are done
    		{
    			return time;
    		}
    		else if(count > 1) //collisions occurred
    		{
    			for(i = 0; i < N; i++)
    			{
    				if(S[i].transmits(time))
    				{
    					S[i].collide();
    				}
    			} //for
    			++time;
    
    	} //while
    } //run


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •