Why does scanf seem to skip input?

I am confused about the behavior of scanf in the following program. scanf appears for input once and then is not entered again until a character stream is printed.

Below in program C

#include<stdio.h>
int main()
{
    int i, j=0;

    do
    {
        ++j;
        scanf("%d", &i);
        printf("\n\n%d %d\n\n", i, j);
    }
    while((i!=8) && (j<10));  

    printf("\nJ = %d\n", j);
    return 0;
}

here, until I enter any integer program, it works fine, but when the character is entered, it continues to print the last value I entered and never stops (until j is 10 when the loop exits) for scanf to do next entrance.

output::  
1    <-----1st input
1 1
2    <---- 2nd input
2 2
a    <---- character input
2 3  
2 4
2 5
2 6
2 7
2 8
2 9
2 10

J = 10  

The same thing happens in C ++.

#include<iostream>
using namespace std;
int main()
{
    int i, j=0;

    do
    {
        ++j;
        cin>>i;
        cout<<i<<" "<<j<<"\n";
    }
    while((i!=8) && (j<10));

    cout<<"\nj = "<<j<<"\n";
}   


output of c++ program ::  
1     <-----1st input
1 1
2     <-----2nd input
2 2
a    <------ character input
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

j = 10  

only change in C ++ is that instead of the last value, 0 is printed.

I know that integer values ​​are expected by the program, but I want to know what happens when a character is entered instead of an integer? what is the reason for everything happening above?

+5
7

a, cin >> i , i int, . , a .

i 0 - . . i . scanf.

:

do
{
    ++j;
    if (!(cin>>i)) 
    {
       //handle error, maybe you want to break the loop here?
    }
    cout<<i<<" "<<j<<"\n";
}
while((i!=8) && (j<10));

( , ):

int i = 0, j = 0;
while((i!=8) && (j<10) && ( cin >> i) )
{
    ++j;
    cout<<i<<" "<<j<<"\n";
}
+5

scanf , , .

. - ( scanf %s %[ fgets), atoi strtol ( ).

scanf; . , scanf("%d", &i); 0, , . getchar() .

+5

, . . , .

+2

, , ( %d scanf int cin>>i;, , ( ), .

, ( , ). ( ), , . , ++ , std::gtline() int std::string , "" , .

+2

. , scanf(3):

RETURN VALUE
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

.

+1

scanf , , . scanf . , , - . :

if (scanf("%d", &i) != 1) {
    char buf[512];
    fgets(buf, sizeof(buf), stdin);
    printf("error in line %d: got %s", j, buf);
    return 0;
}

, , , .

++ fail, . , , .

    std::cin >> i;
    if (std::cin.fail()) {
        std::string buf;
        std::cin.clear();
        std::getline(cin, buf);
        std::cout
             << "error in line " << j
             << ": got " << buf
             << std::endl;
        return 0;
    }

, , cin , , .

, , . :

while read_a_line succeeds
    parse_a_line

C catch , , fgets , . , , sscanf, scanf, .

    if (sscanf(buf, "%d", &i) != 1) {
        printf("error in line %d: got %s", j, buf);
        return 0;
    }

++ sscanf. , , string istringstream .

    std::getline(cin, buf);
    if (std::cin.fail()) {
        break;
    }
    std::istringstream buf_in(buf);
    buf_in >> i;
    if (buf_in.fail()) {
        std::cout << "error in line " << j
             << ": got " << buf
             << std::endl;
        return 0;
    }
+1

You can check the return value scanfto determine if the syntactic integer is correct (return should = 1). In the event of a failure, you have a choice: either notify the user of the error and exit, or restore by reading the next token using scanf("%s" ...), possibly with a warning.

+1
source

All Articles