Thread: Inputting strings to an array to store all string inputs- not just last two inputs..

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    55

    Inputting strings to an array to store all string inputs- not just last two inputs..

    I need help with inputting strings to an array so all strings can be saved and printed to screen. All I can manage is the last 2 inputted strings apparently being saved in the array and printing out! I have tried several fixes but with no success so far...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
     
      char movep1[8];
      char movep2[8];
      //char move_input[20];
      char onemovehistory[300];
      //int i;
      char compmovehistory[300];
      int  moveno=0;
     //start of player enter moves loop
    for (moveno=0; moveno<200; moveno++){
        printf("Player1 Please enter a move eg e2e4: %d.",moveno+1);
        scanf("%s", movep1);
        
         /* Some stuff I've tried to get compmovearray to store all moves!
          * fgets(move_input, 20, stdin);
          *move_input[strlen(move_input) -1] = '\0';
          *printf("The user has entered '%s'\n", move_input);
          *
          *printf("\n");
          *Print array movep1[]; with for loop
          * printf("%s",movep1):
          * for(i=0;movep1 !='\0';i++){
          * printf("%c",movep1[i]);
          * } 
          */
         
         printf("You entered move%d.%s\n",moveno+1,movep1);
             
        //printf("Here is your move:%d.%s\n",moveno+1,movep1);
        
        strcpy(onemovehistory,movep1);
        
        printf("Player2 Please enter a move: %d...",moveno+1);
        scanf("%s", movep2);
        
        printf("Here is your move: %d...%s\n",moveno+1,movep2);
      
        strcat(onemovehistory,movep2);//need to store this //onemovehistory array somewhere somehow!
        
            int i;  //trying to copy array
            for( i=0; i < 300; ++i){
        compmovehistory[i] = onemovehistory[i];
    }
         //strcpy(compmovehistory,onemovehistory);trying to store into new array compmovehistory but keeps being overwritten
         //somehow access all elements and save all elements
        printf("\nMove history list %s\n\n",compmovehistory);
             
    }//end player enters move loop
      
    return 0;
    }
    I am most grateful for helpful replies.Many thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    If this is a single move.
    char movep1[8];

    Then a history needs to be
    char onemovehistory[300][8];

    Then saving it is
    strcpy(onemovehistory[moveno],movep1);
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Salem-Thanks for your helpful reply. This is nearly working for me but I get a warning that I do not know how to fix!
    (I changed array onemovehistory name to movehistory).Here is the console output which is what I wanted:

    Player1 Please enter a move eg e2e4: 1.e2e4
    You entered move1.e2e4
    Player2 Please enter a move: 1...e7e5
    Here is your move: 1...e7e5

    Move history list e2e4e7e5

    Player1 Please enter a move eg e2e4: 2.Ng1f3
    You entered move2.Ng1f3
    Player2 Please enter a move: 2...Nb8c6
    Here is your move: 2...Nb8c6

    Move history list e2e4e7e5Ng1f3Nb8c6

    Player1 Please enter a move eg e2e4: 3.d2d4
    You entered move3.d2d4
    Player2 Please enter a move: 3...exd4
    Here is your move: 3...exd4

    Move history list e2e4e7e5Ng1f3Nb8d2d4exd4

    Player1 Please enter a move eg e2e4: 4.

    Here is the printf statement (on line84) that gives this warning
    printf("\nMove history list %s\n\n",movehistory);
    and here is the actual compiler warning:
    ___
    gcc -Wall -o "rpdinputarray2" "rpdinputarray2.c" (in directory: )
    rpdinputarray2.c: In function main :
    rpdinputarray2.c:84:5: warning: format %s expects argument of type char * , but argument 2 has type char (*)[8] [-Wformat=]
    printf("\nMove history list %s\n\n",movehistory);
    ^
    rpdinputarray2.c:84:5: warning: format %s expects argument of type char * , but argument 2 has type char (*)[8] [-Wformat=]
    Compilation finished successfully.
    ____
    I have tried to fix this with using printf("\nMove history list %s\n\n",movehistory[moveno]); which compiles successfully with no warnings but does not save the movehistory array in full. I also tried: printf("\nMove history list %s\n\n",movehistory[300][8]); when it compiles and runs but this does not give any output after string Move history list prints.

    What do I need to do to fix this warning:
    warning: format %s expects argument of type char * , but argument 2 has type char (*)[8] [-Wformat=] ?

    Again I am very grateful for any help,thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You shouldn't expect to print an entire array with one print statement. You should expect to print an array with a print statement inside a loop.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Hi tabstop Thanks for your reply.I will look into the point I think you are making...meanwhile I am very frustrated by trying to get this to work and thought I would have found some examples of how to save/store (& even print) string arrays in C to guide me with this. I have deleted my source file and will start over now.....

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    I still haven't fixed this yet! I have tried using 1D array move[] to take input move and save it to 2D array movehistory [][] but I can't seem to get that right!
    Otherwise if I take 2 moves from P1/P2 I can join the moves with strcat but how do I save strcat content to a new array movehistory and how would I make that usable to save repeatedly inputted moves?!
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char movep1[8];
        char movep2[8];
        printf("P1 Enter your move\n");
        scanf ("%s",movep1);
        printf("P2 Enter your move\n");
        scanf ("%s",movep2);
        printf("\nmovehistory %s\n", strcat(movep1, movep2));
        return 0;
    }
    My struggles with C programming continue and I'm grateful for any helpful input-thanks

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    I still haven't fixed this. I will keep trying (& this is taking me weeks to fix!). I cannot get an array to store all entered user input. I only get the last entered user input.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char move[8];
        char movehistory[200][8];
        int moveno=1;
        for (moveno=1;moveno<=200;moveno++){
        
            printf("Player1 enter your move:");
            scanf("%s",move);
            strcpy(movehistory[8],move);
            printf("Move history %s\n",movehistory[8]);
                
            printf("Player2 enter your move:");
            scanf("%s",move);
            strcat(movehistory[8],move);
            printf("Move history %s\n",movehistory[8]);
        }
        return 0;
    }
    Outputs:
    Player1 enter your move:e2e4
    Move history e2e4
    Player2 enter your move:e7e5
    Move history e2e4e7e5
    Player1 enter your move:Ng1f3
    Move history Ng1f3
    Player2 enter your move:
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    I want:Move history e2e4e7e5 Ng1f3 etc

    I am grateful for any helpful replies,thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Quote Originally Posted by richardpd
    I want:Move history e2e4e7e5 Ng1f3 etc
    In order to account for the null character, this means that your arrays of char should have 9, not 8, elements.

    Next, this is wrong:
    Code:
    strcpy(movehistory[8],move);
    You probably wanted:
    Code:
    strcpy(movehistory[moveno - 1], move);
    Likewise for the other cases where you wrote movehistory[8].

    You can avoid having to call strcpy and strcat if you read directly into the final destination array. Oh, and instead of using %s for scanf, use %4s since you only want to read 4 characters, e.g., e2e4.
    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

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Hi laserlight,
    Many thanks for your prompt reply.

    However using your changes I get essentially the same output I already had? e.g.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char move[8];
        char movehistory[200][8];
        int moveno=1;
        for (moveno=1;moveno<=200;moveno++){
        
            printf("Player1 enter your move:");
            scanf("%5s",move);
            //strcpy(movehistory[8],move);
            strcpy(movehistory[moveno - 1], move);
            printf("Move history %s\n",movehistory[moveno - 1]);
                
            printf("Player2 enter your move:");
            scanf("%5s",move);
            strcat(movehistory[moveno - 1], move);
            printf("Move history %s\n",movehistory[moveno - 1]);
        }
        return 0;
    }
    Outputs:
    Player1 enter your move:e2e4
    Move history e2e4
    Player2 enter your move:e7e5
    Move history e2e4e7e5
    Player1 enter your move:Ng1f3
    Move history Ng1f3
    Player2 enter your move:
    <<<<<<<<<<<<<<<<<<<<<<<<<<<

    The other option you mention of not using strcpy/strcat is certainly a possibility but I'm sure I will find that hard to code too! Not sure I understand how to 'read directly into destination array'.

    Also in regard to move array for each move input I need only 4-6 char input which is why I declared move[8] (so this should allow for null terminator char) & I think %5s should be fine in this case?

    If you can help me understand & fix this I'd be very grateful-best wishes

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Quote Originally Posted by richardpd
    Ng1f3
    Actually, I did not notice this: if you want this format, then you need 11 characters per complete move: 5 for the first player's move, 5 for the second player's move, and 1 for the null character.

    Quote Originally Posted by richardpd
    However using your changes I get essentially the same output I already had?
    Yes, because you are only printing the string immediately after you read it, thus your mistake is hidden from yourself. Try printing the entire array at one go after reading instead and the difference will become obvious.

    Quote Originally Posted by richardpd
    Also in regard to move array for each move input I need only 4-6 char input which is why I declared move[8](so this should allow for null terminator char) & I think %5s should be fine in this case?
    The array named move only needs 6 characters at most. But each array of char in movehistory must have more than that.

    Quote Originally Posted by richardpd
    I think %5s should be fine in this case?
    Yes.

    Note that if you intend to extract the individual player moves from the move history, having a format with varying number of characters will complicate the parsing.
    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

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    laserlight- Thanks for your further help.
    Unfortunately I do not get this at all! I cannot get it to work.

    All I want is to get string input from stdio (1 or 2 users) with scanf and to save this to a string array that I can then print out to screen with printf that contains ALL entered strings and not just the last entered strings.

    This is actually intended to take in game (chess) moves from users( each player move in chess is actually a ply move e.g. a half move as a full chess move consists of a ply from both players). However it could just as easily be any other example of string array input/output and take any 1D user input string (names/countries/foods etc) and save it to another 2D array which I can then print out.

    The string array that is to hold all inputted strings of course needs to be large and big enough and can be statically or dynamically sized. Here I am statically sizing it.


    I cannot do this as nothing I do seems to be do what I want and I just keep ending up at the same code I had weeks ago! eg

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char move[8];
        char movehistory[200][16];
        int moveno=1;
        for (moveno=1;moveno<=200;moveno++){
            printf("Player1 enter your move:");
            scanf("%5s",move);
            //strcpy(movehistory[8],move);
            printf("Your move %s\n",move);
            strcpy(movehistory[16], move);
            printf("Player2 enter your move:");
            scanf("%5s",move);
            strcat(movehistory[16], move);
            printf("Move history %s\n",movehistory[16]);
        }
        return 0;
    }
    OUTPUT:
    Player1 enter your move:e2e4
    Your move e2e4
    Player2 enter your move:e7e5
    Move history e2e4e7e5
    Player1 enter your move:Ng1f3
    Your move Ng1f3
    Player2 enter your move:Nb8c6
    Move history Ng1f3Nb8c6
    Player1 enter your move:
    <<<<<<<<<<<<<<<<<<<<<<<<

    Please, laserlight or anyone else, help me understand how to get this done?! Most grateful for further help

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    This is wrong:
    Code:
    strcpy(movehistory[16], move);
    It is the exact same issue as I pointed out in post #8. It is wrong because you are reading into exactly one array, i.e., movehistory[16]. For all 200 iterations of the loop, you read into the exact same array. Therefore, when the loop is done, all you have saved is the very last thing read, i.e., into movehistory[16]. Everything else was overwritten since each time you read into movehistory[16], you erase what was previously stored in movehistory[16].

    Hence, when I corrected you, I wrote:
    Code:
    strcpy(movehistory[moveno - 1], move);
    Now, for each iteration of the loop, you read into a different array, i.e., movehistory[moveno - 1].

    Next, a word on asking for help: don't just provide the output that you get, i.e., the actual output. Provide the expected output too. You say that you "cannot get it to work". What does "get it to work" mean to you? You say that you want to "print out to screen with printf that contains ALL entered strings and not just the last entered strings". How do you know that you are only printing out the "last entered strings" when the output that you give seems to print out each string as you read it?
    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

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Well, the heart of the matter is that I don't fully understand C and am still learning C. I know something about C including some knowledge of arrays and pointers but I really don't fully understand how to access and manipulate and use array data and arrays and pointers fully and correctly. I have spent a lot of time trying to learn C from internet resources but have not yet seen an example of C code that shows me how to do what I want to do with this char array data manipulation.

    I understand I should not save the string input to the same array memory location every time I want to get input, otherwise I will just overwrite the values at the same location.

    I want to get string input as a 1D char array from the user with scanf and put it into a 2D char array at different locations and then output the 2D array to screen with printf using nested for loops as necessary eg (my attempt at pseudocode):

    char move[8];
    char movehistory[200][8];

    move[8]="e2e4"; = movehistory[0][8];
    move[8]="e7e5"; = movehistory[1][8];
    move[8]="Ng1f3"; = movehistory[2][8];
    So that (in effect):
    movehistory[0][8]="e2e4";
    movehistory[1][8]="e7e5";
    movehistory[2][8]="Ng1f3";
    etc
    Can I do this (or anything equivalent) and if so how?

    I feel sure I cannot do
    movearray []; = movehistoryarray[][];
    which is why I am trying to use strcpy & strcat
    (but perhaps I can do
    movehistoryarray[]=strcp(movehistoryarray,move);
    or something similar?Maybe not).

    Well I know I am not explaining this in the best way but that is because I am a beginner in C and don't understand C fully and how to do what I want with C-if I did I wouldn't be posting for help! I am just hoping experienced C programmers can remember what it was like when they didn't know or understand how C coding works.So I also hope experienced C programmers can appreciate what it is like to be a beginner learning C and are patient and helpful in helping C students like me understand points they find difficult. Is it too hard for an experienced C programmer to see what I am trying to do here and provide code and guidance that helps me with this? I have only been trying to figure this out for weeks!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char movep1[8];
        char movep2[8];
        char movehistory[200][16];
        int moveno=0;
        for (moveno=0;moveno<=200;moveno++){
            printf("Player1 enter your move:");
            scanf("%5s",movep1);
            printf("Your move %s\n",movep1);
            strcpy(movehistory[moveno+1],movep1);
            printf("Player2 enter your move:");
            scanf("%5s",movep2);
            printf("Your move %s\n",movep2);
            strcat(movehistory[moveno+1],movep2);
            printf("Move history %s\n",movehistory[moveno+1]);
        }
        return 0;
    }
    Output:

    Player1 enter your move:e2e4
    Your move e2e4
    Player2 enter your move:e7e5
    Your move e7e5
    Move history e2e4e7e5
    Player1 enter your move:Ng1f3
    Your move Ng1f3
    Player2 enter your move:Nb8c6
    Your move Nb8c6
    Move history Ng1f3Nb8c6
    Player1 enter your move:
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    The output I want:

    Player1 enter your move:e2e4
    Your move e2e4
    Player2 enter your move:e7e5
    Your move e7e5
    Move history e2e4e7e5
    Player1 enter your move:Ng1f3
    Your move Ng1f3
    Player2 enter your move:Nb8c6
    Your move Nb8c6
    Move history e2e4e7e5Ng1f3Nb8c6
    Player1 enter your move:

    Thanks for your helpful reply laserlight. As you can see I still seem to overwrite the movehistory array. I'm very grateful for more help to make me see the (laser) light and get the movehistory array to output as I want / need it to! :-)

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't print an arbitrary number of things in a print statement. If you want to print every item in an array, you will have to use a loop to print, la
    Code:
    for (int i = 0; i < moveno; i++) {
        printf("%s", moveno[i]);
    }

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    tabstop thank you for your reply. I think this is essentially the same advice you gave me in your first reply 3 weeks ago.
    However when I try the loop code you give now I get a compile error:
    In function main :
    error: subscripted value is neither array nor pointer nor vector
    printf("%s", moveno[i]);
    ^
    Compilation failed.

    I do not understand your code, as afaik, I have not created and used an array moveno[];?
    I do have an array movehistory[moveno+1] and if you think I should loop through this how would I do that? Can you explain anymore and possibly give a complete working code example using either my code or something similar? It would be great if you could help me solve this,thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable string inputs.
    By .RK in forum C Programming
    Replies: 7
    Last Post: 08-18-2012, 05:33 AM
  2. strings inputs
    By aitzaz_cheema in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2010, 08:26 AM
  3. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  4. stoping inputs into an array
    By thestrapman in forum C Programming
    Replies: 3
    Last Post: 08-19-2008, 04:41 PM
  5. String inputs, 1 or 2?
    By SkyRaign in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2005, 07:04 PM