Thread: Please help me with this Extra Credit program

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Talking Please help me with this Extra Credit program

    Hello friends, it has beena while since I asked any questions on this board. Well I am finishing up my 2nd course in C programming. I am doing quite well in a class, but I am having troubles with this extra credit assignment. It is pretty self explanatory (the code I mean) Professor just gave me this assignment since I asked for it many times Bascially if I get do it I will get an 'A' in the class. So please help me, I tried many diffirent things but the best coding I have is below. What am I doing wrong? Please take a look, I really need this 'A' to raise my gpa.

    Its a multi file program.

    Main.c (the main program)
    #include "grep.h"
    #include "cat.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void main(int ac, char *av[])
    {
    if(strcmp("av[1]", "grep")==0)
    {
    fp=fopen("av[3]", "r");
    if(fp!=NULL)
    grep(fp,av[2]);
    else
    printf("Bad file entered\n");
    }
    else if(strcmp("av[1]", "cat")==0)
    {
    fp=fopen("av[3]", "r");
    if(fp!=NULL)
    cat(fp,av[2]);
    else
    printf("Bad file entered\n");
    }
    else
    {
    printf("Bad file entered\n");
    exit(1);
    }
    }


    cat.c
    #include "cat.h"
    #include <stdio.h>
    void (FILE *fp)
    {
    while((c=fgets(fp))!=EOF)
    putchar(c);
    }

    cat.h
    #ifndef CAT
    #define CAT
    void cat(FILE *fp); //prototype
    #endif


    grep.c
    #include "grep.h"
    #include <stdio.h>
    main (int ac, char *av[])
    {
    char s[1000];
    FILE *fp;

    for (i=2;i<ac;i++)
    fp=fopen(av[i],"r");

    if(fp==NULL)
    printf("%s is not Found\n",av[i]);
    else
    {
    while(fgets(s,1000,fp) != NULL)
    if(strstr(s,av[1])!=NULL)
    printf("%s: %s", av[i], s);
    } //ends else
    fclose(fp);
    } //ends for loop
    } //ends main

    grep.h
    #ifndef GREP
    #define GREP
    void grep(FILE *fp); //prototype
    #endif

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > void main
    WOOPS.

    Please use code tags.

    [EDIT]
    > main (int ac, char *av[])
    Whaaaaa?
    Is this a modular program?
    Are you linking several files together into one, with TWO MAINS?
    [/EDIT]

    [2nd EDIT]
    > Its a multi file program.
    I guess so.
    [/2nd EDIT]
    Last edited by Shadow; 05-16-2002 at 03:54 PM.
    The world is waiting. I must leave you now.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You need to check and see how many arguments were passed to your program. You just assume av[1], av[2], av[3] exist. ac will contain how many strings av has.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >It is pretty self explanatory (the code I mean)
    What, so we have to read your code to understand what it is supposed to do in the first place, so we can re-read it again and debug it?? Come on... !

    - What is it supposed to do?
    - What is it doing wrong?
    - Where do YOU think it's going wrong?
    - At a glance, I'd say the prog is quite simple, which bit don't you understand?

    >... gave me this assignment since I asked for it many times. Bascially if I get do it I will get an 'A' in the class
    Then work for it... if you want an A you'll have to be worth it.

    >it has beena while since I asked any questions
    so long you've forgotten how to use code tags

    >So please help me
    >Please take a look, I really need this
    Are we supposed to take pity?? You'll be asking for donations next

    >void main
    ......

    Next time, post a real question, and I'll give you a real answer
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Here are some updates.

    Okay guys here are my errors. I am very new to this multi-file programming and I am very much lost in it.

    main.c
    #include "grep.h"
    #include "cat.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void main(int ac, char *av[])
    {
    if(strcmp("av[1]", "grep")==0)
    {
    fp=fopen("av[3]", "r");
    if(fp!=NULL)
    grep(fp,av[2]);
    else
    printf("Bad file entered\n");
    }
    else if(strcmp("av[1]", "cat")==0)
    {
    fp=fopen("av[3]", "r");
    if(fp!=NULL)
    cat(fp,av[2]);
    else
    printf("Bad file entered\n");
    }
    else
    {
    printf("Bad file entered\n");
    exit(1);
    }
    }

    ***ERRORS***
    GREP.H: FILE cannot start a parameter declaration
    CAT.H: FILE cannot start a parameter declaration
    main.cpp: Extra parameter in call to grep(int *)
    main.cpp: Extra parameter in call to cat(int *)
    main.cpp: Undefined Symbol 'fp'


    cat.c
    #include "cat.h"
    #include <stdio.h>
    void (FILE *fp)
    {
    while((c=fgets(fp))!=EOF)
    putchar(c);
    }

    ***ERRORS***
    CAT.H: FILE cannot start a parameter declaration
    CAT.C expected


    grep.c
    #include "grep.h"
    #include <stdio.h>
    void (FILE *fp)
    {
    //char s[1000];
    //FILE *fp;

    for (i=2;i<ac;i++)
    {
    fp=fopen(av[i],"r");

    if(fp==NULL)
    printf("%s is not Found\n",av[i]);
    else
    {
    while(fgets(s,1000,fp) != NULL)
    if(strstr(s,av[1])!=NULL)
    printf("%s: %s", av[i], s);
    }
    fclose(fp);
    }
    }

    ***ERRORS***
    GREP.H: FILE cannot start a parameter declaration
    GREP.C expected


    cat.h
    #ifndef CAT
    #define CAT
    void cat(FILE *fp);
    #endif

    grep.h
    #ifndef GREP
    #define GREP
    void grep(FILE *fp);
    #endif

    I dont think I can do anymore with this program. What am i doing wrong here? please point me to the correct location? Is my main.cpp completely screwed up?

    Thank you for your time.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, two people in this thread ask you to use code tags, and you just go ahead and post another bucket load of code without them. Maybe if you paid attention to what people say, you might actually learn something.

    I'll give you one hint:
    >grep(fp,av[2]);
    ...
    >void (FILE *fp)
    ...
    >void grep(FILE *fp);

    There's a load more too, but until you pay attention, I ain't going to tell you anymore.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Reply

    What code tags??? I havent been on this board in like 6 months....I guess some things have changed. Where do I learn this code tags? Please dont be upset with me but I dont know what code tags I have to use.

    Let me know and I will use coda tags if it will help read the code.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    What code tags??? I havent been on this board in like 6 months....I guess some things have changed. Where do I learn this code tags? Please dont be upset with me but I dont know what code tags I have to use.

    Let me know and I will use coda tags if it will help read the code.
    Oh my.....
    Did you bother to read hammer's signature?
    The world is waiting. I must leave you now.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    11
    Im sorry I didnt pa

    You guys want me to repost my entire program again, just this time with the
    Code:
     coding source
    Tags? wont that just make things more

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    11
    Sorry I didnt see it, even when it was starring me in my eyes. Okay here is the reposrt with the tags.

    Code:
    main.c 
    #include "grep.h" 
    #include "cat.h" 
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    
    void main(int ac, char *av[]) 
    { 
    if(strcmp("av[1]", "grep")==0) 
    { 
    fp=fopen("av[3]", "r"); 
    if(fp!=NULL) 
    grep(fp,av[2]); 
    else 
    printf("Bad file entered\n"); 
    } 
    else if(strcmp("av[1]", "cat")==0) 
    { 
    fp=fopen("av[3]", "r"); 
    if(fp!=NULL) 
    cat(fp,av[2]); 
    else 
    printf("Bad file entered\n"); 
    } 
    else 
    { 
    printf("Bad file entered\n"); 
    exit(1); 
    } 
    }
    ***ERRORS***
    GREP.H: FILE cannot start a parameter declaration
    CAT.H: FILE cannot start a parameter declaration
    main.cpp: Extra parameter in call to grep(int *)
    main.cpp: Extra parameter in call to cat(int *)
    main.cpp: Undefined Symbol 'fp'


    Code:
    cat.c 
    #include "cat.h" 
    #include <stdio.h> 
    void (FILE *fp) 
    { 
    while((c=fgets(fp))!=EOF) 
    putchar(c); 
    }
    ***ERRORS***
    CAT.H: FILE cannot start a parameter declaration
    CAT.C expected

    Code:
    grep.c 
    #include "grep.h" 
    #include <stdio.h> 
    void (FILE *fp) 
    { 
    //char s[1000]; 
    //FILE *fp; 
    
    for (i=2;i<ac;i++) 
    { 
    fp=fopen(av[i],"r"); 
    
    if(fp==NULL) 
    printf("%s is not Found\n",av[i]); 
    else 
    { 
    while(fgets(s,1000,fp) != NULL) 
    if(strstr(s,av[1])!=NULL) 
    printf("%s: %s", av[i], s); 
    } 
    fclose(fp); 
    } 
    }
    ***ERRORS***
    GREP.H: FILE cannot start a parameter declaration
    GREP.C expected

    Code:
    cat.h 
    #ifndef CAT 
    #define CAT 
    void cat(FILE *fp); 
    #endif
    Code:
    grep.h 
    #ifndef GREP 
    #define GREP 
    void grep(FILE *fp); 
    #endif
    Sorry once again for not using tags. I see how they make things a lot easier for ppl to read.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Better, but next time, paste from your code editor, not a previous post. It's got the right tag, but you've lost all the indentation.

    Anyway

    #include "grep.h"
    #include "cat.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    Should be
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "grep.h"
    #include "cat.h"

    Your .h files refer to FILE, which is in stdio.h, so it needs to be included first

    > void main(int ac, char *av[])
    This must be int main

    > if(strcmp("av[1]", "grep")==0)
    if(strcmp(av[1], "grep")==0)

    > main.cpp: Extra parameter in call to grep(int *)
    So is this a C or C++ program?

    In cat.c
    > void (FILE *fp)
    Where's the function name?
    Same problem in grep.c

    > while((c=fgets(fp))!=EOF)
    Where is c declared

    To be honest, try writing and compiling in smaller steps - big bang coding (write it all then compile) never works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM