Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A scanf() Question for the Gurus

Ed HallEd Hall Posts: 395Member
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):
[code]
#include
#include

int main()
{

int num[5],i;

printf("
Pls enter 5 numbers: ");

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

{
scanf("%d", &num[i]);

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

printf("
The numbers are:");
for(i=0; i<5; i++)
printf("
%d %d", i+1, num[i]);

printf("

");
system("PAUSE");
return 0;
}
[/code]

session 1 - all values entered at once:
[code]

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 . . .
[/code]

session 2 - each value entered individually:
[code]

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 . . .
[/code]

session 3 - double rentry of invalid element:
[code]

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 . . .
[/code]

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

Comments

  • charan_s2003charan_s2003 Posts: 2Member
    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

    [code]
    Pls enter 5 numbers: 1 2 33 4 5
    [/code]

    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


    [code]

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


    [/code]

    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.

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

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

  • Ed HallEd Hall Posts: 395Member
    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
  • nivp4mnivp4m Posts: 14Member
    #include
    #include
    void main()
    {
    int mod, num[5],i,a;

    printf("
    pls 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("

    %d", num[i]);
    }

    printf("

    Program is going to terminate.....");
    }
Sign In or Register to comment.