Thread: Help with (C99 vs Visual Studio)

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    15

    Help with (C99 vs Visual Studio)

    Hello.

    I'm having some trouble with my program. The objective of this code is to read in a file of strings and count the frequency/occurrence of each string in the file. My code is fine and works 100% correctly, but only in C99. I'm using both Code Blocks and Visual Studio C++ 2010 compiler. It works in code blocks because its C99, but because Visual Studio does not support C99 it will not compile even though it's the same EXACT code. The compiler says the parameter is not allowed when it tries to read my "char[] [max]" (you can find this in the third line after I declare int main in the binary Search Function). Does anyone have a clue how to fix this?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define MaxWords 1000
    #define MaxLength 100
    #define MaxWordBuffer MaxLength+1
    
    
    int main()
    {
        int getWord(FILE *, char[]);
        int binarySearch(int, int, char [], int max, char [][max]);
        void addToList(char[], int max, char [][max], int[], int, int);
        void printResults(int max, char [][max], int[], int);
        char wordList[MaxWords][MaxWordBuffer], word[MaxWordBuffer];
        int frequency[MaxWords], numWords = 0;
    
    
        FILE * in = fopen("msu1.txt", "r");
        if (in == NULL)
        {
            printf("Cannot find file\n");
            exit(1);
        }
    
    
        int h;
        for (h = 1; h <= MaxWords ; h++) frequency[h] = 0;
    
    
        while (getWord(in, word) != 0)
        {
            int loc = binarySearch (0, numWords-1, word, MaxWordBuffer, wordList);
            if (strcmp(word, wordList[loc]) == 0) ++frequency[loc]; //word found
            else //this is a new word
                if (numWords < MaxWords)
                {
                    addToList(word, MaxWordBuffer, wordList, frequency, loc, numWords-1);
                    ++numWords;
                }
                //else fprintf("'%s' not added to table\n", word);
        }
        printResults(MaxWordBuffer, wordList, frequency, numWords);
    }
    
    
    int getWord(FILE * in, char str[])
    {
        char ch;
        int n = 0;
        // read over white space
        while (!isalpha(ch = getc(in)) && ch != EOF) ; //empty while body
        if (ch == EOF) return 0;
        str[n++] = tolower(ch);
        while (isalpha(ch = getc(in)) && ch != EOF)
            if (n < MaxLength) str[n++] = tolower(ch);
        str[n] = '\0';
        return 1;
    } // end getWord
    
    
    int binarySearch(int lo, int hi, char key[], int max, char list[][max])
    {
        while (lo <= hi)
        {
            int mid = (lo + hi) / 2;
            int cmp = strcmp(key, list[mid]);
            if (cmp == 0) return mid; // found
            if (cmp < 0) hi = mid - 1;
            else lo = mid + 1;
        }
        return lo;
    }
    
    
    void addToList(char item[], int max, char list[][max], int freq[], int p, int n)
    {
    
    
        int h;
        for (h = n; h >= p; h--) 
        {
            strcpy(list[h+1], list[h]);
            freq[h+1] = freq[h];
        }
        strcpy(list[p], item);
        freq[p] = 1;
    }
    
    
    void printResults(int max, char list[][max], int freq[], int n)
    {
        int h;
        printf("\nWords        Frequency\n\n");
        for (h = 0; h < n; h++)
            printf("%d. %-15s %2d\n", h, list[h], freq[h]);
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    15
    My mistake, the wrong file. For some reason I don't see where to update the file it only allows me to add image, but you can still use that file as a reference.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Visual Studio does not support C99
    You know this and yet you are expecting Visual Studio to compile and link C99...why? Lack of support means you aren't going to get a C99 program to work, period. Microsoft is more interested in supporting C++ development, or its own tech, like C#.

    The "fix" is to rewrite the code in C90 because that's what Visual Studio supports, or to use something else. char[][max] needs to become a char ** and then you can just use max to stay in bounds.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    Instead of using the variable max for the arrays in your function prototypes you should be using your #defined constants that match the arrays you seem to be passing to these functions.

    Also those function prototypes should probably be moved to before main().

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Code:
    int frequency[MaxWords]
    
    // ...
    
    for (h = 1; h <= MaxWords ; h++) frequency[h] = 0;
    Arrays start at zero and go to "size - 1". So you are going over the end of your buffer here by one.

    If you want to initialize the entire array to zero, a simple trick is:

    Code:
    int frequency[MaxWords] = {0};
    According to the standard, if there are fewer values in the brace-enclosed list than there are elements, the rest are set to zero.

    Quote Originally Posted by c11-draft
    6.7.9 Initialization

    ...

    21) If there are fewer initializers in a brace-enclosed list than there are elements or members
    of an aggregate,
    or fewer characters in a string literal used to initialize an array of known
    size than there are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage duration
    .

    ...

    10) If an object that has automatic storage duration is not initialized explicitly, its value is
    indeterminate. If an object that has static or thread storage duration is not initialized
    explicitly, then
    :
    — if it has pointer type, it is initialized to a null pointer;
    — if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    if it is an aggregate, every member is initialized (recursively) according to these rules,
    and any padding is initialized to zero bits
    ;

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    15
    Thanks a lot Matticus! I incorporated your advice in my program.

    Thank you whiteflags and Jimblumberg.

    Jimblunberg - I tried to incorporate your advice but it did not work correctly. But it actually compiles now so that's a huge plus. It seems to only read half the words in the file and doesn't count the frequency, yet it says each word has a frequency count of 1 which is untrue. Any help anyone? If you can demonstrate with an example it will be so much appreciated.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    15
    Nevermind, I got it!! Thanks for all your help!

  8. #8
    Registered User
    Join Date
    Mar 2015
    Posts
    15
    One last question, anyone have a clue on how I would print the frequency of each word in descending order?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Well, one way might be to use a sorting routine on your frequency array ... and whenever a swap is done, also swap the words at those indices in your word list array.

    Or you can try re-writing your program to use a structure instead, that will contain both a given word and its frequency. Then you'd only have one array (of struct) to sort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create .bat file in Visual C++? Ms Visual Studio 2010
    By rousse101 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2015, 04:55 PM
  2. Visual Studio DLL and Visual Basic
    By mathguy in forum C Programming
    Replies: 18
    Last Post: 12-08-2011, 03:55 PM
  3. Visual C++ and Visual Studio 2010
    By 03jh01 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 04:03 AM
  4. visual studio 6 and visual studio.net difference
    By gemini_shooter in forum Tech Board
    Replies: 5
    Last Post: 02-04-2006, 01:32 AM
  5. Replies: 1
    Last Post: 05-26-2004, 09:59 AM