Integer error C -


this question has answer here:

this have now:

int main() { int number; printf("type number: "); scanf("%i",&number);  char code[4]; printf("type code: "); scanf("%s",&code); 

when type numbers in first 1 script goes crazy, shows

type number: notanumber

type code: therestofthescript

-- command line

what want is

type number: notanumber

you didn't enter number

-- command line

how can this?


this different said duplicate. i'm talking c here, not c++. c doesn't know cin, answer said duplicate. however, answer found below

for input string use

  scanf("%s",code); 

or better

  scanf("%3s",code); 

to check correct input number use value returned scanf:

   char ch;    if( 1 == scanf("%i",&number) )    {         // use correct number    }    else    {         // clean input bufer        while( (ch = getchar()) != '\n' && ch != eof);    } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -