Welcome, Guest. Please login or register.
Did you miss your activation email?
June 29, 2016, 02:42:18 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 6 - Making Decisions
| | | |-+ Ex 6.4 does not work if input reversed - why is this?
Pages: [1] Go Down
Author Topic: Ex 6.4 does not work if input reversed - why is this? (Read 1217 times)
Posts: 10

on: May 12, 2010, 12:43:24 PM

In Ex 6.4 you use a scanf("%lf  %c", &number, &cinput); to get a  user input like:
  33.3  S [return key]

I had a student reverse this order so the character was input first.. and the program no longer works..

So..   scanf("%c  %lf", etc..

causes some weird error in the program..

I tracked it down to this..

when the student inputs:
  S  33.4 [return key]

The program processes the data properly the first time and then loops back to get new data.. however, instead of flushing the old data is seems to get a 'CRLF' in the input stream left over from the previous input, processes that (which the student's program showed as a 'bad operator' and then .. well.. a little hard to explain.. but it continues to process the new input properly..

But if you take a working program, that has a default block that will say 'bad operator' and if you change the order of the input to accepting the character first.. the program does not work as it should..

I'm just wondering if Stephen ran across this when designing the exercise.. because I can't understand why it gives the error.. and I think it is some weird compiler bug or limitation of the scanf function I don't understand.

Dave Crabbe
Hero Member
Posts: 3114

Reply #1 on: May 12, 2010, 01:02:16 PM


This is the way scanf works with the %c format characters.  If you precede it with a space character in the format string, any leading  whitespace characters will be skipped.  Without those, scanf will literally read the next character on input, which could be a space, tab, newline, carriage return or line feed (a so-called whitespace character).


Steve Kochan
Posts: 10

Reply #2 on: May 15, 2010, 04:01:59 AM

thanks, Stephen..

The space before the %c is very important. I don't know if you mention it in the book ..

If an enterprising student were doing 6.7 and put in a loop to continuously get input, the program shouldn't work as listed .. From what you've said, you should have scanf ("  %c",&c); 

I'll make this note for when I cover scanf next year in my class .. we'll talk about the newline character and how it is not cleared by scanf ..
Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.