Thread: Could someone please point me in the right direction ~ basic structs and funtions

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    9

    Could someone please point me in the right direction ~ basic structs and funtions

    The exercise specifically asks for char parameters in the create_colour function for some reason.

    I'm getting a compiler error along the lines of invalid conversion from 'char' to 'char' which i'm also unclear on.

    Any help appreciated, thanks!

    Code:
    #include <stdio.h>
    
    
    struct colour{
      char red;
      char green;
      char blue;
    
    
      };
    
    
    struct colour create_colour(char r, char b, char g);
    void printcolour(struct colour a);
    
    
    int main(void)
    {
    
    
    struct colour red = create_colour(255, 0, 0);
    
    
    //struct colour green =create_colour(0, 255, 0);
    //struct colour blue =create_colour(0, 0, 255);
    //struct colour yellow =create_colour(255, 255, 0);
    //struct colour pink =create_colour(255, 0, 127);
    
    
    printcolour(red);
    
    
    return 0;
    }
    
    
    struct colour create_colour(char r, char b, char g)
    {
    
    
      struct colour x;
      sprintf(x.red, r);
      sprintf(x.green, g);
      sprintf(x.blue, b);
    
    
      return x;
    
    
    }
    
    
    void printcolour(struct colour a)
    {
      printf("RGB: %c, %c, %c\n", a.red, a.green, a.blue);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    What is the exact error message? Copy and paste it here; don't type it out.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sprintf(x.red, r);
    Yeah, these aren't going to work.

    Code:
    struct colour create_colour(char r, char b, char g)
    {
      struct colour x;
      x.red = r;
      x.green = g;
      x.blue = b;
      return x;
    }
    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.

  4. #4
    Registered User
    Join Date
    Jun 2020
    Posts
    9
    Thanks for the responses, so i've updated it and now it runs, however its not printing the values I thought I had saved into the red colour struct in the main function, instead it's just printing blanksCould someone please point me in the right direction ~ basic structs and funtions-aaaaaaaaaaaa-png
    updated code:
    Code:
    #include <stdio.h>
    
    
    struct colour{
      char red;
      char green;
      char blue;
    
    
      };
    
    
    struct colour create_colour(char r, char b, char g);
    void printcolour(struct colour a);
    
    
    int main(void)
    {
    
    
    struct colour red = create_colour(255, 0, 0);
    
    
    //struct colour green =create_colour(0, 255, 0);
    //struct colour blue =create_colour(0, 0, 255);
    //struct colour yellow =create_colour(255, 255, 0);
    //struct colour pink =create_colour(255, 0, 127);
    
    
    printcolour(red);
    
    
    return 0;
    }
    
    
    struct colour create_colour(char r, char b, char g)
    {
    
    
      struct colour x;
      x.red = r;
      x.green = g;
      x.blue = b;
    
    
      return x;
    
    
    }
    
    
    void printcolour(struct colour a)
    {
      printf("RGB: %c, %c, %c\n", a.red, a.green, a.blue);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try printing them with %d instead of %c.
    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
    Jun 2020
    Posts
    9
    Now the output displays "RGB: -1, 0, 0" instead of the desired 255, 0, 0

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Whether char is signed or unsigned is implementation defined, and it looks like it is signed in this case, hence 255 is converted to -1. I would change to use unsigned char instead of char since that makes it explicit that you want unsigned char.
    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

  8. #8
    Registered User
    Join Date
    Jun 2020
    Posts
    9
    Wow we won! I read on google somewhere that the char would be unsigned by default but I guess not.. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone point me in the right direction?
    By MadMac in forum C++ Programming
    Replies: 7
    Last Post: 09-19-2005, 03:37 PM
  2. need a point in the right direction
    By sweetly in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2003, 05:19 PM
  3. Point me in the right direction
    By RealityFusion in forum C++ Programming
    Replies: 17
    Last Post: 08-22-2003, 05:05 AM
  4. a point in the right direction
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 03:56 PM
  5. can someone point me in the right direction?
    By bean583 in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 10:35 PM

Tags for this Thread