Thread: strings and loops...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    strings and loops...

    Hi, I'm writing a program with 8 strings (from a to h) and I want to be able to edit elements, copy elements and delete elements from one string to another what not. but the only way I can think of for doing it is to have loads of loops which makes the program very ineloquent and messy.

    it's a very basic chess program. the user says they want to move (for example) from g1 to f1. I have a loop that goes something like

    (wm is the white move [a string with two elements, a letter and number], mp is the moving piece, a, b, ... g, h are strings of 12 elements each)

    Code:
    if (wm[0]=='a')
    {
    			
    	mp=a[wm[1]-48];
    	a[wm[1]-48]=' ';
    }
    I have one of those for a, b, c, d,e, f, g and h, but I want a loop which will do it for me. this equates the moving piece (mp) to the element number you specify in wm[1] in a. as you can see with there being 8 strings, there are a lot of (8 of) these loops. is there a way to do a for loop for string names? If I do it with a letter, it won't work cause the letter is only a sincge character, not a string.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    PS I hope I'm being clear!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why aren't you using a two dimensional array for your chess board instead of a string?


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

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    to be honest, they kinda confuse me. is it easier to do that with an array?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could just do:
    Code:
    char board[8][8];
    Then if the first dimension is your a-h and the second is 0-7 you could just do:
    Code:
    board[wm[0] - 'a'][wm[1] - '0'] = ' ';
    P.S. -'0' is clearer than -48 to most people.
    If you understand what you're doing, you're not learning anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char onedee[ SIZE ];
    Read it from right to left.
    An array SIZE elements big, called onedee of type char. You can access each element of the array by doing:
    Code:
    x = onedee[ SOMENUMBER ]; /* Get the value in slot SOMENUMBER and put it in x. */
    onedee[ SOMENUMBER ] = x; /* Put the value of x in slot SOMENUMBER. */
    The only "hard" thing to rememeber, is that you start counting from zero up trough SOMESIZE-1. Thus, if you have an array of 8 items, you have valid elements:
    Code:
    onedee[ 0 ]
    onedee[ 1 ]
    ...
    onedee[ 7 ]  /* if SOMESIZE is 8, you get up to 8-1 to access... */
    Two dimensional arrays are done exactly the same way, but you have a "row" element also.
    Code:
    char twodee[ ROW ][ COLUMN ];
    Think of it as a grid, such as graph paper, or the pixels of your monitor. Again, you get 0 through SIZE-1 for both accessors. So, if we had an 8 by 8 grid, we would get:
    Code:
    twodee[ 0 ][ 0 ]
    twodee[ 0 ][ 1 ]
    ...
    twodee[ 0 ][ 7 ]  /* all the elements in the first row... */
    
    twodee[ 1 ][ 0 ]
    twodee[ 1 ][ 1 ]
    ...
    twodee[ 1 ][ 7 ] /* all the elements in the second row... */
    ...
    ...
    twodee[ 7 ][ 7 ] /* up through the very last item in the last row... */

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings and loops
    By steelydan07 in forum C++ Programming
    Replies: 25
    Last Post: 09-18-2006, 12:38 AM
  2. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Problems with strings and for loops
    By petedee in forum C Programming
    Replies: 52
    Last Post: 04-02-2004, 03:53 AM
  5. help with strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 02:15 AM