Jump to content
  • Advertisement
Sign in to follow this  
Xnin

Please help with c scanf() function

This topic is 5081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've got this little problem. I made an array[m][n] where the user inputs m, n using the scanf() function. Heres the relevant code:
-------------------------------------------
int main()
{        
        int rows = 0, columns = 0;
	
	printf ( "Enter rows: ");
	scanf ("%d", &rows);
	
	printf ( "Enter columns: ");
	scanf ("%d", &columns);
	
	printf ("Enter values for array:\n");
	
	char array[rows][columns];
	int i,j;
	
	for (i = 0; i < rows; i++)
	{
		j = 0;
		while ( ( (array[j] = getchar()) != EOF  ) 	&&	(j < columns)	)
			j++;
	}


	printf("\narray[0][0] = %c\n", array[0][0]); //just print the [0][0] element.
        return (0);
}
------------------------------------------------------
and heres the output when i run it:
----------------------------
Enter rows: 3
Enter columns: 3
Enter values for array:
ron
jon
don

array[0][0] =            //should there be 'r' here????
-----------------------------
Any idea why its not displaying the [0][0] element? When i change it so that there is a fixed number of rows and columns (ie the user doesnt input anthing), it works normally?? So im thinking its got something to do with the scanf() function? By the way, im compiling this with gcc v3.3.4. And the code is alot easier to understand if u copy n paste it to your faivourite editor. Any help is greatly appreciated! :) p.s. feel free to move the thread if its in the wrong forum. Thanks again.

Share this post


Link to post
Share on other sites
Advertisement
Where do you assign the values you enter at runtime to the array?

The "while" statement only checks for what you enter is not EOF and j is smaller than the column size.

Share this post


Link to post
Share on other sites
What happens to the enter character that users enter between words - you should prolly skip over whitespace characters (or at least some of the whitespace characters).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thedevdan has got it right here. inputting those values at run time makes no sense, because your array is being allocated on the stack. this means that it is already created before you try to create it, see how it makes no sense? what you need is dynamically allocated memory, or an array whose boundaries are specified at compile time. malloc is your friend.

krines

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Right thanks guys, i understand why its not working now, i never thought of it that way. I'll just change the method. and malloc is too complicated for what im doing. :)

Thanks again. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!