CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Posts
    218

    what is the problem about this code

    Dear,
    Why the below code ask me to enter 6 numbers? does it not input c[0]....c[4]?
    Code:
    	int c[5], *y,i;
    	for(i=0;i<5;i++)
    	{
    		printf("%d:",i);
    		scanf("%d\n",&c[i]);
    	}

  2. #2
    Join Date
    May 2007
    Posts
    437

    Re: what is the problem about this code

    Quote Originally Posted by lwong
    scanf("%d\n",&c[i]);
    why \n with scanf , remove it .
    ashu
    always use code tag

  3. #3
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: what is the problem about this code

    Thats because you screwd it up by supplying \n in the format string, scanf does not shift the cursor to new line, it instead considers it as an input.

    You should mention \n in 'print' kind of functions only.
    Code:
    int c[5], *y,i;
    	for(i=0;i<5;i++)
    	{
    		printf("%d:",i); // You can supply \n here i.e. printf("%d:\n",i);
    		scanf("%d",&c[i]);
    		printf("\n"); // here print the new line...
    
    	}
    Last edited by Krishnaa; July 20th, 2007 at 02:35 AM.
    Regards,
    Ramkrishna Pawar

  4. #4
    Join Date
    Feb 2004
    Posts
    218

    Re: what is the problem about this code

    oh, yes, thank you all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured