Thread: Need some help with errors in the code

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Naperville, Illinois, United States
    Posts
    3

    Need some help with errors in the code

    hey, Im not good with this stuff. Im writing this program in C and it gives me some errors, and I cant figure out how to solve it when it gives me those errors:

    Code:
    #include "lab10.h"
    
    int main(void)
    {
        int response;
        int count;
        string words[MAX_COUNT];
        
        /* your code goes here */
        
    while(1) 
        {
            response = showMenu();
           switch(response) 
            {
        
            case 0: return; /* your code goes here */
                    break;
            case 1: count = inputWords(string words[]);
                    break;
        case 2: displayWords(int count,string words[]);
            break;
            } /* end of switch */
        
        
        } /* end of while */
    
    
    
    } /*end main */
    
    
    
    
    int showMenu(void)
    {
        int response;
        /*make this printf function complete to display the whole menu*/
        printf(" Main Menu\n"
           " ---------\n"
           " 1. Input words\n"
           " 2. Display words\n"
           " 3. Search for an exact match\n"
           " 4. Search for a partial match\n"
               " 5. Calculate the average length\n"
           " 0. Exit\n");
    
        printf("\n Please enter a choice: ");
        /* use a scanf here to read users response from keyboard */
        scanf("%i", &response);
        return response;
    }
    
    int inputWords(string words[])
    {
    
        FILE * ptr;
    
        int count; /*declare and initialize the variable count*/
    
    
        ptr = fopen("illini.txt","r");
    
        /*copy the code for the loop as shown in lab10 instructions*/
    while( EOF != fscanf(ptr,"%s", words[count]))
           ++count;
      return count;
    
        /* Remember to change this return statement below to actually return the value of count.*/
        /* Also don't forget to update case 1:  in main. */
    }
    
    void displayWords(int count,string words[])
    {
         int i;
        for(i=0; i < count ;++i)
            printf("%s\n", words[i]);
    }
    compiler gives me this error:
    Code:
    lab10.c: In function 'main':
    lab10.c:19: error: expected expression before 'string'
    lab10.c:21: error: expected expression before 'int'
    lab10.c:21: error: too few arguments to function 'displayWords'
    I know in which lines is the problem, but i don't know how to fix it. I don't know what to do. any help would be appreciate it.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Code:
    case1: count = inputWords(string words[]);
    Here you are not calling the function properly.

    What variable do you want to perform the function inputWords on? That is what should go in the parenthesis.

    The other errors are similar.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Naperville, Illinois, United States
    Posts
    3
    thanks a lot for your help. This is only the first part. I have like 4 parts left, so if I get stuck, Ill post my problems.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    after your
    Code:
     
    #include "lab10.h"
    you need to call your functions below like
    Code:
    #include "lab10.h"
    
    int showMenu(void);
    int inputWords(string words[]);
    void displayWords(int count,string words[]);
    
    int main (void)
    {
    this calls for the functions for main. That should get rid of your problem and for the int error, you do not need to call int count. You can simple just call count since your function calls for a integer.

    Also did you #define MAX_COUNT after your include statement?

    also you don't have to call the string words[] in your main, just call words. You already set your string words[MAX_COUNT] to look for a array of string at a predetermined length. So calling it afterwards you only need the name of the variable like if I said

    Code:
    int i;
    count(i);
    printf("%d\n", i);
    I would not have to type int i into count just i. Did I at least shine some light?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Naperville, Illinois, United States
    Posts
    3
    Ya i get it, it was simple mistake, that I even didnt think about. This is a lab that im doing, everything was set up, Im just writing the functions that go in between steps. (ya the max count got declared in a different file lab.h)

    Now I am stuck with this one part. this part is a continuation of my first post. instructions say: Write a for loop to search for exact match between the entered words and a target string. To compare two strings, use strcmp function declared in string.h. The strcmp function returns 0 if two strings are the same, and non-zero value otherwise. If there is an exact match, increment a local counter num.

    so far i have this:
    Code:
    void exactMatch(int count, string words[])
    {
    
    int num;
    char target;
    
    printf("\n Please enter a word you would like to search for: \n");
    scanf("%s", &target);
        for(strcmp(words, target) == 0)
            num++;
      printf("The number of strings exactly matched with %s: %i\n", &target, &num);
    }
    am i anywhere close to what it is asking?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    you need a function to take the string entered and copy it to an array. Then have another function call the array and look to see if the LETTERS in the stored array match the ones in a predetermined string (would be the easiest way with a predetermined string aka char str1 = "I love my programming class";

    Once you get that you can call your function strcmp which btw would be nice to atleast include the code so people can understand your program better....

    And then you can print f the word it finds. Remember computer are DUMB. They only do math very fast. So I treat it as if I am talking to a baby when programming, you can't tell a student to read a book and then find a line that matches your line you wrote. You have to give the show the student the words, then tell him to remember them, then you can ask for the words he remembered, and then show him the words you want him to find. And finally he can search for them and then print them out.

    All that work for something so basic, but to CPUs they are like students they only do what you tell them to do :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with errors on my code!
    By ashleyd in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2011, 01:35 PM
  2. Replies: 2
    Last Post: 05-09-2010, 11:30 PM
  3. 2 errors in my code
    By johnnyg in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2006, 08:04 AM
  4. Errors in code
    By itzme in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2005, 05:11 PM
  5. Errors (not in code)
    By Da-Spit in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2002, 12:31 AM