Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 2 votes

How to use scanf in C.

myvariable scanf

  • Please log in to reply
30 replies to this topic

#25 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 02 August 2012 - 02:08 PM

You're using logical comparison incorrectly for this purpose.

"N" and "n" both could point to a memory location and both be well above 0, and because one or both of them should be above 0, it becomes true.

if(playagain != (true || true)) { 

}

You must use a longer compound comparison.

if(playagain != 'N' || playagain != 'n') { //...

You must use character literals 'n' and not strings which are not characters, but possibly pointers to some location "N" is really 'N' '\0' at some location on the stack.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#26 object

object

    CC Regular

  • Member
  • PipPipPip
  • 35 posts
  • Programming Language:C, C#, JavaScript

Posted 04 January 2013 - 08:33 AM

Well, this is one very misinformed thread!

I'll comment on scanf since I think it's an awful function. Scanf expects data of the correct type. If you enter in the wrong data then it fails to work. Now you can catch this by catching the return code, scanf returns the number of items it read in so if it returns 0 you know that it failed on the first entry.

Thankyou! Most people completely ignore the return value, which is a very poor practise. You are one of the more enlightened people, though still not enlightened enough to use scanf correctly.

You may think this appropriate

int check = 1;
int var = 0;
while(check){
check = !scanf("%d", &var);
}

Since there's only one variable read, it will either return 0 on failure or 1 on success. The ! inverts this and check feeds it back into the while loop (so that this will loop until there is correct input).

You may think job done. Unfortunately scanf doesn't flush the input buffer on failure so if I enter 'error' it will stay on the input buffer for the next time, this program gives an infinite loop on an error.

There are several things wrong here. Firstly, scanf may return a negative integer! It's far more appropriate to check that scanf is reading the correct number of items, like: while (scanf("%d", &var) != 1 && getchar() >= 0);

Why do you mention flushing input buffer? No such thing as an input buffer buffer is required. To flush is to write; Writing to a file open for input just makes no sense, and is hence undefined behaviour. That'd be roughly the same as saying that you flush the toilet and the waste comes out of the bowl! The reason for the infinite loop you mention has nothing to do with flushing, and everything to do with ungetc. When scanf comes across a conversion failure, it ungetc's the character back onto the stream, ready to be read next time you call scanf.

For example, let us say we call scanf to expect integer input, and it gets "a{enter}". scanf reads 'a' from stdin, notices the invalid data and returns 0, indicating a conversion failure... but before it returns, it puts 'a' back into stdin. We loop to retry, and the same thing happens all over again...

So what can we do? We could call fflush(stdin); but this behaviour is undefined according to the C standard. ...

You can use getchar, as I did above, to read and discard a character in a portable, well defined way.

Personally I prefer to use fgets. ...

Great! Stick to what you know how to use. It seems very wise to refrain from using angle grinders and other power-tools until you know how to use them safely. Just be aware that scanf does have neat applications. Due to the fact that scanf requires no "buff" variable, parsing could be much more efficient in applications that use stdin heavily.

@Boykie: When scanf using %d gets the following string from stdin: "1234\n", it'll parse 1234 and leave the '\n' on the stream. This is what your %c picks up: A newline.

@Daisycrystal: Is it often that you flush the toilet to find the waste flying back up at your face? Don't use fflush(stdin) on a computer programmed toilet ;) That's undefined behaviour. Read this entire post for a well defined alternative, which will work on every system today and well into the future.

@VakhoQ: You are very confused. getchar returns an int, not a char. Why is that so? The successful return value of getchar() will be an unsigned char value. Are you sure the negative EOF is an unsigned char value? I suggest reading the manpages for getchar. What is the difference between "k" and 'k'? Which book are you reading?

@Promo2: Sure. Disect the following format string: "%*[0-9]%[0-9]%*[0-9]%*[0-9]". Google "opengroup scanf" for more information about the %[ format specifier. It requires a char input.

@Alexander: cplusplus.com is probably not the best reference for C functions. I'd suggest opengroup.

@poffpaul: C++ is not C, nor a superset of C. To clarify, there are features of C that C++ doesn't have, and that are subtly different to features that appear to be the same. Take modulo for example... -3 % 2 isn't required to be the same in C++ as it is in C. Another example is implicit void pointer conversion: char *foo = malloc(42); doesn't work in C++. Don't learn C++ as though it'll teach you C, because it won't. Learn C as though it'll teach you C, and C++ as though it'll teach you C++. Read this entire post for a solution to your problem.

Edited by object, 04 January 2013 - 08:35 AM.

  • 0

#27 shaunbags

shaunbags

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 20 February 2014 - 06:16 PM

hi guys, just a quick question on scanf

how would i underline the output when using this function?

the problem set im doing on my course states the output must be underlined....

i have not had any luck in searching this..

cheers
  • 0

#28 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts
  • Location:Chicago
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Others
  • Learning:Assembly, Others

Posted 20 February 2014 - 11:16 PM

hi guys, just a quick question on scanf

how would i underline the output when using this function?

the problem set im doing on my course states the output must be underlined....

i have not had any luck in searching this..

cheers

 

scanf is for input, so I really don't think you can use it to underline output. And I am pretty sure that this cannot be done with printf either.


  • 0

                                                                                                                                                                            FACEB00K Likes this.


#29 shaunbags

shaunbags

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 20 February 2014 - 11:39 PM

scanf is for input, so I really don't think you can use it to underline output. And I am pretty sure that this cannot be done with printf either.

thanks for the reply KJGino, I found it strange they requested it without covering it in the study material
-Shaun
  • 0

#30 WETHECOMPUTERGUYS

WETHECOMPUTERGUYS

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 February 2014 - 01:53 AM

This video will provide u all information:


Edited by WETHECOMPUTERGUYS, 21 February 2014 - 01:54 AM.

  • 0

#31 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 21 February 2014 - 05:12 AM

First of all, underlining output depends on the output medium. Plain text doesn't have the concept of text formatting. It's sort of like asking, "How do I underline text in Notepad?" You don't.

 

Second of all, as KJGino note, scanf is input, not output :)


  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: myvariable, scanf