Thread: Need help with code

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    18

    Question Need help with code

    So I'm practicing structures and passing parameters in functions, and I'm trying to make a simple program where you choose between Mage, Warrior, and Healer classes. After choosing one, you enter your level, then if you are equal to or over lvl 15, you can choose to buy a weapon specially designed for each class. Then you enter the amount of gold you have, which the program then converts to in game gold (which I named PD), and if you have sufficient funds, you can buy it.

    I've only done Mage, and I wanted to test run it, and it first asks me to choose a class. Then when I do, the program just exits.
    What did I do wrong? (Sorry, it's kinda long)

    Need help with code-1-jpg
    Need help with code-2-jpg
    Need help with code-3-jpg
    Need help with code-screen-shot-2020-11-30-10-20-12-pm-jpg
    Last edited by leopoldo100; 12-01-2020 at 12:22 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of calling the mageWeapon function in the main function, you declared it.

    By the way, next time post your code in code tags instead of posting images.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Instead of calling the mageWeapon function in the main function, you declared it.

    By the way, next time post your code in code tags instead of posting images.
    How exactly do you do that? I tried putting "void mageWeapon(float amount); " right under int main, but it still did the same thing.
    And yes, I tried putting it in code tags but copying and pasting from xCode brought up all the color tags too so it just got way too messy, so I decided to SS it.
    Last edited by leopoldo100; 12-01-2020 at 12:39 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leopoldo100
    How exactly do you do that? I tried putting "void mageWeapon(float amount); " right under int main, but it still did the same thing.
    This is a declaration of the function named mageWeapon:
    Code:
    void mageWeapon(float amount);
    it tells the reader what is the function name, parameter types, and return type. You have also used it to indicate the parameter names, which is a good thing as long as it is consistent with the function definition, which looks like this:
    Code:
    void mageWeapon(float amount)
    {
        /* ... */
    }
    the function definition is the declaration of the function that also tells the reader what is the implementation of the function.

    This is a function call:
    Code:
    mageWeapon(amount);
    it tells the reader the name of the function that is called as well as the names of the arguments that are passed to the function. In fact, you wrote this in the definition of the mageWeapon function, which makes it a recursive call. In this case, such a recursive call is almost certainly a mistake.

    Quote Originally Posted by leopoldo100
    And yes, I tried putting it in code tags but copying and pasting from xCode brought up all the color tags too so it just got way too messy, so I decided to SS it.
    There should be an option to paste as plain text (or to copy as plain text). Xcode is a famous IDE, so it should support such a simple thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your player info is all wrong.
    Code:
    typedef enum { mage, warrior, healer } role;
    typedef struct {
      role role;
      char name[20];
      int level;
    } player;
    Which you can then initialise with something like
    Code:
    player newPlayer(void) {
      player p;
      int role;
      scanf("%d",role);
      if ( role == 1 ) p.role = mage;
      // etc
      scanf("%s",p.name);
      p.level = 1;
      return p;
    }
    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.

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    Quote Originally Posted by laserlight View Post
    This is a declaration of the function named mageWeapon:
    Code:
    void mageWeapon(float amount);
    it tells the reader what is the function name, parameter types, and return type. You have also used it to indicate the parameter names, which is a good thing as long as it is consistent with the function definition, which looks like this:
    Code:
    void mageWeapon(float amount)
    {
        /* ... */
    }
    the function definition is the declaration of the function that also tells the reader what is the implementation of the function.

    This is a function call:
    Code:
    mageWeapon(amount);
    it tells the reader the name of the function that is called as well as the names of the arguments that are passed to the function. In fact, you wrote this in the definition of the mageWeapon function, which makes it a recursive call. In this case, such a recursive call is almost certainly a mistake.


    There should be an option to paste as plain text (or to copy as plain text). Xcode is a famous IDE, so it should support such a simple thing.
    Thanks for your reply.
    I tried to change it to "mageWeapon (amount);", but it's coming up with an error saying "Use of undeclared identifier 'amount' ". Sorry, I am very new to programming so I need a little explanation on this if you can. I think my understanding of creating and calling functions is completely wrong.

    This is what I have so far. I didn't include the rest of the code here since it's the same.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef enum { mage, warrior, healer } role;
    typedef struct {
        
        role role;
        char name[25];
        int level;
        
    }player;
    
    
    char confirm;
    void question(void);
    void mageWeapon(float amount);
    
    
    player p;
    
    
    int main () {
        int a;
        
        printf("Welcome player. Please enter your class type:");
        printf("\n1) Mage \t2) Warrior \t3) Healer");
        scanf("%d", &a);
        if (a == 1) {
            
            p.role = mage;
            mageWeapon(amount); //error is occurring here
            
        }
    Last edited by leopoldo100; 12-01-2020 at 06:30 PM.

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    Oh and I forgot to mention I took @Salem 's advice and changed up my structure. I've never used "enum" before, is this correct?

  8. #8
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    So I've tried declaring "float amount;" at the top above int main (), but now when I run it and enter "1", now I'm getting a strange "(11db)" error (?) in green, and it's saying "Thread 1: breakpoint 1.1 (1)" in this line of code:

    Code:
    float resultPD = amount * 1.5;
    And the word "amount" is underlined.

    This is frustrating... I've been staring at the program for a good hour now and I've tried other things but none of them are working. I've been looking up tutorials for void functions but none of them are helping.

    Oh and something I forgot to mention, I've utilized @Salem 's comment and changed up my structure definition. Does this work?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks good so far with the enum.

    In main, you need to call the function with a value.
    Code:
    int main () {
        int a;
         
        printf("Welcome player. Please enter your class type:");
        printf("\n1) Mage \t2) Warrior \t3) Healer");
        scanf("%d", &a);
        if (a == 1) {
             
            p.role = mage;
            mageWeapon(42.0); //error is occurring here
             
        }
    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.

  10. #10
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    Quote Originally Posted by Salem View Post
    Looks good so far with the enum.

    In main, you need to call the function with a value.
    Code:
    int main () {
        int a;
         
        printf("Welcome player. Please enter your class type:");
        printf("\n1) Mage \t2) Warrior \t3) Healer");
        scanf("%d", &a);
        if (a == 1) {
             
            p.role = mage;
            mageWeapon(42.0); //error is occurring here
             
        }
    Do you know how I would have it so that whatever amount you enter becomes the value the function calls? Because essentially I want it so that the program asks the player (me) for the amount of current gold I have, and I enter the amount, then the program converts it to RP currency. I wrote this code in the void mageWeapon function. What should I put in the brackets for this function call?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leopoldo100
    Do you know how I would have it so that whatever amount you enter becomes the value the function calls? Because essentially I want it so that the program asks the player (me) for the amount of current gold I have, and I enter the amount, then the program converts it to RP currency. I wrote this code in the void mageWeapon function. What should I put in the brackets for this function call?
    Ah, but that implies that amount should not be passed as an argument. You probably want to make it a local variable in mageWeapon instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Ah, but that implies that amount should not be passed as an argument. You probably want to make it a local variable in mageWeapon instead.
    I was getting the idea that this is what I should do. Thanks! I'll try this and see if it works.

  13. #13
    Registered User
    Join Date
    Nov 2020
    Posts
    18
    I forgot to update here, but I got it to work! I just had to write that piece of code separately as you said. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread