Thread: my code is not working

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Post my code is not working

    Hi, I m new in the forum and not very good in C programming.
    I found a code in the internet and tried to modify it, I m trying to compile it but I always have an error message.
    can you please have a look and tell me what is wrong in my code or any idea.
    and also can any one tell me where I can find an IDE better than MPLAB, I m looking for an IDE able to tell me where the error is (at least syntax error or other basic errors) and not just refuse to compile because there is an error somewhere.

    here is my code:
    Code:
    #include <stdio.h>   //standard inpout output header
    #include <string.h>   // add the sthing.h header to recognise strcmp (string compare) and strcopy (string copy)
    
    /************************************************************************
      local definitions
    *************************************************************************/
    
    /* colour indexes */
    #define RBOW_RED               1
    #define RBOW_ORANGE            2
    #define RBOW_YELLOW            3
    #define RBOW_GREEN             4
    #define RBOW_BLUE              5
    #define RBOW_INDIGO            6
    #define RBOW_VIOLET            7
    
    /* colour names */
    #define RBOW_RED_STR           "red"
    #define RBOW_ORANGE_STR        "orange"
    #define RBOW_YELLOW_STR        "yellow"
    #define RBOW_GREEN_STR         "green"
    #define RBOW_BLUE_STR          "blue"
    #define RBOW_INDIGO_STR        "indigo"
    #define RBOW_VIOLET_STR        "violet"
    
    
     
    /************************************************************************
      type definitions
    *************************************************************************/
    
    typedef struct _RAINBOW
    {
      int  num_cols;           // ??????? data type definition
    
      int  cols[7];        //should be 6 not 7, because we have 7 colours going from 0 to 6 in the array
      char *cols_str[7];    // 6 no 7
    
    } RAINBOW;
    
    
    /************************************************************************
      local variables
    *************************************************************************/
    
    static RAINBOW rbow;  
    /************************************************************************
    
      Name: init
    
      Purpose:
    
        To initialise the rainbow
    
    
    *************************************************************************/
    
    int init()
    {
      char *cols_str[];    
      static char fn[] = "rainbow_init";  //  fn not specified should be cols
      cols_str = fn;
      cols_str[0] = RBOW_RED_STR;       //those represent index
      cols_str[1] = RBOW_ORANGE_STR      /* semicolon missing  */
      cols_str[2] = RBOW_YELLOW_STR; 
      cols_str[3] = RBOW_GREEN_STR; 
      cols_str[4] = RBOW_BLUE_STR; 
      cols_str[5] = RBOW_INDIGO_STR; 
      cols_str[6] = RBOW_VIOLET_STR; 
    
      num_cols = 7;               //defined previously as an integer
    
      return (0);
    
    } /* init */
    
    
    
     
    /************************************************************************
    
      Name: get_rainbow_colour_str
    
      Purpose:
    
        To determine the name associated with a rainbow colour.
    *************************************************************************/
    
    char *get_rainbow_colour_str(int colour)
    {
      static char fn[] = "init";
    
      char col_str[5];        
      int  col_idx;
      
      strcpy(col_str, rbow.col_str[colour]);   
    
      return (&col_str);
    
    } /* get_rainbow_colour_str */
    
    /************************************************************************
      Name: get_rainbow_colour
    
      Purpose:
    
        To determine the name associated with a rainbow colour.
    *************************************************************************/
    
    int get_rainbow_colour(char *colour_str)
    {
      static char fn[] = "get_rainbow_colour";
    
      int col_idx;  
    
      for (col_idx = RBOW_RED; col_idx <= RBOW_VIOLET; col_idx++)
      {
        if (!strcmp(rbow.cols_str[col_idx], colour_str)) //see pge80, 
        {
          return (col_idx);
        }
      }
    
    } /* get_rainbow_colour */
      
    
    /************************************************************************
      Name: test_rainbow_colour
    
      Purpose:
    
        To test the application  interfaces to rainbow colour.
    *************************************************************************/
    
    int test_rainbow_colour()
    {
      static char fn[] = "test_rainbow_colour";
    
      get_rainbow_colour_str(0);
      get_rainbow_colour_str(RBOW_RED);
      get_rainbow_colour_str(RBOW_VIOLET);
    
      get_rainbow_colour("black");
      get_rainbow_colour(RBOW_RED_STR);
      get_rainbow_colour(RBOW_VIOLET_STR);
    
    } /* get_rainbow_colour */
    many thanx

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't mention your operating system, but if it's Windows XP or Vista, I'd recommend the free version of Microsoft Visual C express edition.

    What are you trying to do with this program, exactly?

    I'll add something of my own in a minute, if I can find it.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, that code is full of mess. I will have to ask you the same question on what is you trying to do? If you’re new to programming C. Then I would strongly suggest you get a good C book and start going through it.

    And if you’re looking for a good editor, I would suggest you go for Dev-C++ or the Code Block. And again they work on Windows XP but they use MIGWIN which ports to GCC compiler.

    MPLAB are often used in programming embedded systems. I am using it for from PIC microcontroller programming. It is very good IDE for some specialised programming. But certainly not good for beginners.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My color code is DOS specific, so it throws a fit in Windows.

    This code has no visible purpose - not even a main() function. It uses ugly long strings in the code, only to have the preprocessor replace them, with short, simple one's. WTH?

    It doesn't show anything on the monitor, or write anything to a file, either. No point to it, yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM