GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2005, 11:17
sssalsa sssalsa is offline
New Member
 
Join Date: Jan 2005
Posts: 4
sssalsa is an unknown quantity at this point

Error: unexpected end of file found?


ive written a c++ program with my own function defined to compute the numer n less than m so that both n+a and n+b are also prime. so what i did was write a function triples(int m, int a, int b) to get c++ to find out the primes n, n+a and n+b, then my main int main() was to implement this. (this is how i was told i had to do this).at the end i only get 2 errors, they are as follows:

error C2601: 'main' : local function definitions are illegal
cpp(65) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

17563c2.exe - 2 error(s), 0 warning(s)

the 1st is related to the first curly bracket underneath where i have written int main()
and the final error is for the very last curly bracket right at the end of the program. why are these errors ocuuring and how can i get rid of them?

this is my program (its quite long):

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>

int triples( int m, int a, int b )
{
int d, n;
double root, root1, root2;
if ((n<=1)) return 0;

else if (n==2) return 1;

else if (n>2 && n%2==0) return 0;

else

root = sqrt(n);
for (d=2; d<=root; d++){

if (0 == n%d) return 0; 

else if (((n+a)<=1)) return 0;
else if((n+a)==2) return 1;
else if ((n+a)>2 && (n+a)%2==0) return 0;

else

root1 = sqrt(n+a);
for (d=2; d<=root1; d++){

if (0 == (n+a)%d) return 0; 

else if (((n+b)<=1)) return 0;
else if ((n+b)==2) return 1;
else if ((n+b)>2 && (n+b)%2==0) return 0;

else

root2 = sqrt(n+b);
for (d=2; d<=root2; d++) {

if (0== (n+b)%d) {

return 0;
}
}

return 1;
}


int main ()
{
int a, b, m;
cout << "Enter intgers a, b and m: ";
cin >> a >> b >> m;

for (n=2; n<=m; n++)

if (triples(m, a, b))

cout << n << n+a << n+b << endl;

return 0;
}
Last edited by LuciWiz : 26-Mar-2006 at 09:30. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 17-Feb-2005, 11:41
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
missing a closing brace in your triples function.
__________________
spasms!!!
  #3  
Old 17-Feb-2005, 11:43
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
Well, your first problem could very well be the fact that you have 5 open { brackets but only 3 close ones in your triples() function. Try something like this:

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>

int triples( int m, int a, int b )
{
	int d, n;
	double root, root1, root2;
	
	if ((n<=1)) return 0;
	else if (n==2) return 1;
	else if (n>2 && n%2==0) return 0;
	else root = sqrt(n);
	
	for (d=2; d<=root; d++)
	{
		
		if (0 == n%d) return 0;
		else if (((n+a)<=1)) return 0;
		else if((n+a)==2) return 1;
		else if ((n+a)>2 && (n+a)%2==0) return 0;
		else root1 = sqrt(n+a);
		
		for (d=2; d<=root1; d++)
		{
			if (0 == (n+a)%d) return 0;
			else if (((n+b)<=1)) return 0;
			else if ((n+b)==2) return 1;
			else if ((n+b)>2 && (n+b)%2==0) return 0;
			else root2 = sqrt(n+b);
			
			for (d=2; d<=root2; d++)
			{
				
				if (0== (n+b)%d)
				{
					
					return 0;
				}
			}
		}
	}
	return 1;
}

int main ()
{
	int a, b, m, n; //Note, you were also missing the variable 'n'

	cout << "Enter intgers a, b and m: ";
	cin >> a >> b >> m;
	
	for (n=2; n<=m; n++)
	
	if (triples(m, a, b))
	
	cout << n << n+a << n+b << endl;
	
	return 0;
}

EDIT: Oops, machinated beat me to it, oh well.
  #4  
Old 17-Feb-2005, 12:01
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
haha yeah i didn't bother to go thru his code, just counted his braces
__________________
spasms!!!
  #5  
Old 13-Feb-2008, 12:16
seasons seasons is offline
New Member
 
Join Date: Nov 2007
Posts: 7
seasons is on a distinguished road

Re: error:unexpected end of file found?


Ok, now I have to ask the question that likely no one wants to think about.

What about when such an error is not caused by the brackets? In other words, I have some code (that I actually cannot show all of due to the grounds of this work), and have counted all the brackets for all the files, but there is no missing }. Yet I still get the error.
  #6  
Old 16-Feb-2008, 01:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: error:unexpected end of file found?


You know, you wouldn't have to count braces if you used proper and consistent formatting in your code.
__________________

Age is unimportant -- except in cheese
  #7  
Old 16-Feb-2008, 13:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error:unexpected end of file found?


Quote:
Originally Posted by seasons
...no one wants to think about...
I don't mind thinking about it, but there is no way that anyone can solve your problem without seeing the code. (On the other hand, the point here is try to get you to the point where you can solve your problem, not to have someone else do it, right?)

Unexpected end-of-file can be caused by unmatched {} braces, unmatched [] brackets, unmatched () parentheses, unmatched " " double quotes, unmatched ' ' single quotes, missing semicolons, improperly formed if or for statements, and probably other things that don't just jump off of the top of my little pointed head at the moment. (Many of these might give other errors or warning messages, depending on context and depending on what compiler you are using, and the level of error reporting that is enabled. Does your compiler give any messages other than the one you posted?)

So: How big is your file? If it is really huge, then I have to ask whether you wrote (and wrote and wrote) all of that code without trying some compiles along the way. I personally wouldn't (ordinarily) put more than a few tens of lines of code into a new file without doing a test compile. I write the functions one at a time and at least compile them before going on to the next. Often I have a skeleton test program in place so that I can actually test the functionality incrementally. (But that's just me; not everyone works the same way.)

If you are really stuck, then here's a suggestion:

Start with a new (empty) file. Put your #include stuff and whatever function prototypes you need at the top, and then just add one function. Try a compile.

Add another function. Try a compile.

Etc.

Regards,

Dave
  #8  
Old 17-Feb-2008, 21:39
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 427
Peter_APIIT is an unknown quantity at this point

Re: error:unexpected end of file found?


I agree with davekwx. His method of coding s the prereffe method for all programmer.

Besides that, u also can try to comment out some function and compile.

I hope this help.
  #9  
Old 21-Feb-2008, 21:37
maximuz maximuz is offline
New Member
 
Join Date: Feb 2008
Posts: 26
maximuz is on a distinguished road

Re: Error: unexpected end of file found?


hai Peter,

I have an opinion, to better use Switch statements and avoid more if statements, that may improve your code execution speed...

The problem you faced was with the missing braces...


regards...
Maxiiiiiii
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.