Thread: Function not working! :*(

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    Function not working! :*(

    Here is the program I am working on. It is very simple and is going to be used for a school project... but I am getting an error on my character function. Here is the code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define WAIT 999999999
    int wait(void);
    char menu(char com[30]);
    FILE *fp;
    
    int main()
    {
        
        char Name[20], Pass[20], File[30];
        char Fpass[20], Fname[20], command[30];
        int suffix = '.txt';
        printf("Enter the name of the file: ");
        scanf("%s", &File);
        if(strchr(File, suffix))
        {
          fp = fopen(File, "r");
        }
        else
        {
          printf("*Error* \nMake sure to put the suffix at the end of the file name!\n");
          printf("Ending program...");
        }
        printf("Enter name: ");
        scanf("%s", &Name);
        printf("Enter pass: ");
        scanf("%s", &Pass);
        fscanf(fp,"%s", &Fname);
        if(strcmp(Name, Fname) != 0)
        {
           printf("Either the user or the password is incorrect!\n");
           printf("Ending program...");
           wait();
           system("exit");
        }
        fscanf(fp,"%s", &Fpass);
        if(strcmp(Pass, Fpass) != 0)
        {
           printf("Either the user or the password is incorrect!\n");
           printf("Ending program...");
           wait();
           system("exit");
        }
        printf("Logging in...\n");
        wait();
        do{
        printf("Command: ");
        scanf("%s", &command);
        menu(command);
        command = menu();
        }while(strcmp(command, "quit") != 0);
    }
    int wait(void)
    {
         int x; 
         for(x = 0; x < WAIT; x++)
         ;
         return x;
    }
    char menu(char com[30])
    {
        char com[30];
        if(strcmp(com, "user") == 0)
        {
           char New_user[20], New_pass[30];
           printf("Enter username: ");
           scanf("%s", &New_user);
           fscanf("Enter password: ");
           scanf("%s", &New_pass);
           printf("Creating new user...");
           fscanf(fp, "%s\n", &New_user);
           fscanf(fp, "%s\n", &New_pass);
           fscanf(fp, "----------------");
           printf("Done!\n");
        }
        return com;
    }
    I am getting an error on line 7... too few arguments to function 'char menu(char*)
    line 53: at this point in file
    " : incompatible types in assignment of 'char' to 'char[30]' in function 'char menu(char*)';
    line 65: declaration of 'char com[30] shadows a paramter
    " : [warning] address of local variable 'com' returned
    line 71: cannot convert 'const char*' to 'FILE*' for argument '1' to 'int fscanf(FILE*, const char*,...)'
    line 79: invalid conversion from 'char*' to 'char'


    These are all the errors verbatim that I received in the program, any help would help ALOT! Thanks all
    -- Will you show me how to c++?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You would have been able to solve this without help if you had put the error/warning messages and the code side by side. When an error occurs at line 53, a rough rule of thumb is to look at line 53 in the source code (and, if that doesn't work, some of the lines before line 53).

    To get you started, I'll help with the first two error messages. They are being caused by this;
    Code:
        menu(command);
        command = menu();
    The first line I've quoted is OK, but throws away the return value. The second line triggers the first error message because menu() is declared as taking an argument, and you haven't supplied one. It triggers the second error message because menu() returns a char, but command is an array of 30 char, so the types don't match.

    Normally, I'd help with the remainder of the error messages. But, since you obviously haven't even tried (i.e. you got messages from your compiler, didn't even look at the code or think about why it would trigger those messages, but posted here expecting others to do your work) I'm not inclined to.

    To quote your signature "Succeeding is habit. Unfortunatly so is failing." You have asked for help before trying to solve the problem yourself, so you are establishing a habit for failing.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    you know grumpy is right

    the mistakes you have made are easy to fix if you just go through it. the code isn't that long and the same mistakes are made over and over again.

    for example
    Code:
    scanf("%s", &array_name);
    
    /* should be */
    scanf("%s", array_name);
    
    /* but i recommend using fgets */
    fgets(array_name, sizeof(array_name), stdin);

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    hey thanks for the help, but i am still learning and i had to learn with a crash course through a 1980's learn C in 21 days. But past the excuses sorry for putting on crappy code. After i posted it i fixed a whole bunch of the problems, but i still need to know how to make a function accept character arrays, and return them. HELP plz.
    -- Will you show me how to c++?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    e.g
    Code:
    char *func(char a[], char b[]) {
        int i=0;
        while(*b!='\0') {
            *(a+i)=*b;
            b++;
            i++;
        }
        *(a+i)=*b;
        return a;
    }
    this is equivalent to the strcpy function declared in the string.h library

    i suggest you go to this link http://www.cprogramming.com/tutorial/c/lesson8.html

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char menu(char com[30])
    {
        char com[30];
    you have argument of name com
    and local var of name com that hides the argument. You should remove the second line

    Code:
    char menu(char com[30])
    {
     ...
        return com;
    }
    com has type char*
    menu in your case returns char - these types are incompatible
    or change the return type
    char* menu(char* com)

    or return value

    Why do you need to return com if you don't modify it inside the function?
    Don't forget to check the memory overrun during reading user input. Check the FAQ how you can do it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by mramazing
    hey thanks for the help, but i am still learning and i had to learn with a crash course through a 1980's learn C in 21 days. But past the excuses sorry for putting on crappy code. After i posted it i fixed a whole bunch of the problems, but i still need to know how to make a function accept character arrays, and return them. HELP plz.
    Your menu function already accepts a char array.

    Returning a char array from a function is a bit more problematical and, in fact, is a weakness of C. The simplest way is to return a pointer. For example;
    Code:
    char *function();
    The problem is what you return. For example;
    Code:
    char *function()
    {
         char x[10];
         /*  put something in x  */
         return x;
    }
    causes problems because the array, x, in this function no longer exists when the function returns --- so the caller of the function cannot use it. A simple minded fix of that is;
    Code:
    char *function()
    {
         static char x[10];
         /*  put something in x  */
         return x;
    }
    but gives a problem because the array is overwritten between calls, so;
    Code:
         char *x, *y;
         x = function();
         y = function();   /* assume different contents placed in array on second call */
         another_function(x,y);   /* another_function() receives the same data via two arguments */
    Yet another option is this;
    Code:
    char *function()
    {
         char *x = malloc(10);
         /*  put something in x  */
         return x;
    }
    but this requires the caller to use free(the_returned_pointer). If the caller forgets to do that, there is a memory leak.

    There are a couple of other options for returning char arrays as well. But the catch is that they all introduce some problems. A better way is to force the caller to allocate the array. For example;
    Code:
    void function(char *buffer)
    {
        strcpy(buffer, "Hello");   /*  Assume buffer is an array with 6 or more char's */
    }
    
    void caller()
    {
         char buffer[10];   /* 10 > 6, so OK */
         function(buffer);
          /*  now we can use buffer */
    }
    
    void alternate_caller()
    {
         /*  compute a value of length */
         char buffer[length];   /* if length >= 6, we're OK */
         function(buffer);
          /*  now we can use buffer */
    }
    
    void alternate_caller_for_old_compilers()
    {
         /*  compute a value of length */
    
        char *buffer = malloc(length);   /* if length >= 6, we're OK */
         function(buffer);
    
          /*  now we can use buffer */
    
         free(buffer);   /* but we must remember to release it because we malloc'ed it */
    
    }
    In this set of examples, caller() will always work if the length of the array is known in advance (i.e. at compile time). If the length is not known at compile time (eg it is computed at run time) then
    - alternate_caller() will only work with C compilers that comply with the 1999 C standard.
    - alternate_caller_for_older_compilers() will work with older C compilers (before the 1999 C standard)
    Last edited by grumpy; 01-06-2007 at 11:30 PM.

  8. #8
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    thanks a lot, I think i have it know. TY to grumpy , i will show the code if any1 wants to see it.

    thanks all
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM