printing statements

This is a discussion on printing statements within the C Programming forums, part of the General Programming Boards category; hi anyone see anything wrong with this? i keep getting these errors. Do i need to define prototypes or something? ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    printing statements

    hi anyone see anything wrong with this?
    i keep getting these errors. Do i need to define prototypes or something?
    All i want to do is get a random quote when i call that function. When i call it i want it to print out a quote.

    warning C4013: 'get' undefined; assuming extern returning int
    error C2371: 'get' : redefinition; different basic types

    Code:
    void get(void) 
    {
      	int val;
      	val=rand()%4;
      	switch(val) {
      	case 0: printf( "Hello!\n " ); break;
      	case 1: printf( "How Are YOU!\n " ); break;
      	case 2: printf( "abcdefg\n" ); break;
      	case 3: printf( "hey there\n" ); break;
      	case 4: printf( "What!\n" ); break;
    	}
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hmm i threw in some function prototypes and it produced no error. Why would you need function prototypes in this case and not in some other cases??

    Aslo im using MVC++6.0 and sometimes i get an error with linking? ive compiled and it says no warnings or erros then this pops up. any ideas??


    --------------------Configuration: get - Win32 Debug--------------------
    Linking...
    get.obj : error LNK2001: unresolved external symbol _get
    Debug/get.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    get.exe - 2 error(s), 0 warning(s)
    there are only 10 people in the world, those who know binary and those who dont

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    It works for me as long as get() is declared before it is called. Can't really see why you would get the linking error if the code is structured correctly. This should work for example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void get(void);
    
    int main(void)
    {
    	get();
    
    	return 0;
    }
    
    void get(void) 
    {
      	int val;
      	val=rand()%4;
      	switch(val) {
      	case 0: printf( "Hello!\n " ); break;
      	case 1: printf( "How Are YOU!\n " ); break;
      	case 2: printf( "abcdefg\n" ); break;
      	case 3: printf( "hey there\n" ); break;
      	case 4: printf( "What!\n" ); break;
    	}
    }
    Also note that rand()%4 will never return 4...

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    why wont rand()%4 ever equal 4?

    will rand() ever equal 0? then 0%4 = 4? isnt it?
    there are only 10 people in the world, those who know binary and those who dont

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    isn't get a keyword in a cpp compiler? Try renaming your function GET, getR, or something. I think that is what "Redifinition" is going on about.

    You don't need the prototype if you provide the definition before main unless you're using Code Warrior that is.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 02:13 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM

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