I then want them to input each value that they want in each row.
It complies, and I am pretty sure the pointers are allocated correctly.
I think the loop is not inputing each user input for the values.

First, argv should be a char**, not a char*. Typo, I imagine, and one that probably has no effect on your program.

You've run into a very common problem when doing user input in C. If you do

Code:

scanf("%d", &x);

Generally the user will type a number and hit enter. %d will read the number, but it will not read the enter (which comes through as a '\n', or newline character). That newline is still hanging around waiting to be read when you call getchar(). getchar() doesn't discriminate, so it reads the newline and returns it. But guess what the user did when you asked for a character? He gave the character and hit enter. The character will be read by the next getchar(), but the newline will be read by the getchar() after that. And so on.

You could use scanf() with %d to read in values for your matrix. %d will eat up any initial whitespace it sees, so all those enters will disappear. This is sort of a band-aid, though, because if the user types in something alphabetic, scanf() will panic and not read anything. This is why it's always smart to check the return value of scanf(), and why it's usually smarter to abandon scanf() completely.

The generally suggested method to read input is to read a line and parse it. The user can type whatever he pleases. Instead of hoping that what he typed is valid, you just read a bunch of it and check. Use fgets() to read a line; it's not perfect but it goes a long way to making your code more robust.

11-07-2007

gibsosmat

Quote:

Originally Posted by tikelele

well I got it to work I changed:

Code:

matrix[i][j] = getchar()

to

Code:

scanf(" &#37;d", &userNum);
matrix[i][j] = userNum;

and I think it is efficient from a memory perspective.

I find faq to be useful in many cases before posting :)
well in your case these links will help you