Thread: How do I fix these annoying warnings and errors?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    How do I fix these annoying warnings and errors?

    The warnings and errors are in the code as comments on the line they belong to.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define EXCH(x,y) {struct elecVotes tmp = x; x = y; y = tmp;}
    
    struct elecVotes
    {
        char name[3];
        int votes;
        int income;
    };
    
    struct incomeTmp
    {
        char name[3];
        int income;
    };
    
    void bubSort(struct elecVotes a[], int left, int right);
    
    int main()
    {
        struct elecVotes array[51];
        struct incomeTmp array2[51];
        int i;
        int j;
    
        FILE* file1;
        file1 = fopen("elecvotes.txt", "r");
        if (file1 == NULL) return -1;
        FILE* file2;        /* Warning: ISO C90 forbids mixed declarations and code */
        file2 = fopen("income.txt", "r");
        if (file2 == NULL) return -1;
        FILE* file3;       /* Warning: ISO C90 forbids mixed declarations and code */
        file3 = fopen("states_by_income.txt", "w");
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file1,"&#37;2s %d", &(array[i].name), &(array[i].votes));   /* Warning: char format, different type arg (arg 3) */
            printf("%2s %2d\n", array[i].name, array[i].votes);
        }
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file2,"%2s %5d", &(array2[i].name), &(array2[i].income));   /* Warning: char format, different type arg (arg 3) */
            printf("%2s %2d\n", array2[i].name, array2[i].income);
        }
    
        i = 0;
        j = 0;
    
        while ((i < 51) && (j < 51))
        {
            if (strcmp(array[i].name, array2[j].name) == 0)
            {
                array[i].income = array2[j].income;
                printf("%2s %2d %d\n", array[i].name, array[i].votes, array[i].income);
                i++;
                j = 0;
            }
            else
            {
                j++;
            }
        }
    
        bubSort(array, 0, 51);
    
        for (i = 0; i < 51; i++)
        {
            fprintf(file3,"%2s %2d $%d\n", array[i].name, array[i].votes, array[i].income);
        }
    
        fclose(file1);
        fclose(file2);
        fclsoe(file3);
    
        return EXIT_SUCCESS;
    }
    
    void bubSort(struct elecVotes a[], int left, int right)
    {
        int i, j;
        for (i = left; i < right; i++)
        {
            for (j = right; j < i; j--)
            {
                if (a[j] < a[j-1]) EXCH(a[j-1], a[j]);    /* ERROR: invalid operands to binary < */
            }
        }
    }
    Last edited by zyphirr; 11-12-2008 at 05:05 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't feel like reading your code to play "find an error." What are the errors generated?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    um...

    Quote Originally Posted by master5001 View Post
    I don't feel like reading your code to play "find an error." What are the errors generated?
    dude, sorry if I didn't make it clear, lol.

    the warnings and errors are in the code as comments

    I thought I'd put it in the code because the code format doesn't have line numbers when I post them.
    Last edited by zyphirr; 11-12-2008 at 05:06 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    array[i].name is already essentially a pointer, you don't take the address of that pointer. (For strings you don't use & in scanf-like functions).

    You can't compare elecVotes items with operator <. Probably you want to compare a particular field of the struct.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Master5001, although it's not easy to spot, the errors are actually marked out....

    In C, you can't compare structures. You need to either compare the individual components, or write a function to do the compare of each part, and then return a value corresponding to the difference (like strcmp does).

    You also should not use the & operator when reading strings in scanf, since the char array is turned into a pointer in itself. You also don't need the parenthesis for either of the arguments to scanf - The . "operator" has higher precedence than &, so when you do &a.b, it gives the address of element b in struct a.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define EXCH_CPY(x, y) memcpy(&(x), &(y), sizeof (x))
    #define EXCH(x,y) {struct elecVotes tmp; EXCH_CPY(tmp, x), EXCH_CPY(x, y); EXCH_CPY(y, tmp);}
    #define CMP(x, y) ((x).votes < (y).votes)
    
    struct elecVotes
    {
        char name[3];
        int votes;
        int income;
    };
    
    struct incomeTmp
    {
        char name[3];
        int income;
    };
    
    void bubSort(struct elecVotes a[], int left, int right);
    
    int main()
    {
        struct elecVotes array[51];
        struct incomeTmp array2[51];
        int i;
        int j;
    
        FILE* file1;
        FILE* file2;
    
        file1 = fopen("elecvotes.txt", "r");
        if (file1 == NULL) return -1;
    
        file2 = fopen("income.txt", "r");
        if (file2 == NULL) return -1;
        FILE* file3;       /* Warning: ISO C90 forbids mixed declarations and code */
        file3 = fopen("states_by_income.txt", "w");
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file1,"&#37;2s %d", array[i].name, &(array[i].votes));
            printf("%2s %2d\n", array[i].name, array[i].votes);
        }
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file2,"%2s %5d", array2[i].name, &(array2[i].income));
            printf("%2s %2d\n", array2[i].name, array2[i].income);
        }
    
        i = 0;
        j = 0;
    
        while ((i < 51) && (j < 51))
        {
            if (strcmp(array[i].name, array2[j].name) == 0)
            {
                array[i].income = array2[j].income;
                printf("%2s %2d %d\n", array[i].name, array[i].votes, array[i].income);
                i++;
                j = 0;
            }
            else
            {
                j++;
            }
        }
    
        bubSort(array, 0, 51);
    
        for (i = 0; i < 51; i++)
        {
            fprintf(file3,"%2s %2d $%d\n", array[i].name, array[i].votes, array[i].income);
        }
    
        fclose(file1);
        fclose(file2);
        fclsoe(file3);
    
        return EXIT_SUCCESS;
    }
    
    void bubSort(struct elecVotes a[], int left, int right)
    {
        int i, j;
        for (i = left; i < right; i++)
        {
            for (j = right; j < i; j--)
            {
                if (CMP(a[j], a[j-1])) EXCH(a[j-1], a[j]);
            }
        }
    }
    Last edited by master5001; 11-12-2008 at 05:19 PM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by matsp View Post
    Master5001, although it's not easy to spot, the errors are actually marked out....
    No that was just laziness getting the best of me... Sorry to the OP. I just scrolled down and didn't see a compiler error dump. So I just posted... My bad. Corrections are posted now.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    bleh

    Ok, awesome. No more warnings or errors.

    But...the program isn't doing what it's intended to do.

    The bubSort function is suppose to bubble sort array, which is a struct elecVotes, by the income field.

    Any clues?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you shouldn't be using .votes in your comparison, if you want to sort by income. (Not that the master knew that you wanted to sort by income.)

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    I didn't use .vote in my code, I noticed that master used it, so I changed it.

    I'm using .income, but it still doesn't sort at all.

    In my code, I just print the sorted list into a text file. I'm guessing it can't be the fprintf(), so it must be something wrong with the sort function still.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    j needs to be bigger than i, not less than i, for that inner for loop.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    forgot to mention that I changed that too.

    still not working...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, then what does your code look like?

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    here ya go

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LESS(x,y) ((x).income < (y).income)
    #define EXCH(x,y) { struct elecVotes tmp = x; x = y; y = tmp; }
    #define COMPEXCH(x,y) if (LESS(x,y)) EXCH(x,y)
    
    struct elecVotes
    {
        char name[3];
        int votes;
        int income;
    };
    
    struct incomeTmp
    {
        char name[3];
        int income;
    };
    
    void bubSort(struct elecVotes a[], int left, int right);
    
    int main()
    {
        struct elecVotes array[51];
        struct incomeTmp array2[51];
        int i;
        int j;
    
        FILE* file1;
        FILE* file2;
        FILE* file3;
    
        file1 = fopen("elecvotes.txt", "r");
        if (file1 == NULL) return -1;
    
        file2 = fopen("income.txt", "r");
        if (file2 == NULL) return -1;
    
        file3 = fopen("states_by_income.txt", "w");
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file1,"%2s %d", array[i].name, &(array[i].votes));
        }
    
        for (i = 0; i < 51; i++)
        {
            fscanf(file2,"%2s %5d", array2[i].name, &(array2[i].income));
        }
    
        i = 0;
        j = 0;
    
        while ((i < 51) && (j < 51))
        {
            if (strcmp(array[i].name, array2[j].name) == 0)
            {
                array[i].income = array2[j].income;
                i++;
                j = 0;
            }
            else
            {
                j++;
            }
        }
    
        bubSort(array, 51, 0);
    
        for (i = 0; i < 51; i++)
        {
            fprintf(file3,"%2s   %2d   $%d\n", array[i].name, array[i].votes, array[i].income);
        }
    
        fclose(file1);
        fclose(file2);
        fclose(file3);
    
        return EXIT_SUCCESS;
    }
    
    void bubSort(struct elecVotes a[], int left, int right)
    {
        int i, j;
        for (i = left; i < right; i++)
            for (j = right; j > i; j--)
                COMPEXCH(a[j-1], a[j]);
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    bubSort(array, 51, 0);
    Generally, 0 is considered to be the left and 51 to be the right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-06-2008, 04:06 AM
  2. Definitions for gcc errors and warnings?
    By cpjust in forum C Programming
    Replies: 12
    Last Post: 10-04-2007, 09:18 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 11:16 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM