Newbie function help please

This is a discussion on Newbie function help please within the C Programming forums, part of the General Programming Boards category; I am currently learing C with cprogramming.com's "C made easy" tutorial. I am trying to call a function in a ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Newbie function help please

    I am currently learing C with cprogramming.com's "C made easy" tutorial. I am trying to call a function in a switch case statement and when I try to compile it gives me "too few arguments to function" error. Can someone please help me figure this out?

    Thanks in advance!

    Code:
    #include <stdio.h>
    
    
    int playgame ( int input );
    int loadgame ( int input );
    int playmultiplayergame ( int input );
    
    
    int main()
    {
    int input;
    
    printf("1. Play game\n");
    printf("2. Load game\n");
    printf("3. Play Multiplayer\n" );
    printf("4. Exit Game\n");
    printf("Selection: ");
    scanf("%d", &input);
    switch ( input ) {
    	case 1:
    		 playgame( input );
    		break;
    	case 2:
    		loadgame( input );
    		break;
    	case 3:
    		playmultiplayergame( input );
    		break;
    	case 4:
    		printf("Thanks for playing\n");
    		break;
    	default:
    		printf("Bad input, quitting!\n");
    		break;
    		
    	}
    int playgame ( int input )
    	{
    	printf("You selected play game. Have fun!\n");
    	}
    	
    	int loadgame ( int input )
    	{
    	printf("Load game from where?\n");
    	}
    	
    	int playmultiplayergame ( int input )
    	{
    	printf("Who would like to play with?\n");
    	}
    
    }
    Last edited by matthewmystar17; 08-29-2005 at 12:41 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You need to post the code that is giving you problems.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    Code:
    */ Working code */
    #include <stdio.h>
    
    
    int playgame ( int input );
    int loadgame ( int input );
    int playmultiplayergame ( int input );
    
    
    int main()
    {
    int input;
    
    printf("1. Play game\n");
    printf("2. Load game\n");
    printf("3. Play Multiplayer\n" );
    printf("4. Exit Game\n");
    printf("Selection: ");
    scanf("%d", &input);
    switch ( input ) {
    	case 1:
    		 playgame( input );
    		break;
    	case 2:
    		loadgame( input );
    		break;
    	case 3:
    		playmultiplayergame( input );
    		break;
    	case 4:
    		printf("Thanks for playing\n");
    		break;
    	default:
    		printf("Bad input, quitting!\n");
    		break;
    		
    	}
    } */ To here */
    
    int playgame ( int input )
    {
    printf("You selected play game. Have fun!\n");
    }
    	
    int loadgame ( int input )
    {
    printf("Load game from where?\n");
    }
    	
    int playmultiplayergame ( int input )
    {
    printf("Who would like to play with?\n");
    }
    
    */ } moved from here */

    You have placed the main() end tag "}" in the end of the program. You have to
    have it where the main function ends.
    Last edited by Laars; 08-29-2005 at 12:58 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    thanks

    Thanks a lot. I was looking for big issues.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > Can someone please help me figure this out?
    Yeah, really big
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Laars
    Code:
    */ Working code */
    Oh no it's not. Not as of the first character on the first line it isn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    shouldn't he also have the return 0; at the end to?

    and im not sure if you know or not, but you can also put your functions before mian.

    Code:
    #include <stdio.h>
    
    int playgame (int input)
    {
        printf("You selected play game. Have fun!\n");
    }
    	
    int loadgame (int input)
    {
        printf("Load game from where?\n");
    }
    	
    int playmultiplayergame (int input)
    {
        printf("Who would like to play with?\n");
    }
    
    int main()
    {
        int input;
    
        printf("1. Play game\n");
        printf("2. Load game\n");
        printf("3. Play Multiplayer\n" );
        printf("4. Exit Game\n");
        printf("Selection: ");
        scanf("%d", &input);
    
        switch (input)
        {
            case 1:
    	    playgame(input);
    	    break;
    	case 2:
    	    loadgame(input);
    	    break;
    	case 3:
                playmultiplayergame(input);
    	    break;
    	case 4:
    	    printf("Thanks for playing\n");
    	    break;
    	default:
                printf("Bad input, quitting!\n");
    	    break;
        }
    
        return (0);
    }
    Edit: opps i was just gonna make evertying else with a short but i forgot to...
    Last edited by Massaker; 08-29-2005 at 05:14 PM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > short input;
    Should be an int, given the usage everywhere else in the code (especially the scanf which is now broken).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    On all functions that return int but main(), you need a return 0. If there is no return statement in main(), return 0 is assumed. That doesn't cover void main(), though.

    [edit]
    That doesn't cover your other functions that return int, either. Make them void or return something.
    [/edit]
    Last edited by dwks; 08-29-2005 at 07:55 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by dwks
    On all functions that return int but main(), you need a return 0. If there is no return statement in main(), return 0 is assumed. That doesn't cover void main(), though.
    You must never have void main(), the C standard requires main to return int.

    Also, to my knowledge, return 0 is not assumed, unless they've changed this for C99.

  11. #11
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    You must never have void main()
    If you want a portable program on a hosted implementation, yea. But never say never, because you can get bitten by an exception.
    return 0 is not assumed, unless they've changed this for C99.
    C99 does make this change.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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