Help with arrays

This is a discussion on Help with arrays within the C Programming forums, part of the General Programming Boards category; I'm working on a C Programming assignment, and its basically a hare and tortoise racing game. Here's what I have ...

  1. #1
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23

    Help with arrays

    I'm working on a C Programming assignment, and its basically a hare and tortoise racing game.

    Here's what I have so far: User puts in a string of commands for the tortoise and the hare. Ex: HhHHhHHpPD That part works, and I am using switch statements to process the commands. The problem is, how do I take the commands from the string, process them, and display the result in a 2x30 array using loop.

    Teacher gave us a sample output:

    __________T____________
    ________H______________

    The 2 dimensional array is to be printed out for each iteration of the loop. Any input would be greatly appreciated (newbie coder)...

    BTW yes, I already know about the segmentation fault in the code... Working on that too

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define STRSIZ 30
    
    int
    main(void)
    {
    
       char hareStr[STRSIZ]="";
       char tortStr[STRSIZ]="";
       char myStr[STRSIZ][2]={{'_'},{'_'}};
    
       char hareChoice, tortChoice;
    
       int i = 0, j = 0, c, x = 0, harePace = 0, tortPace = 0;
    
       printf("Enter Hare Commands>");
    
       while ( (c = getchar() ) != '\n')
          hareStr[i++] = c;
          hareStr[i] = '\0';
    
       printf("Enter Tortoise Commands>");
    
       while ( (c = getchar() ) != '\n')
          tortStr[j++] = c;
          tortStr[j] = '\0';
    
      printf("BANG! AND THEY ARE OFF!!!\n");
    
       while( ( strlen(hareStr) != EOF ) || ( strlen(tortStr) != EOF ) || harePace <= 30 || tortPace <=30 )
       {
    
       hareChoice = hareStr[x];
       tortChoice = tortStr[x];
    
       /* Hare Command Switch */
    
       switch (hareChoice)
       {
    
          case 'h':
             harePace = harePace + 1;
             break;
          case 'H':
             harePace = harePace + 9;
             break;
          case 's':
             harePace = harePace - 2;
             break;
          case 'S':
             harePace = harePace - 12;
             break;
          case 'D':
             harePace =  harePace;
             break;
          default:
             printf("Error: Invalid Command!\n");
             harePace = EOF;
             break;
       }
    
       /* Tortoise Command Switch */
    
       switch (tortChoice)
       {
    
          case 'P':
             tortPace =  tortPace + 2;
             break;
          case 'p':
             tortPace = tortPace + 1;
             break;
          case 'S':
             tortPace = tortPace - 6;
             break;
          default:
             printf("Error: Invalid Command!\n");
             tortPace = EOF;
             break;
       }
    
       if ( harePace < tortPace)
               printf("Hare wins, Yuch!\n");
       else if ( harePace > tortPace)
               printf("Tortoise wins! Yay!\n");
       else if ( harePace == tortPace)
               printf("Hey wow! Its a TIE!!\n");
    
       x++;
    
    
       }
    
       return(0);
    
    
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Just a few quick things...

    Don't the two strings have to be the same length?



    strlen(hareStr) != EOF

    EOF is defined as -1. the length of the string will not change (and can't be less than zero) so this will not work.
    Try something like

    (hareStr[iCurrent]!='\0') ect with \n \r

    or

    (x<strlen(hareStr))//count is less than length as count is zero based

    harePace = harePace + 1;

    or

    harePace += 1;


    To draw the array you will need to;

    Code:
    Clear the screen or create an area to draw on
    Find the hare and torts place (Place)
    Draw a number of spaces (<Place)
    Draw the T or H
    Finish the spaces Place+1 -> MAX
    Good Luck!
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF is defined as -1.
    No it's not.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23
    According to the teacher, they do not have to be the same length.

    In fact, if one of the commands is invalid, it is supposed to jump out of the loop, and exit the program.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>According to the teacher, they do not have to be the same length.


    test each length. use the minimum as the count (length). Can use the macro min() from math.h or write your own.




    >>EOF is defined as -1.
    >>No it's not.

    So what is it defined as?



    From stdio.h in my MSVC.NET 2003 SDK complier, line 121.

    #define EOF (-1)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23
    Thx for everyone's help, between y'all and my teacher, the program works, now, the only thing I have to do now is just split the program into separate functions. ( I know, shoulda done that first, oh well)...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by novacain
    >>EOF is defined as -1.
    >>No it's not.

    So what is it defined as?

    From stdio.h in my MSVC.NET 2003 SDK complier, line 121.

    #define EOF (-1)
    .. and from the ISO standard:
    [EOF]... expands to an integer constant expression, with type int and a negative value, that
    is returned by several functions to indicate end-of-file, that is, no more input from a
    stream;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    .. and from the ISO standard:
    Which makes no specification other than it must be negative. It doesn't say "EOF must be -1". Which means, if you're testing for -1, my compiler that I write for fun, which defines EOF as -54321, which is fully ANSI compliant, will make your test invalid.

    Which was my entire point. It is not defined as -1 by the standard. It is defined as negative.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23
    Ok one last problem, here is the current version of the program. I am having problems with the hareFunc and tortFunc functions. If I put a return(0); at the end of each function, (i.e. getting rid of "control reaches non void function" error) then it defaults the return value to 0 towards the end of the looping.

    Any ideas???


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define STRSIZ 30
    
    int hareFunc( char hareCmd, int harePace );
    int tortFunc( char tortCmd, int tortPace );
    void raceCheck( int harePace, int tortPace );
    
    
    int
    main(void)
    {
    
       char hareCmd, tortCmd;
       char hareStr[STRSIZ]="";
       char tortStr[STRSIZ]="";
       char raceStr[2][STRSIZ + 1]={{'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'}, {'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'}};
    
       int i = 0, j = 0, c, x = 0, harePace = 0, tortPace = 0;
       int m, n;
    
       printf("Enter Hare Commands>");
    
       while ( (c = getchar() ) != '\n')
          hareStr[i++] = c;
          hareStr[i] = '\0';
    
       printf("Enter Tortoise Commands>");
    
       while ( (c = getchar() ) != '\n')
          tortStr[j++] = c;
          tortStr[j] = '\0';
    
       printf("BANG! AND THEY ARE OFF!\n");
    
       while( hareStr[x] != '\0' || tortStr[x] != '\0' )
       {
    
       hareCmd = hareStr[x];
       tortCmd = tortStr[x];
    
       raceStr[0][harePace] = '_';
       harePace = hareFunc(hareCmd, harePace);
    
       if ( harePace != EOF)
       {
          if ( harePace > 30)
             harePace = 30;
          else if ( harePace < 0)
             harePace = 0;
       }
       raceStr[0][harePace] = 'H';
    
    
    
       raceStr[1][tortPace] = '_';
       tortPace = tortFunc(tortCmd, tortPace);
       if ( tortPace != EOF)
       {
          if ( tortPace > 30)
             tortPace = 30;
          else if ( tortPace < 0)
             tortPace = 0;
       }
       raceStr[1][tortPace] = 'T';
    
    
       for ( m = 0; m < 2; m++)
       {
          for ( n = 0; n < 31; n++)
          {
             printf("%c", raceStr[m][n]);
          }
          printf("\n");
       }
    
       printf("\n");
    
       if (harePace == tortPace)
          printf("OUCH!!!\n");
    
       if (harePace == 30)
          break;
       else if (tortPace == 30)
          break;
    
       x++;
       }
       raceCheck( harePace, tortPace);
    
       return(0);
    }
    
    int hareFunc( char hareCmd, int harePace )
    {
    
       if (hareCmd != '\0')
          switch (hareCmd)
          {
          case 'h':
             return(harePace = harePace + 1);
                break;
          case 'H':
             return(harePace = harePace + 9);
                break;
          case 's':
             return(harePace = harePace - 2);
                break;
          case 'S':
             return(harePace = harePace - 12);
                break;
          case 'D':
             return(harePace);
                break;
          default:
             printf("Error: Invalid Command!\n");
             return(harePace = -1);
                break;
          }
    
    
    
    }
    
    int tortFunc( char tortCmd, int tortPace )
    {
    
       if (tortCmd != '\0')
          switch (tortCmd)
          {
          case 'P':
             return(tortPace = tortPace + 3);
                break;
          case 'p':
             return(tortPace = tortPace + 1);
                break;
          case 'S':
             return(tortPace = tortPace - 6);
                break;
          default:
             printf("Error: Invalid Command!\n");
             return(tortPace = -1);
                break;
         }
    }
    
    
    void raceCheck( int harePace, int tortPace)
    {
    
       if ( (harePace == 30) || (tortPace == 30))
       {
    
          if ( harePace > tortPace)
             printf("Hare wins, Yuch!\n");
          else if ( harePace < tortPace )
             printf("Tortoise wins!!! Yay!\n");
          else if ( harePace == tortPace )
             printf("Oh my! Its a TIE!!!\n");
       }
    }

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>if ( harePace != EOF)
    Use -1 as that is what you have returned. That is don't mix the defines with values. Use all defines or all values. Just in case the define changes.

    >>hareCmd = hareStr[x];
    tortCmd = tortStr[x];

    watch this, what happens if one string is shorter than the other?
    You will be reading past the '\0' terminator you placed. this will contain random values. I think you will have to use seperate indexes or make sure both arrays are the same length.

    or

    Try filling the smaller one with '\0' to until both are the same lengtb as soon as you fill in the arrays.



    in the
    hareFunc() you need a return value in case the switch does not get called. I would remove this line as the case then will take care of any input.
    One string could be shorter than the other and so be '\0'

    if (hareCmd != '\0')

    >>while( hareStr[x] != '\0' || tortStr[x] != '\0' )

    At this point i and j contain the length of the strings. They could be useful here. Say ( x < max( i , j ) )




    >>Which makes no specification other than it must be negative.

    quzah,
    The whole reason I pointed out EOF was negative (-1) was to show that this test would not work;


    strlen(hareStr) != EOF


    You just posted to be picky and to not be helpful.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23
    Guys guys guys!!!! Didnt want to start a flame war, just a newbie programmer seeking help from fellow coders... Good news though, got the program done, works perfectly. Thanks to all who helped...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21