> 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?
Printable View
> 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?
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.Quote:
You have a splendid nack of trying every single possible combination except the right ones told to you by everyone else.
Really, i don't mean to be of any annoyance and mostly all the stuff i use comes from books or online.
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.
.. 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?
to solve above problem, do thisCode: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
and also why woud you print the characters one by one when you can print is as a wholeCode:char *n;
n = strchr( userInput[0], '\n' );
*n = '\0';
I suggest that you go and read the replies to this thread before posting some more questions. Your too excited :)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);
Compile, run and study this and see if it helps you:
Some notes: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
*/
- 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.
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.Quote:
Originally Posted by Hammer
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:
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[0], '\n' );
*n = '\0';
Code:
char *n;
n = strchr( userInput[row], '\n' );
*n = '\0';
gives me the dreaded segmentation fault
I'm guessing it's because
somehow in the for loop it stops workingCode:
for (line = 0 ; line < 8; userInput++)
fgets(userInput[line], 160, stdin);
char *n;
n = strchr( userInput[0], '\n' );
*n = '\0';
Also, a question why have you choosen to put a new line in userInput[0]? and not the last column in the row?
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.
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.
actually, i tried what you suggested:
which gave me the following,
i modified this bit: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
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 :-)
>>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.