Something strange with a simple program, possible scanf problem?

Up vote 0 down vote favorite share g+ share fb share tw.

I : most_fat_food_id; printf ("\nThe most fattening product is: %c with %5.2f calories",food_idmost_fat_food_id,food_calmost_fat_food_id); return 0; } /* enter the following data: food ID, Cal value and Amount eaten example A 10 3 Enter product #0:A 1000 2 A 1000.00 2.00 Enter product #1:B 500 3 input error, input length was 1 excpexted 3 Total amount of calories is 2000.00 The most fattening product is: A with 1000.00 calories */ it just skips the input of the 3rd No idea why... two people looked at the code, looked fine but still have an error. C scanf link|improve this question edited Nov 8 '10 at 21:28Roger Pate asked Nov 8 '10 at 21:06Iakovl194 50% accept rate.

Roger - mea culpa - but do notice that the OP posted a running example that perfectly displayed his error. – KevinDTimm Nov 8 '10 at 21:29 @Kevin: Yes, I've, unfortunately, been using a form-like message with general advice for this — the links are useful for users5..'s later questions and anyone else's questions, too. – Roger Pate Nov 8 '10 at 21:30 @Roger - I'll change if you will :) – KevinDTimm Nov 8 '10 at 21:31 @Kevin: (How's this?

;) "The homework tag, like other so-called 'meta' tags, is now discouraged," but, @user501160, please continue to follow general guidelines, state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. – Roger Pate Nov 8 '10 at 21:32 @Roger - I think he already did the second part though ;) (show what you've tried so far) – KevinDTimm Nov 8 '10 at 22:36.

Input: A 1000 2ENTERB 500 3ENTER... The first scanf("%c %f %f") reads 'A', 1000, and 2 and stops at the ENTER The second scanf("%c %f %f") reads the ENTER and chokes with the B for the "%f" conversion. You can try using scanf(" %c %f %f") to force a skip of optional whitespace before reading the char.

Thx... this did the trick – Iakovl Nov 8 '10 at 21:30 Just 1 important thing to remember: the "normal" scanf conversion specifiers (%d, %i, %f, %s) skip leading whitespace (spaces, newlines, ...); "%c" doesn't. To force a skip of whitespace use a space inside the format string. – pmg Nov 8 '10 at 21:51.

After line 33 add: char nl = 0; scanf("%c", &nl); that will consume the final newline character that's screwing things up. After the said suggestion, this is the output: . /test enter the following data: food ID, Cal value and Amount eaten example A 10 3 Enter product #0:a 10 2 a 10.00 2.00 Enter product #1:b 3 29 be 3.00 29.00 Enter product #2:c 32 4 c 32.00 4.00 Total amount of calories is 235.00 The most fattening product is: c with 32.00 calories.

Why so many characters in your program name ;) – KevinDTimm Nov 8 '10 at 21:27.

You don't consume the '\n' the quick/dirty method is to add a variable: char readNewLine; after your scanf(), add another: scanf("%c", &readNewLine); This is ugly, but it will suffice - better methods will become available after you've used C for a while.

Better methods are avaliable already. Why not suggest that the OP read up on fgets(), especially since his input data is going to be quite small? – Chris Lutz Nov 8 '10 at 21:24 Because, as homework, I don't presuppose his/her level of learning.

If the Prof (et.al. ) haven't gotten that far, it's too much info. I'm almost ashamed I answered the question at all (besides just saying that the '\n' wasn't consumed) – KevinDTimm Nov 8 '10 at 21:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions