Thread: Retrieving Multiple inputs with fgets?

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fgets(*userInput, 160, stdin);
    You have a splendid nack of trying every single possible combination except the right ones told to you by everyone else.

    Are you a troll?
    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.

  2. #17
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    You have a splendid nack of trying every single possible combination except the right ones told to you by everyone else.
    My original question is yet to be answered. I still can't fill a 2-d array with fgets :| well, i can sort of but i want to manipluate the columns as it's filling. I tried using a pointer that worked, but the array wasn't being filled properly, tried the actual index number of the element i.e. array[0][0] that didn't work. What confuses me is that array[line] works but array array[line][line2] does not.

    Really, i don't mean to be of any annoyance and mostly all the stuff i use comes from books or online.
    Last edited by Axel; 09-12-2005 at 03:49 PM.

  3. #18
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    seems as though userInput[line1][line2] works after all. i'm going to try filling the array using the line2(column) and discard the spaces at the end. I'll post back with the outcome soon.

  4. #19
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    .. ok maybe not:
    passing arg 1 of `fgets' makes pointer from integer without a cast
    userInput[line1][line2]

    both userInput[line]
    userInput[line2] work perfectly but i don't want the row :| i want a reference to the column

    any ideas?

  5. #20
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    userInput[0][0] = 'H';
    userInput[0][1] = 'e';
    userInput[0][2] = 'l';
    userInput[0][3] = 'l';
    userInput[0][4] = 'o';
    userInput[0][5] = ' '; // The ony problem i see is the newline that will get included using fgets
    to solve above problem, do this

    Code:
    char *n;
    n = strchr( userInput[0], '\n' );
    *n = '\0';
    and also why woud you print the characters one by one when you can print is as a whole

    Code:
      printf("%c", userInput[line1][line2]); // WTF!!  :eek:
    
       
    
    just do:
    printf( "%s", userInput[line1] );
    
    Code:
        for (row = 0 ; line < 6; row++)
            fgets(userInput[row][col], 160, stdin);   // this is wrong
    should be fgets(userInput[row], sizeof( userInput[row] ), stdin);
    I suggest that you go and read the replies to this thread before posting some more questions. Your too excited
    Last edited by loko; 09-12-2005 at 05:05 PM. Reason: respelled strchar to strchr

  6. #21
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Compile, run and study this and see if it helps you:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char userInput[2][20];
      int i, j;
      
      for (i = 0; i < 2; i++)
      {
        sprintf (userInput[i], "This is line %d", i);
      }
      
      printf ("Printing complete array:\n");
      
      for (i = 0; i < 2; i++)
      {
        for (j = 0; j < 20; j++)
        {
          printf ("userInput[%d][%2d] = '%c'\n",  
                  i, j, isprint(userInput[i][j]) ? userInput[i][j] : '.' );
        }
      }
      
      printf ("Printing only populated area of array:\n");
      
      for (i = 0; i < 2; i++)
      {
        for (j = 0; j < 20 && userInput[i][j] != '\0'; j++)
        {
          printf ("userInput[%d][%2d] = '%c'\n",  
                  i, j, isprint(userInput[i][j]) ? userInput[i][j] : '.' );
        }
      }
      
      printf ("Printing as strings:\n");
      
      for (i = 0; i < 2; i++)
      {
        puts (userInput[i]);
      }
      
      return(0);
    }
    
    
    /*
     Output
     
    Printing complete array:
    userInput[0][ 0] = 'T'
    userInput[0][ 1] = 'h'
    userInput[0][ 2] = 'i'
    userInput[0][ 3] = 's'
    userInput[0][ 4] = ' '
    userInput[0][ 5] = 'i'
    userInput[0][ 6] = 's'
    userInput[0][ 7] = ' '
    userInput[0][ 8] = 'l'
    userInput[0][ 9] = 'i'
    userInput[0][10] = 'n'
    userInput[0][11] = 'e'
    userInput[0][12] = ' '
    userInput[0][13] = '0'
    userInput[0][14] = '.'
    userInput[0][15] = '.'
    userInput[0][16] = '.'
    userInput[0][17] = '.'
    userInput[0][18] = '@'
    userInput[0][19] = '.'
    userInput[1][ 0] = 'T'
    userInput[1][ 1] = 'h'
    userInput[1][ 2] = 'i'
    userInput[1][ 3] = 's'
    userInput[1][ 4] = ' '
    userInput[1][ 5] = 'i'
    userInput[1][ 6] = 's'
    userInput[1][ 7] = ' '
    userInput[1][ 8] = 'l'
    userInput[1][ 9] = 'i'
    userInput[1][10] = 'n'
    userInput[1][11] = 'e'
    userInput[1][12] = ' '
    userInput[1][13] = '1'
    userInput[1][14] = '.'
    userInput[1][15] = '.'
    userInput[1][16] = 'H'
    userInput[1][17] = '.'
    userInput[1][18] = '@'
    userInput[1][19] = '.'
    Printing only populated area of array:
    userInput[0][ 0] = 'T'
    userInput[0][ 1] = 'h'
    userInput[0][ 2] = 'i'
    userInput[0][ 3] = 's'
    userInput[0][ 4] = ' '
    userInput[0][ 5] = 'i'
    userInput[0][ 6] = 's'
    userInput[0][ 7] = ' '
    userInput[0][ 8] = 'l'
    userInput[0][ 9] = 'i'
    userInput[0][10] = 'n'
    userInput[0][11] = 'e'
    userInput[0][12] = ' '
    userInput[0][13] = '0'
    userInput[1][ 0] = 'T'
    userInput[1][ 1] = 'h'
    userInput[1][ 2] = 'i'
    userInput[1][ 3] = 's'
    userInput[1][ 4] = ' '
    userInput[1][ 5] = 'i'
    userInput[1][ 6] = 's'
    userInput[1][ 7] = ' '
    userInput[1][ 8] = 'l'
    userInput[1][ 9] = 'i'
    userInput[1][10] = 'n'
    userInput[1][11] = 'e'
    userInput[1][12] = ' '
    userInput[1][13] = '1'
    Printing as strings:
    This is line 0
    This is line 1
    */
    Some notes:

    - When printing the entire array, the areas after the actual data are filled with unpredicatable data. That's the nature of uninitialised memory.

    - In my second print loop, I said "print populated area". I have neglected to print the \0 terminator for each string, which is technically part of the "populated area". Minor technicallity, didn't want to get picked up on it!

    - I used isprint() to determine if a character is printable before attempting to print it. That way, any unprintable characters will be displayed as a dot instead of garbage.

    - My data is simulated input, what out for the \n that fgets() may add to the end of the userInput string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #22
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by Hammer
    Compile, run and study this and see if it helps you:
    Thanks for that. Although it did clear a few things up i already know the basics of 2-d arrays. The problem is with inputted text.

    In the populated area you have:

    for (j = 0; j < 20 && userInput[i][j] != '\0'; j++)

    that's what i'm stuck on, i can't check if a column has a space. cwr's code only accepts rows as the parameter i can't get it to accept a column reference to discard spaces using a for loop. Where as in your code you're checking.

    But with your code it kind of gives me an idea, maybe i should copy it [the userInput array] into a new array and run a for loop to discard the spaces at the end like what you have done and instead of printing it, i'll just copy it into a new array without the spaces? I'll give that a go and post back. This problem is really annoying.

    also loko, the following:

    Code:
    char *n;
    n = strchr( userInput[0], '\n' );
    *n = '\0';
    doesn't seem to do anything. When printed, the array is exactly the same so i tried tinkering with it:

    Code:
    char *n;
    n = strchr( userInput[row], '\n' );
    *n = '\0';

    gives me the dreaded segmentation fault

    I'm guessing it's because

    Code:
      for (line = 0 ; line < 8; userInput++)
           fgets(userInput[line], 160, stdin);
    
      char *n;
    n = strchr( userInput[0], '\n' );
    *n = '\0';
    somehow in the for loop it stops working

    Also, a question why have you choosen to put a new line in userInput[0]? and not the last column in the row?

  8. #23
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok, copying the userInput array into a 'clean' array with no spaces didn't work. The same thing happens when printing out the array even if you include a for loop saying i don't want '\0'

    Code:
        for (line = 0 ; line < 6; line++)
           fgets(userInput[line], 160, stdin);
    
    
    int i, j;
    
     for (i = 0; i < 6; i++)
      {
        for (j = 0; j < 160 && userInput[i][j] != '\0'; j++)
        {
          printf("\n%s", "Column "); printf("%d ", j); printf("%s", "has -=> ");
          printf ("%c", userInput[i][j]);
        }
      }

    result:


    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>

    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>

    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>

    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>

    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>

    Column 0 has -=> h
    Column 1 has -=> e
    Column 2 has -=> l
    Column 3 has -=> l
    Column 4 has -=> o
    Column 5 has -=>




    ... bit strange don't you think? EVEN after i specifically said in the inner for loop to not include spaces.
    Last edited by Axel; 09-12-2005 at 10:42 PM.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not a space. It's a newline. You're using fgets, which includes the newline in the input. Try printing the decimal value of the character instead of the character representation of it. (%d)


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

  10. #25
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    actually, i tried what you suggested:
    which gave me the following,


    Code:
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 97
    Column 3 has -=> 100
    Column 4 has -=> 115
    Column 5 has -=> 10
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 115
    Column 3 has -=> 10
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 115
    Column 3 has -=> 10
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 115
    Column 3 has -=> 10
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 115
    Column 3 has -=> 10
    Column 0 has -=> 97
    Column 1 has -=> 100
    Column 2 has -=> 115
    Column 3 has -=> 97
    Column 4 has -=> 100
    Column 5 has -=> 115
    i modified this bit:

    Code:
     
    
    printf ("%d", userInput[i][j]);

    but your post gave me an idea, i put != '\n' in the for loop and it works now thanks :-)

  11. #26
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i put != '\n' in the for loop and it works now thanks
    Just remember, fgets() won't always put a \n at the end of the string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  2. How do i ouput multiple inputs?
    By vodka9er in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 09:45 AM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Reading multiple values using fgets
    By Stripes in forum C Programming
    Replies: 3
    Last Post: 06-12-2002, 07:21 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM