Thread: Simple question on creating fucntions

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    24

    Simple question on creating fucntions

    Here are my files I have so far and here is my intent w/them. I am currently taking a c programming course at a university. I am trying to create a header file and a .c file that will make a function usable in the main function. We have been instructed to do it in this format. I am trying to just get it to work and am having some problems.

    What i'm trying to do w/this program. I have my main section of the program and I'm trying to call the function readValue, which reads in a number. I do this three times and then I add the numbers together in the main function and print the result.
    Code:
    //example.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "avg.h"
    
    int main(int argc, char *argv[])
    {
    int n1, n2, n3, sum = 0;
    double avg;
    
    n1 = readValue();
    n2 = readValue();
    n3 = readValue();
    
    sum = n1 + n2 + n3;
    
      system("PAUSE");
      return 0;
    }
    
    
    
    //avg.h
    
    #ifndef AVG_H
    #define AVG_H
    #include<stdio.h>
    
    int readvalue();
    
    #endif
    
    
    //avgfunction.c
    
    #include "avg.h"
    int readValue()
    {
    int num;
    printf("please enter an int > 0");
    scanf("%i", &num);
    while(num <=0)
    {
    printf("please enter an int > 0");
    scanf("%i", &num);
    }
    }
    return num;
    //error: 'undefined reference to readValue'

    If anyone could help me get this to work that would be awesome. I can not for the life of me find out why. I've read online and read the book, which is using a different style for creating functions.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    C is case-sensitive make sure all references to readValue() are indeed that.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make sure you compile all the .c files. Or alternatively, use an IDE that does it for you.
    And avg.h really does not need to include stdio.h. avgfunction.c, on the other hand, does.
    Also make sure to improve your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    Thanks for the input. I actually just through this together out of my notes (out of notepad) so the yes the indention sucks. I am using wxdev-C++ 6.10.2. I changed the things you mentioned, but still no luck. Still getting the same error:[LinkerError] 'undefined reference to readValue' Here is the updated code.


    Code:
    //example.c
    #include <stdio.h>
    #include <stdlib.h>
    #include "avg.h"
    
    int main(int argc, char *argv[])
    {
    int n1, n2, n3, sum = 0;
    double avg;
    
    n1 = readValue();
    n2 = readValue();
    n3 = readValue();
    
    sum = n1 + n2 + n3;
    
      system("PAUSE");
      return 0;
    }
    
    
    //avg.h
    #ifndef AVG_H
    #define AVG_H
    #include<stdio.h>
    
    int readValue();
    
    #endif
    
    
    //avgfunc.c
    #include "avg.h"
    #include <stdio.h>
    
    int readValue()
    {
    int num;
    printf("please enter an int > 0");
    scanf("%i", &num);
    while(num <=0)
    {
    printf("This stuff");
    scanf("%i", &num);
    }
    }
    return num;
    any ideas? I have a freakin quiz on this stuff tomorrow. lol
    Last edited by live4soccer7; 02-08-2009 at 03:26 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you compiling and linking all of your source files?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Go to cmd, and cd to the directory your sources are in.
    Code:
    gcc example.c avgfunc.c -o example.exe
    example.exe

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who says live4soccer7 is using gcc, or a command-line compiler for that matter?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    i am using wxdev-C++ 6.10.2. Is the code right? Is it something I am doing in the compiler/IDE that's causing it to not work?

    I'm hitting the compile and run button.


    Here is the compile log:

    Compiler: Default GCC compiler
    Executing gcc.exe...
    gcc.exe "C:\Documents and Settings\Nick\My Documents\CSCD 255 Projects HW\example.c" -o "C:\Documents and Settings\Nick\My Documents\CSCD 255 Projects HW\example.exe" -I"include" -L"C:\Program Files\Dev-Cpp\Lib"
    C:\DOCUME~1\Nick\LOCALS~1\Temp/ccaicaaa.o:example.c.text+0x32): undefined reference to `readValue'
    C:\DOCUME~1\Nick\LOCALS~1\Temp/ccaicaaa.o:example.c.text+0x3a): undefined reference to `readValue'
    C:\DOCUME~1\Nick\LOCALS~1\Temp/ccaicaaa.o:example.c.text+0x42): undefined reference to `readValue'
    collect2: ld returned 1 exit status
    Execution terminated
    Last edited by live4soccer7; 02-08-2009 at 03:49 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Have you added both source files to the project? Because it's only compiling one file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The fact that he is using Dev-C++?

    Dev-C++ uses GCC last time I checked.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    The fact that he is using Dev-C++?

    Dev-C++ uses GCC last time I checked.
    Not using Dev-C++ at all. Sure, it's an extension, but not Dev-C++.
    Yes, it uses gcc, but the question is - is live4soccer7 compiling via the command line?
    It appears not, and not everyone is comfortable doing so, which is exactly why I don't like jumping the gun, so to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    True, let's wait for some Dev-C++ expert to come, then .

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    I'm not sure what you mean, add both source file to the project. I thought it compiled all the 'tabs' that I had running in wxdev C++. I have the tabs for all the files I mentioned above:

    example.c
    avg.h
    avgfunc.c

    If there is something else I have to do besides press the compile and run button(the play button lol), what is it? (i'm not using command line, btw)

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    This thing is going to drive me CRAZY!!! I bet it's something simple too

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It is, but non of us use Dev-C++, so we don't know the answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Simple Question from Stupid Person...
    By Monmouth3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-02-2002, 08:47 AM