Thread: Please help! I get a "conflicting types for" error.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Please help! I get a "conflicting types for" error.

    Here is my code so far: (not a complete program)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX 81
    
    FILE *fileOpen(char fileName[]);
    int readFile(FILE* inputFile);
    
    int checkCreditCard(char *strng);
    int digitsOnly(char *strng);
    int checkLastDigit(char *strng);
    
    
    int main (void)
    {
    
    FILE *inputFile;
    char fileName[MAX];
    
    
    
    inputFile = fileOpen(fileName);
    readFile(inputFile);
    
    char *strng;
    checkCreditCard(strng);
    fclose(inputFile);
    return 0;
    }
    
    FILE * fileOpen(char fileName[]) //function A
    {
    FILE *inputFile;
    
    
           printf("Please enter a file name.\n");
           scanf("%s", fileName);
    
            inputFile = fopen(fileName, "r");
            if (!inputFile){
            printf("\aCould not open the input file.\n");
            exit (1);
           }
    
           return inputFile;
    
    }
    int readFile(FILE* inputFile) //function B
    {
    
     char strng[MAX];
     int len;
    
    
           fgets(strng, sizeof(strng), inputFile);
           len = strlen(strng);
           if (strng[len - 1] == '\n'){
                   strng[len - 1] = '\0';
           }
           while(fgets(strng, sizeof(strng), inputFile)){
    
               fgets(strng, len, inputFile);
    
           }
           return 0;
    
    
    
    }
    
    int checkCreditCard(char* strng)// Function C
    {
    int strlen(char* strng);
    int len;
    if (len != 16)
    {
           return 0;
    }
    return 1;
    digitsOnly(strng);
    }
    int digitsOnly(char *strng)// Function F
    {
    int i;
    
    for (i=0; strng [i] != '\0'; i++){
           if(!isdigit(strng[i]))
                   return 0;
    }
    
    return 1;
    }
    

    -When I compile it, it tells me "conflicting types for 'strlen'. I don't know how to resolve this. Please help! Let me know if you need more information. This is for a homework assignment. Thank you for your time.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    int strlen(char* strng);
    What's this line supposed to do inside your "checkCreditCard()" function?

    And why is everything purple?!
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by GReaper View Post
    Code:
    int strlen(char* strng);
    What's this line supposed to do inside your "checkCreditCard()" function?

    And why is everything purple?!
    -I'm trying to check the length of a string from a file and make sure that it is exactly 16 characters. We were told to use strlen for this. This is my interpretation of this. Not sure how to make it work...

    -Sorry, I didn't realize that it was all purple... It doesn't look purple from my screen, all the text is black... I'm writing the program in UNIX vi editor and then emailing it to myself so that I can copy and paste it here... that might be why. But it looks fine to me.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Anyway, your problem is that you try to redeclare "strlen()", that's why your compiler is complaining. You don't need to, because it's already declared inside "string.h"
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Okay, so should I just delete the whole line? I did, and it seems to compile and run fine, but I'm still not sure if everything is correct or not...

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The way you declared strlen() is wrong anyway (cause of the error). If you got the prototype correct, it would have been a wasted line, but at least it wouldn't have annoyed the compiler.

    Code:
    size_t strlen (const char *s);
    I haven't figured out why people do this sort of thing. Maybe they expect it to work like more modular capabilities in other languages. It doesn't.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by whiteflags View Post
    The way you declared strlen() is wrong anyway (cause of the error). If you got the prototype correct, it would have been a wasted line, but at least it wouldn't have annoyed the compiler.

    Code:
    size_t strlen (const char *s);
    I haven't figured out why people do this sort of thing. Maybe they expect it to work like more modular capabilities in other languages. It doesn't.
    - I really don't have any explanation for you. I can tell you that this is not the case for me since C is the first language that I am learning. I honestly don't even remember how I came up with "int strlen(char* strng);". I've been working on this code all day and wrote that particular section a few hours ago. And at this point I'm so tired and dead that I can't think straight. Oh the fun of being a programming newbie. Let's just hope it all eventually clicks.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    When you were told to use strlen, they meant that you should use the 'built in' function. You should not create your own function with the same name. Use strlen16() for example.
    Code:
    int strlen16(char *strng) {
    return strlen(strng) == 16; }
    ... which returns 1 if the string has length 16.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  2. "Segment Violation" error, fopen("r+")
    By jiboso in forum C Programming
    Replies: 1
    Last Post: 03-10-2011, 09:57 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM