Aggrovating C++ Assignment

Discussion in 'OT Technology' started by Macbeth, Mar 12, 2009.

  1. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Okay, so I am in a basic C++ programming course, and for some reason that I cannot figure out my function is displaying random values. I keep looking over the code and everything seems in order (to me), but it keeps displaying random values.

    The program should take in values for a score of a three frame game of bowling and display an X for strikes. I've written a function to display the strikes as X's, and when I change out the X for a random number it displays that fine. Obviously it displays the correct value for anything but 10 (the next assignment deals with input checking), but yeah...

    Code:
    int lve(int n)
    {
     if (n == 10)
      {
       char n = 'x';
       return n;
      }
     else
      {
       return n;
    }
     
  2. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Your return type for this function is int
    but for a strike you create a char type then return the character 'x' which would mean the random value you are getting is 120, the decimal representation of 'x'.
     
  3. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Ah. I see. I would use a void function to return an X, but then I would cluelessly fumble around trying to format it correctly. Do you know of any good way to use a void function to display a strike? Sigh.
     
  4. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    So I redid the function as a char and it looks like it's working. For some reason it's displaying a ÿ for ball2 even though it is set to -1, although since the assignment asks me to display a '-' for non rolled balls/misses that probably doesn't matter.
     
  5. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Your function is really only testing if you got a strike, why not just return a boolean type. return true if you got a strike, and false if you didnt.

    based on the return value you know to either print the value or an 'x'
     
  6. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Part of the function is supposed to display a '-' if a ball isn't rolled or if it is a miss. So now I'm getting more issues... ugh. I set ball2, ball4, and ball7 to -1 for later reference.

    EDIT: Strikes and non rolled balls are showing up fine, but misses and rolled balls other than strikes don't show up.

    Code:
    Frame   Ball 1  Ball2   Ball3
    ----------------------------------
    1               -
    2               -
    3       -       X
    Is the output from:

    Code:
    Enter pin total for 1st ball in frame 1: 5
    Enter pin total for 2nd ball in frame 1: 0
    Enter pin total for 1st ball in frame 2: 5
    Enter pin total for 2nd ball in frame 2: 0
    Enter pin total for 1st ball in frame 3: 0
    enter pin total for 2nd ball in frame 3: 10
    Enter pin total for 3rd ball in frame 3: 5
    New function:

    Code:
    char lve(int n)
    {
     if (n == 10)
      {
       char n = 'X';
       return n;
      }
     if (n == -1)
      {
       char n = '-';
       return n;
      }
     if (n == 0)
      {
       char n = '-';
       return n;
      }
     else
      {
       return n;
    
      }
    }
    
     
    Last edited: Mar 12, 2009
  7. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    your final else is returning an int, the unchanged value you sent to the function, but it is converted into a char by the return type.

    in the case you posted, you got nothing printed where it should be a 5.
    When your function converts the integer 5 to a char, it represents a non-printable character so you are getting a blank space.

    characters
    '1' = 49
    '2' = 50
    .......
    '8' = 56
    '9' = 57

    so if you added 48 to the final else return value you would print the proper value, as a character
     
  8. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    This ascii table show you what each decimal number is represented as a char
    [​IMG]
     
  9. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    That seems to have fixed it. It's a huge work around for such a simple task, but if it gets the job done I guess it works for me. Thanks for the help.
     
  10. CrazyInteg

    CrazyInteg Honda-Acura.net OG

    Joined:
    Dec 30, 2000
    Messages:
    12,206
    Likes Received:
    3
    Location:
    Omaha
    It's 1 or 2 extra lines of code. That's not a huge workaround.
    This is just a feature of c++.
     
  11. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    I'm just supposing there's a simpler means of doing it that doesn't pull any added information about using char because the professor hasn't mentioned anything about it, but as long as it produces the desired results I have no room to complain.
     
  12. CrazyInteg

    CrazyInteg Honda-Acura.net OG

    Joined:
    Dec 30, 2000
    Messages:
    12,206
    Likes Received:
    3
    Location:
    Omaha
    The problem is that your function returns an integer. So you have to send some integer back to represent that x, and after that you need to translate it to an x. I haven't coded C++ since college, so I couldn't give you an easier idea. I would probably just return a 90 for a strike and then when you see a 90 change it to an x in your main program.

    C++ is a statically typed language. That's your "problem". It's actually a feature, and the programmer just needs to learn to deal with that or actually learn to take advantage of that. Krakerjak had the best idea with the boolean value.

    Pseudocode (I haven't used C++ since college)
    Code:
    bool is_strike (int n) { 
      if (n == 10) { return 1; }
      return 0;
    }
    
     
  13. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Sure, there are better ways to code this but i was just helping you debug your own. You learn more about programming by debugging code than you do writing code.

    At least you learned a few things about char and int types
     

Share This Page