declaration syntax error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naxic
    New Member
    • Feb 2010
    • 3

    declaration syntax error

    Hey everyone, i have been doing c for only a short time and i am having a small problem with compiling my code below which is a presentation question.

    Here is the question:
    Write a C program that will be able to ceive different types of numbers and write them in appropriate files. We shall use three files file_even which is a word document, file_odd which is a text document and file_prime which is an excel file.

    Using the integers from 1 to 500,
    • Write all even files with their halves in file_even. Every even number should be on its own line and a number is separated from its half with a tab.
    • Write all odd numbers with their squares in file_odd. Every odd number should be on its own line and it is separated from its square with a tab
    • Write all prime numbers and their thirds (rounded to 2 decimall places) in file_prime. Every prime number should be on its own line and a prime number and its third should be in separate cells (equivalent of tabs)



    and here is my code:

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    
    /* A doc file file_even*/
    FILE *file_even;
    FILE *file_odd;
    FILE *file_prime;
    
    /* declaring the arrays to store the values to be written to files */
    int i;
    int odd_num[];
    int even_num[];
    int prime_num[];
    float even_halves[];
    int odd_squares[];
    float prime_thirds[];
    
    /* declaring the functions to be used */
    float half(int i);
    int square(int i);
    float third(int i);
    
    
    int main(void){
    /* working with the files */
    
    
    /* odd numbers */
    int odd;
    for(odd=1;odd<=500;odd++){
    if(odd%2==1){odd_num[i]=odd;i++;}
    
    /*even numbers */
    int even;
    for(even=1;even<=500;even++){
    if(even%2==1){even_num[i]=even;i++;}
    
    /*prime numbers */
    int j,i=1,c=0;
    while(i<=500){
    for(j=1;j<=500;j++){
    if(j%i==0){c++;}
    if(c==2){prime_num[a]=j;a++;}
    }
    }
    
    for(i=0;i<500;i++){
    even_halves[i]=half(even_num[i]);
    odd_squares[i]=square(odd_num[i]);
    prime_thirds[i]=third(prime_num[i]);
    }
    /* storing the even numbers in a file */ 
    file_even=fopen("file_even.doc","a");
    for(i=0;i<500;i++){
    fprintf(file_even,"%d\t%f\n",even_num[i],even_halves);
    }
    fclose(file_even);
    
    /* storing the odd numbers in a file */
    file_even=fopen("file_odd.txt","a");
    for(i=0;i<500;i++){
    fprintf(file_odd,"%d\t%f\n",odd_num[i],odd_squares[i]);
    }
    fclose(file_odd);
    
    /* storing the prime numbers */
    file_even=fopen("file_prime.xls","a");
    for(i=0;i<500;i++){
    fprintf(file_prime,"%d\t%f\n",prime_num[i],prime_thirds[i]);
    }
    fclose(file_prime);
    
    
    }
    
    float half(int i){
    return (i/2.0);
    }
    
    int square(int i){
    return (i*i);
    }
    
    float third(int i){
    return (i/3.0);
    }


    Any one wanna tell me where the problem might be ? because it is only faced with that one error
    Last edited by Banfa; Feb 22 '10, 10:16 PM. Reason: Added [code] ... [/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by naxic
    Any one wanna tell me where the problem might be ? because it is only faced with that one error
    I probably missed it, but I couldn't find where you described "that one error". I can't help you if I don't know what's wrong.

    By the way, your code would be much easier to read if you enclosed it in CODE tags.

    Reconsider the description of your output file formats. It is way too hard to format your output for Word (*.doc) and Excel (*.xls). You are going to generate three text files. The text file destined for Excel will be a tab-delimited text file. I don't know what you intend to do for the text file destined for Word.

    General comment: rather than cycle through the integers 1-500 three times, why not do it once. Consider the following pseudocode:
    Code:
    Open the three output files.
    Begin loop for each integer from 1 to 500 {
       if (the number is even) {
          write the required even information to the even file.
          }
       if (the number is odd) {
          write the required odd information to the odd file.
          }
       if (the number is prime) {
          write the required prime information to the prime file.
          }
       }
    Close the three output files.

    Comment

    • naxic
      New Member
      • Feb 2010
      • 3

      #3
      thanks for your post.
      actaully that one error is a "declaratio n syntax error " pointed to the start of the function definitions just after the main. and how hard would it be to write to the .doc and .xls formats ?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        When posting wrror messages please copy and paste them into your post tranlating the line numbers so they match the line numbers in the posted code.

        Are you sure that all your { and } are matched I think the compiler thinks your functions following main are inside main (which isn't allowed). Talking of which format your braces properly it will greatly help diagnose this problem, particularly having the { and } on the same line is very poor practice. Also use proper indentation (but perhaps you did and it just got destroyed in the forum post).

        It is very hard to write .doc and .xls formats because they are closed formats, Microsoft does not advertise what they are so you would need to reverse enginner the format (or read and understand the code of a program that has already reversed engineered them). It is much easier to write text data in a format that Excel or Word can use (and other office programs for that matter). I personally would use comma separated (CSV) data for Excel and either plain text or rich text format (RTF) for word.

        Comment

        • naxic
          New Member
          • Feb 2010
          • 3

          #5
          thanks alot for that bit of information Banfa. I will post the error line next time and details, i actually got it working. only that i had to really struggle with altering the formats, it was a pain but i finally just used the rtf for word and comma seperation for excel. Thanks alot guys, this was really helpful.

          Comment

          Working...