Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
A scanf() Question for the Gurus Posted by Ed Hall on 7 Oct 2009 at 6:10 PM
I often rework programs from the forum to either help the original poster or learn a little more from some of the code (or both), but I have run across an unexpected result in a recent rework using scanf().

In this case scanf() is used to fill a simple array, with each value being tested against a limit. But, the array seems to be being manipulated differently in some cases.

In the following code, if all five values are entered at once, with one needing modification, the modified one is moved to the end of the array and the others are moved up. If the values are entered individually, all is as expected.

The program as I rewrote it (compiled using Dev-C++ 4.9.9.2 in WinXP):
#include <stdio.h>
#include <stdlib.h>

int main()
{
  
	int num[5],i;

	printf("\nPls enter 5 numbers: ");

	for(i=0; i<5; i++)
	     
	{
        	scanf("%d", &num[i]);

		while(num[i]>11)
	        {
			printf("\nPls reenter number %d: ", i+1);
			scanf("%d", &num[i]);
                }
	}

	 printf("\nThe numbers are:");
         for(i=0; i<5; i++)
	        printf("\n%d %d", i+1, num[i]);  
           
  printf("\n\n");	  
  system("PAUSE");	
  return 0;
}


session 1 - all values entered at once:

Pls enter 5 numbers: 1 2 33 4 5

Pls reenter number 3: 3

The numbers are:
1 1
2 2
3 4
4 5
5 3

Press any key to continue . . .


session 2 - each value entered individually:

Pls enter 5 numbers: 1
2
33

Pls reenter number 3: 3
4
5

The numbers are:
1 1
2 2
3 3
4 4
5 5

Press any key to continue . . .


session 3 - double rentry of invalid element:

Pls enter 5 numbers: 1 2 33 4 5

Pls reenter number 3: 33

Pls reenter number 5: 3

The numbers are:
1 1
2 2
3 4
4 5
5 3

Press any key to continue . . .


Obviously, the array is being manipulated different from my expectations. Why is the value of element 2 (shown as 3) being moved in this manner?

Thanks.

Take Care,
Ed
Report
Re: A scanf() Question for the Gurus Posted by charan_s2003 on 8 Oct 2009 at 10:10 PM
Hi,

scanf() function will maintain an internal buffer and will read from stdin(user input) only if the internal buffer is empty.

So if you consider the case where all five values are entered at once, with one needing modification, internally this happens

Pls enter 5 numbers: 1 2 33 4 5


So after the first call to scanf(i=0) inside the for loop, the internal_buffer has 5 values which are given.

When i=1, scanf() will fetch the data from its internal_buffer and return 2.

Similarly when i=2, 33 it is returned from internal_buffer which is invalid, so it enters into that while condition



while(num[i]>11)
	        {
			printf("\nPls reenter number %d: ", i+1);
			scanf("%d", &num[i]);
                }




Now the call to the scanf() inside the while loop will again fetch the data from its internal_buffer , so 4 is returned and num[2] = 4 is set.

Next num[3] = 5 is set (from internal buffer). Now i=4 , internal_buffer is empty , so the user input is taken from the keyboard.

for(i=0; i<5; i++)	     
	{
        	scanf("%d", &num[i]);


Now when you enter 3 , num[4] = 3 is set.

Report
Re: A scanf() Question for the Gurus Posted by Ed Hall on 10 Oct 2009 at 6:07 AM
Thanks for your reply, but I still do not see why the array is not filled properly. The scanf() function does not see 33 as an invalid entry. It is still an integer, which is what is expected for that variable. It should read all five values in sequence and merely replace the 33 within the designated position.

Again, thank you and I will revisit your response to see if I can come to an understanding when I can devote some more time to it.

Take Care,
Ed
Report
Solution:Program compiled and executed in C-Free 4.1 Posted by nivp4m on 12 Oct 2009 at 12:36 AM
#include <stdio.h>
#include <conio.h>
void main()
{
int mod, num[5],i,a;

printf("\npls enter 5 numbers::");

for(i=0; i<5; i++)

{
scanf("%d", &num[i]);
}
for(i=0; i<5; i++)
{
for(i=0; i<5; i++)

{
if (num[i]>11)
{a=i;break;}
}
}
printf("enter the no to be inserted at position no. %d :",(a+1) );
scanf("%d",&mod);
num[a]=mod;


for(i=0; i<5; i++)

{
printf("\n\n%d", num[i]);
}

printf("\n\nProgram is going to terminate.....");
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.