Thread: compile issues

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    63

    compile issues

    I am trying to write a averaging of 10 numbers program. When I compile it, it prints nothing to the screen. I checked everything and looks to be correct! Maybe there something i am not catching. I figured it might be my computer being wacky again!?? Sometimes when I use gets, fgets or getchar, my compiler saying... "Using _____ is dangerous" and wont compile but if i save the code and restart my compiler and open it under a new file it will run. idk heres the code so far

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int avg(void);
    
    int main(void)
    {
        int avg(void);
    }
    
    
    int avg(void)
    {
        char avg[10];
        printf("Please enter a number. ");
        fgets(avg, sizeof(avg), stdin);
        printf(avg);
        return 0;
    }
    That would be the second issue. Why is my compiler doing that??
    Last edited by codecaine_21; 09-22-2010 at 01:18 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    int main(void)
    {
        int avg(void);
    }

    You are not calling avg above. Try:

    Code:
    int main(void)
    {
        avg();
        
        return 0;
    }
    I also added a return statement, which you should include, unless you are compiling as c99. It never hurts to have it there. In the case of your program, you could do something like:

    Code:
    int main(void)
    {
        return avg();
    }
    Last edited by kermit; 09-22-2010 at 09:57 AM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    yeah that worked and another thing, have you ever heard of that issue i am having with my compiler???

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Hard to say what the issue is there. What compiler are you using? Are you using an IDE?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    its called code blocks for ubuntu server

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Could be something glitchy with Code::Blocks saving the source file. I have seen that sort of thing happen where you hit save, but for some reason the IDE does not actually update the file.

    Edit: GCC will definitely warn you* about using gets though, whether you have warnings turned on or not, so that is what that is all about.

    * Technically, it is the linker that complains about using gets().
    Last edited by kermit; 09-22-2010 at 03:24 PM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    well it it just wont let me compile when using gets or something similar but if code blocks is restarted it will. Sometimes when i compile it opens up libraries and layout for code blocks that only have read permissions. Do you know of a better compiler for my system??

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    its a pretty big pain in the ass!!!

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If you want to use an IDE, look into Kdevelop, or Anjuta, or Netbeans, or.. There is another, but I can't remember. Alternatively, you could use gedit (or vim, or whatever) to edit the source code, and then just call GCC directely from the command line. That is the way I do it, but each to his own.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    i have used vim and gedit but i have never compiled from command line. How does that work if i have errors? Will it catch them if i do it this way?

  11. #11
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    I use Gedit. To compile from command line using gcc you could do
    Code:
    gcc -Wall -o target_filename file.c
    Or if you have multiple sources, use a Makefile

    Code:
    SOURCES = main.c foo.c
    TARGET=main
    CC = gcc
    CLFAGS = -g -Wall
    OBJECTS = $(SOURCES:.c=.o)
    
    all: $(TARGET)
    $(TARGET): $(OBJECTS)
         $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS)
    
    clean:
         rm -rf $(OBJECTS) $(TARGET)
    Something like that would work. I dont have a make utility in windows, not from this machine so...

    And to compile, all you'd have to do at command line is: "make"
    Last edited by Syscal; 09-22-2010 at 10:23 AM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by codecaine_21 View Post
    well it it just wont let me compile when using gets or something similar but if code blocks is restarted it will.
    Then don't use them. gets is particularly dangerous and should never be used.
    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.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    Thanks syscal! And Elysia, i am trying to get out of the habbit! This is for school so fgets could be used "for now". So i am having trouble with the program. So i guess its not that simple for me after all! But i got to the point where i have my program repeat printf("Enter a number "); 10 times. And store it into an array. But i cannot get the array to increment every time the for loop iterates. For example, at the start of the iteration the array would be at 0. Then the next, at 1. And each time it iterates it stores the integer in the next array. then prints them. The print is just for a test. The ultimate goal is an average of 10 numbers program.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If I put it this way:

    For every i:
    print Enter a number
    Store the number in the ith element.
    Where i is [0, 10).

    Does it help?

    If not, try posting the code so we can see what the problem is exactly and guide you accordingly.
    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.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    i need to print the array. The print is testing it to see if its working. My ultimate goal is to have an averaging of 10 numbers program, keep that in mind!

    Code:
    #include <stdio.h>
    
    int avg(void);
    
    int main()
    {
        int i;
        for(i=0; i<10; i++) avg(); /*This loops avg1() 10 times*/
        for(i=0; i<10; i++) avg1[i-1]; /*This is the iteration for placing the numbers into the array*/
        return 0;
    }
    
    
    int avg()
    {
        char avg1[9];
        printf("Please enter a number: ");
        scanf("%d", &avg1[0]);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  2. compile program?
    By Goosie in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2005, 02:26 PM
  3. dvv-cpp not able to compile?
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-23-2004, 06:36 PM
  4. Replies: 3
    Last Post: 03-27-2004, 12:15 PM
  5. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM