Thread: Help!

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Question Help!

    Could someone please tell me what is wrong with this program. It is meant to return "GO" when

    COMETHALEBOPP
    HEAVENSGATE

    is in the input file, but it doesn't work.

    Really annoying me. Need to know answer!!!!

    #include <stdio.h>

    void main()
    {
    char comet_name[72], group_name[72];
    long int total1 = 1, total2 = 1;
    int i, num = 0 ;

    FILE *output, *input;

    input = fopen("ride.in", "r");
    output = fopen("ride.out", "w");

    fscanf(input, "%s",comet_name);
    fscanf(input, "%s",group_name);

    for(i = 0; comet_name[i] != NULL; i++)
    {
    num = comet_name[i];
    printf("%c", comet_name[i]);
    total1 *= num;
    }

    for(i = 0; group_name[i] != NULL; i++)
    {
    num = group_name[i];
    printf("%c", group_name[i]);

    total2 *= num;
    }

    if ((total1%47) == (total2%47))
    fprintf(output, "GO\n");
    else
    fprintf(output, "STAY\n");
    }


  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Drako,

    I think you are trying to tackle this problem from the wrong angle. You say the program is meant to search for particular strings, yet what you are doing is multiplying the character values of the file contents and checking to see if it is evenly divisible by 47.

    Once you have the contents of the file, you will need something to compare it too. You could either provide this in the code, or prompt the user for a value to search for. The ANSI C library functions strcmp() and strncmp() were made for this purpose. If this is a homework assignment that forbids the use of these ANSI functions, consider making a for loop that compares the elements of two character arrays.

    Hope that helps,
    Jason Deckard

Popular pages Recent additions subscribe to a feed