Like Tree4Likes
  • 1 Post By vart
  • 3 Post By vart

Mutiple function definition errors.

This is a discussion on Mutiple function definition errors. within the C Programming forums, part of the General Programming Boards category; I am getting multiple function declaration errors when I try to execute the code. I did a function declaration at ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Mutiple function definition errors.

    I am getting multiple function declaration errors when I try to execute the code.

    I did a function declaration at the start, is the way I did it wrong ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int gradeint;
    char grade;
    
    /* function declaration */
    int getMark();
    void print(char grade);
    char convert(int mark);
    
    int main(int argc, char** argv) {
        getMark();
        convert(gradeint);
        print(grade);
        return (EXIT_SUCCESS);
    }
     
    int getMark(){
        printf("Enter your grade: ");
        scanf("%d", &gradeint);
        printf("What you have entered as your grade: %d\n", gradeint);
        return gradeint;
    }
    
    char convert(int mark){
        // converts int to char 
         grade = mark + '0';
         return grade;
    }
    
    void print(char grade){
        if(grade >= 80){
        printf("Your grade is an A\n");
        }
        else if (grade >= 70){
            printf("Your grade is a B\n");
        }
        else if (grade >= 60){
        printf("Your grade is a C\n");
            }
        else if (grade >= 50){
        printf("Your grade is a D\n");
            }
        else if (grade >= 40){
        printf("Your grade is an E\n");
            }       
        else{
        printf("Your grade is a F\n");
        }
    }

  2. #2
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    program closed before you see any thing
    no need to transform int to char (unless you need so for some reason (homework))


    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int gradeint;
    
     
    /* function declaration */
    int getMark();
    void print(int grade);
    
     
    int main(int argc, char** argv) {
        
        print(getMark());
        system("pause");
        return (EXIT_SUCCESS);
    }
      
    int getMark(){
        printf("Enter your grade: ");
        scanf("%d", &gradeint);
        printf("What you have entered as your grade: %d\n", gradeint);
        return gradeint;
    }
     
    
     
    void print(int grade){
        if(grade >= 80){
        printf("Your grade is an A\n");
        }
        else if (grade >= 70){
            printf("Your grade is a B\n");
        }
        else if (grade >= 60){
        printf("Your grade is a C\n");
            }
        else if (grade >= 50){
        printf("Your grade is a D\n");
            }
        else if (grade >= 40){
        printf("Your grade is an E\n");
            }      
        else{
        printf("Your grade is a F\n");
        }
    }
    Last edited by Hassan Ahmed; 07-12-2013 at 09:39 PM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Yes, I need to transform it to char because the given printf() function accepts char parameters.

    I did some debugging and some inspiration from your code, the issue seems to lie in the conversion.

    The conversion of int to char doesn't work. Why ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int gradeint;
    char grade;
    
    /* function declaration */
    int getMark();
    void print(char grade);
    char convert(int mark);
    
    int main(int argc, char** argv) {
        //getMark();
        char retConvert;
    
        retConvert = convert(getMark());
        print(retConvert);
        return (EXIT_SUCCESS);
    }
     
    int getMark(){
        printf("Enter your grade: ");
        scanf(" %d", &gradeint);
        printf("What you have entered as your grade: %d\n", gradeint);
        return gradeint;
    }
    
    char convert(int mark){
        
         printf("mark is %d\n", mark); // prints correctly
         // converts int to char
         grade = mark + '0';
         printf( "grade is %c\n", grade); // prints wrongly 
         return grade;
    }
    
    void print(char grade){
        if(grade >= 80){
        printf("Your grade is an A\n");
        }
        else if (grade >= 70){
            printf("Your grade is a B\n");
        }
        else if (grade >= 60){
        printf("Your grade is a C\n");
            }
        else if (grade >= 50){
        printf("Your grade is a D\n");
            }
        else if (grade >= 40){
        printf("Your grade is an E\n");
            }       
        else{
        printf("Your grade is a F\n");
        }
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    do not use the global variables.

    All if/else logic from print should go into convert function

    print will then be one liner

    Code:
    void print(char grade){
        printf("Your grade is an %c\n", grade);
    }
    Krabiki likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    vart , thanks for correcting my code logic and the errors .

    On the other hand, why does a global variable not work ? A global variable is supposed to have the scope of the whole file right ?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    global variables work, but make maintaining of projects of any reasonable size - hell.

    So you should from the beginning learn to avoid its usage when possible.

    In your case - since you are passing everything as a parameter - you do not need globals at all
    laserlight, Salem and Krabiki like this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Function Definition Help
    By Devon Smith in forum C Programming
    Replies: 6
    Last Post: 03-17-2013, 08:15 PM
  3. Replies: 7
    Last Post: 03-17-2013, 07:05 AM
  4. Replies: 4
    Last Post: 01-04-2013, 02:10 AM
  5. Definition of a function.
    By KOFI in forum C Programming
    Replies: 1
    Last Post: 04-13-2010, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21