string arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slacker7272
    New Member
    • Mar 2007
    • 3

    #1

    string arrays

    hey, i have only been working with c for a very short time and i have a queston that seems to be poorly covered in all areas i have looked.

    i am attempting to create an array of strings so that 9 strings start out as "----------" then are replaced by user input with a for loop. Knowing that strings are just character arrays, i initially tried just creating a 2d character array but it became quickly apparent that this was not the best way to go about it. after some research, i have been trying to use pointers, but the code is still not working properly. relevant code is as follows:

    Code:
    char *batter[9] = {
    	"----------",
    	"----------",
    	"----------",
    	"----------",
    	"----------",
    	"----------",
    	"----------",
    	"----------",
    	"----------"
    };
    int main(int argc,char *argv[])
    {
         for(l = 0; l < 9; l++) {
              printf("\n\nWhat is the name of batter %d?",l+1);
              gets(batter[l]);
              clr();
              for(k = 0; k < 9; k++) {
    	printf("%s \n",batter[k]);
              }
         }
         return 0;
    }
    unfortunetly, this every time the user enters a string, it changes all strings to that value instead of only the intended one. a steer in the correct direction would be greatly appreaciated.
    Thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Instead of using nested for...loops, you should use 2 separate loops - the first should fill the array (ending after the gets call), and the second should display (starting after the clr() call).

    Comment

    • slacker7272
      New Member
      • Mar 2007
      • 3

      #3
      that's not really what i'm trying to do, if possible i'd like to print them all after every entry.

      regardless, i tried it anyway and it still just prints them all as the last entry. and other suggestions on how to make it work either way would be great.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        in this definition
        Code:
        char *batter[9] = {
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------"
        };
        the strings "----------" are constants and attempting to change them will give a segmentation error.
        Allocate a 2D array initialised with the strings, e.g.
        Code:
        char batter[9][12] = {
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------",
        	"----------"
        };
        int main(int argc,char *argv[])
        {
             int l,k;                             // added
             for(l = 0; l < 9; l++) {
                  printf("\n\nWhat is the name of batter %d?",l+1);
                  gets(batter[l]);
                  //clr();
                  for(k = 0; k < 9; k++) {
        	printf("%s \n",batter[k]);
                  }
             }
             return 0;
        }
        you should check that the string read by fgets() does not exceed the storage in the array

        Comment

        • slacker7272
          New Member
          • Mar 2007
          • 3

          #5
          thank you, that did it, appreaciate the all the help

          Comment

          Working...