help!! Errors I don't understand 
Author Message
 help!! Errors I don't understand

I've been writing a program to find prime numbers and it's finding some
wacky errors with my integer declarations or something.  I'd give more
info but I don't know what else to say.  The error messages and code are
both below

#include <stdio.h>

int sprime(int);
int fprime(int,int []);

int main()
{
  /* Ask which prime */
  printf("What slow prime?");
  int i;
  scanf("%d",&i);
  /*Initialize speed table for fprime()*/
  int table[100];
  for(int x=0;x<100;x++)
    {
      table[x]=0;
    }
  /*Calculate slowprime and print it out*/
  int d;
  d=sprime(i);
  printf("The %d prime is %d\n",i,d);
  /*Ask about fastprime*/
  printf("Which fastprime?");
  scanf("%d",&i);
  /*calculate fastprime and print it out*/
  d=fprime(i,table);
  printf("The %d prime is %d\n",i,d);
  /*we're done*/
  return 0;

Quote:
}

int sprime(int n)
{
  /*the first prime is 2*/
  if(n==1)return 2;
  /*It's larger than the previous prime*/
  int guess=prime(n-1)+1;
  /* We just try division*/
  for(int x=2;x<n;x++)
    {
      if(float(guess)%float(x)==0)
        {
          x=1;/*We want x to be two, the loop will increment*/
          guess++;
        }
    }

Quote:
}

int fprime(int n,int table[])
{
  /*The first prime is 2*/
  if(n==1)return 2;
  /*The table here maxes out at 99*/
  if(n>99)return -1;
  /*Did we already find it?*/
  if(table[n]!=0)return table[n];
  /*We know it's larger than the previous prime*/
  int guess=prime(n-1)+1;
  for(int x=1;x<n;x++)
    {
      if(float(guess)%float(prime(x,table[]))==0)
        {
          x=0;
          guess++;
        }
    }
  table[n]=guess;
  return table[n];

Quote:
}

prime.c: In function `main':
prime.c:10: parse error before `int'
prime.c:11: `i' undeclared (first use in this function)
prime.c:11: (Each undeclared identifier is reported only once
prime.c:11: for each function it appears in.)
prime.c:13: parse error before `int'
prime.c:14: parse error before `int'
prime.c:14: `x' undeclared (first use in this function)
prime.c:14: parse error before `)'
prime.c: At top level:
prime.c:20: `i' undeclared here (not in a function)
prime.c:20: initializer element is not constant
prime.c:20: warning: data definition has no type or storage class
prime.c:21: parse error before string constant
prime.c:21: warning: data definition has no type or storage class
prime.c:23: parse error before string constant
prime.c:23: warning: data definition has no type or storage class
prime.c:24: parse error before string constant
prime.c:24: warning: data definition has no type or storage class
prime.c:26: redefinition of `d'
prime.c:20: `d' previously defined here
prime.c:26: `i' undeclared here (not in a function)
prime.c:26: `table' undeclared here (not in a function)
prime.c:26: initializer element is not constant
prime.c:26: warning: data definition has no type or storage class
prime.c:27: parse error before string constant
prime.c:27: warning: data definition has no type or storage class
prime.c: In function `sprime':
prime.c:37: parse error before `int'
prime.c:39: parse error before `int'
prime.c:39: `x' undeclared (first use in this function)
prime.c:39: parse error before `)'
prime.c:44: `guess' undeclared (first use in this function)
prime.c: At top level:
prime.c:46: parse error before `}'
prime.c: In function `fprime':
prime.c:58: parse error before `int'
prime.c:59: parse error before `int'
prime.c:59: `x' undeclared (first use in this function)
prime.c:59: parse error before `)'
prime.c:64: `guess' undeclared (first use in this function)
prime.c: At top level:
prime.c:66: parse error before `}'
prime.c:67: `n' undeclared here (not in a function)
prime.c:67: variable `table' has initializer but incomplete type
prime.c:67: `guess' undeclared here (not in a function)
prime.c:67: warning: data definition has no type or storage class
prime.c:68: parse error before `return'
Exit 1

This is getting urgent, please help!!!

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sat, 09 Mar 2002 03:00:00 GMT  
 help!! Errors I don't understand

Quote:
> I've been writing a program to find prime numbers and it's finding some
> wacky errors with my integer declarations or something.  I'd give more
> info but I don't know what else to say.  The error messages and code are
> both below

> #include <stdio.h>

> int sprime(int);
> int fprime(int,int []);

> int main()
> {
>   /* Ask which prime */
>   printf("What slow prime?");
>   int i;

Variable definitions first, then code. Your code seems to suggest (by this
and other signs) that you are trying to write C, but are including
occasional C++ syntax, then compiling as C.

Move int i; above the printf. Same goes for table[] (which you can
initialise more simply by saying int table[100] = {0}; ) and d. Also,
define int x in the same place (above that first printf) instead of in your
for loop.

Fix these and any other similar syntax errors, then try a recompile. I
think/hope you'll be surprised and pleased by the result. Most of your
error messages should disappear.

NB if you read each error message in turn, starting with the first, and
dealing with them one at a time, they become less scary. It takes a little
practice before you get used to dealing with error messages - take the time
to practice.

One technique you might like to try is to write a valid program, for
example:

#include <stdio.h>

int main(void)
{
  char msg[] = "Hello world.";
  printf("%s\n", msg);
  return 0;

Quote:
}

Check that it compiles. Then change something. For example, change main()
to mian(). Rebuild your program. Look at the resulting error message.
Change it back. Then change, say, msg to mdg in the printf statement, but
not in the definition statement. Rebuild. Observe error message(s). Change
it back. Remove the final closing brace. Observe error message(s). Change
it back. Rebuild. And so on.

It doesn't take long, using this method, to start getting a handle on how
error messages can help you to quickly locate and fix errors in your code.

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.



Sat, 09 Mar 2002 03:00:00 GMT  
 help!! Errors I don't understand

Quote:

> I've been writing a program to find prime numbers and it's finding some
> wacky errors with my integer declarations or something.  I'd give more
> info but I don't know what else to say.  The error messages and code are
> both below

You are making the "wacky error" of trying to declare variables with
block scope after an executable statement.  Move your declarations to
the top of the block where they belong.

[...]

Quote:
> {
>   /* Ask which prime */
>   printf("What slow prime?");

    ^^^^^^^ executable statement
Quote:
>   int i;

    ^^^^^ declaration
[...]

--

__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com



Sat, 09 Mar 2002 03:00:00 GMT  
 help!! Errors I don't understand

...

Quote:
>Thanks, that works.  What I read somewhere is that C, just like C++,
>lets you declare them anywhere.  What I've also been told is that most C
>compilers wont deal with this anyway.  comments?

The C language requires any conforming C compiler to complain about this
(it is a syntax error). If a compiler accepts this silently then the
bottom line is that it is not a C compiler.

--
-----------------------------------------


-----------------------------------------



Sun, 10 Mar 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. don't understand compile-time error

2. An error message I don't understand...

3. I don't understand this link error ...

4. don't understand error message

5. Don't understand why I am getting the C2236 error

6. Linker Error (I don't understand)

7. Please help, I don't understand

8. Don't understand what's Functions

9. Don't understand what's Functions

10. I don't understand the syntax.

11. I don't understand...

12. I don't understand

 

 
Powered by phpBB® Forum Software