Thread: Beginner having trouble passing arguments to functions :/

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Beginner having trouble passing arguments to functions :/

    Hey Guys

    I'm new to programming and also new to this forum.
    Having a little trouble passing arguments to functions.

    I wrote this simple program to help me get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do... could someone please tell me where i'm going wrong?

    Thanks in advance

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <windows.h>
    
    
    void menu(int HP,int Gold,int Armour);
    void HealthPosion(int HP);
    void BuyArmour(int Gold, int Armour);
    void GoblinAttack(int HP);
    void NewGame(int HP,int Gold,int Armour);
    void DisplayInfo(int HP,int Gold,int Armour);
    void pause();
    
    
    main()
    {
         int Gold;
         int HP;
         int Armour;
    
    
         menu(HP, Gold, Armour);
         return 0;
    }
    
    
    void menu(int HP,int Gold,int Armour)
    {
         int menuRep=1;
         int menuOption;
    
    
         do
         {
           system("cls");
           printf("\t\tDRAGON HUNTER\n");
           printf("--------------------------\n\n");
    
    
           printf("1] NEW GAME....\n\n");
           printf("2] DRINK HEALTH POSION....\n\n");
           printf("3] BUY SOME ARMOUR....\n\n");
           printf("4] GET ATTACKED BY A GOBLIN....\n\n");
           printf("5] DISPLAY PLAYER INFO....\n\n");
           printf("6] Exit....\n\n");
    
    
           printf("please select an option....\n");
    
    
           scanf("%d",&menuOption);
    
    
           switch(menuOption)
           {
              case 1:
                   system("cls");
                   printf("\t NEW GAME....\n");
                   printf("-----------------------------\n\n");
                   NewGame(HP,Gold,Armour);
                   pause();
                   break;
    
    
              case 2:
                   printf("DRINK HEALTH POSION....\n");
                   printf("-------------------------------------------------\n");
                   HealthPosion(HP);
                   pause();
                   break;
    
    
              case 3:
                   printf("\tBUY SOME ARMOUR....\n");
                   printf("-------------------------------------------------\n");
                   BuyArmour(Gold, Armour);
                   pause();
                   break;
    
    
              case 4:
                   printf("\t\tGET ATTACKED BY A GOBLIN....\n");
                   printf("-----------------------------------\n");
                   GoblinAttack(HP);
                   pause();
                   break;
    
    
              case 5:
                   printf("\t\tDISPLAY PLAYER INFO....\n");
                   printf("-----------------------------------\n");
                   DisplayInfo(HP,Gold,Armour);
                   pause();
                   break;
    
    
              case 6:
                   menuRep=0;
                   break;
    
    
           }
         }
         while(menuRep==1);
    }
    
    
    void NewGame(int HP,int Gold,int Armour)
    {
        HP=100;
        Gold=50;
        Armour=2;
    }
    
    
    void HealthPosion(int HP)
    {
        HP=HP+10;
    }
    
    
    void BuyArmour(int Gold, int Armour)
    {
        Gold=Gold-5;
        Armour=Armour+2;
    }
    
    
    void GoblinAttack(int HP)
    {
        HP=HP-10;
    }
    
    
    void DisplayInfo(int HP,int Gold,int Armour)
    {
         system("cls");
         printf("GOLD:%d\nHP:%d\nArmour:%d\n\n\n\n", Gold,HP,Armour);
         pause();
    }
    
    
    void pause()
    {
         system("pause");
    }
    (Hope im doing the tags right :/)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your problem does not appear to be related to passing arguments to the function(s). it appears to be that you are expecting to update those variables inside those functions. if you want to update them, you need to pass pointers instead of the variables themselves. in your GoblinAttack or BuyArmour functions, you are only modifying copies of those variables, not the variables you're hoping to change. it would likely make more sense to have a data structure, and pass its pointer around, since hp, gold and armour are all closely related data.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Elkvis has answered your question. I would like to offer some additional advice.

    Rather than start with seven functions, start by writing two. One to update the variables (like in your "NewGame()" function) and one to print those variables (like in your "DisplayInfo()" function). Make sure these work as you expect. If you had done this, you would have discovered the problem earlier, before a lot more of the code was written.

    Once they work, expand your program a little bit more. Take it in little steps.

    This link helps illustrate the idea of breaking a program into little steps and tackling them one at a time: A Development Process

    (Hope im doing the tags right :/)
    Yes, you did it just fine. Furthermore, your code indentation is great overall - that's rare for a new poster. Welcome to the forum!

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Thanks for the quick replies will wip out my text book and learn more about pointers and data structures

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble passing arguments to method
    By Dale in forum C# Programming
    Replies: 8
    Last Post: 12-02-2011, 12:40 PM
  2. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  3. passing arguments to functions
    By Micko in forum C Programming
    Replies: 4
    Last Post: 07-18-2004, 10:59 AM
  4. Passing functions as arguments
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:02 PM
  5. passing arguments through functions
    By cwd in forum C Programming
    Replies: 2
    Last Post: 09-30-2001, 06:07 PM