Hey,
Remove the ampersand & from the scanf() line of code. Strings don't work with that, just numbers and stuff.
Your char arrays are very small. Remember that all strings need a space for the end of string marker: '\0', which scanf() will try and place onto the end of the string.
You can't flush your kitchen faucet, only the toilet will flush. fflush(stdin), is undefined. Use fflush() with OUTWARD streams, only, not INWARD streams like stdin.
use getchar(); to remove the last newline from the key buffer stream. It IS necessary, sometimes.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
Hi Adak
Thank you very much for the advice, it turns out it was a space issue with my variables.
As you said, they need space for the '\0' byte character but I was not taking into consideration the '\n' when pressing return so there was no room for the NULL character.
I just added an extra byte to each variable to test and magic! it works without problem now.
Can I just clarify, you mentioned not to use the '&' in the scanf() function with strings. I've never come across that (or I just missed it), so is it like the same reason to not use it just as you don't need it when initialising a pointer to an array?
Many thanks
its no difference in string weather you write & or not in string because the string name saves the address it self.
;)
YAMNA MIDHAT
Junior Poster in Training
57 posts since Aug 2011
Reputation Points: 5
Solved Threads: 4
its no difference in string weather you write & or not in string because the string name saves the address it self.
;)
Actually, there's a subtle but significant difference. The address may be the same, but thetype is different. If you pass an object of a different type than scant() expects, the result is undefined behavior. In many cases, undefined behavior means working the same way as if the correct type were passed, but that doesn't excuse doing things the wrong way.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
There is no such thing as undefined behavior in C.
The language definition appears to disagree with you. Undefined behavior is stated as being behavior for which the standard imposes no requirements. The compiler writer is free to choose any result, reasonable or unreasonable, or even not choose a result and simply let whatever falls out of the compiler's design happen. So yes, there's most certainly a such a thing as undefined behavior in C.It's programmers fault.
Yes, it's the programmer's fault for invoking undefined behavior.You can sue the compiler maker for such problems!
Not successfully. As an analogy, you can't successfully sue the police for punishing you in a clear case of violating the law. Compiler vendors arenot responsible for code compiled under their software that violates the rules of the language. Now if the code was compliant with the language standard and failed catastrophically, the compiler vendor could be sued because that falls under their contractual obligations.
Input two words and see that it prints only 1. This is because scanf was designed to take till \0 is encountered.
Incorrect. The %s specifier is defined as being whitespace delimited, so it reads until encountering any whitespace character. The two words are separated by whitespace in your example, and that's why only the first is converted by scanf().The C standard library has all these defined.
Indeed. Maybe you should take some time to read the standard since you're clearly not familiar enough with it. :icon_rolleyes:When working with strings always use gets and puts instead of scanf and printf!
Never use gets(), it's impossible to use safely. puts() is fine, but it appends a newline to the stream which might not be desirable. If you're just dumping a string rather than formatting it, fputs() does not include a newline and is a decent alternative to puts(). On the other hand, if youdo need to format the string first, the printf() family is by far the simplest way to display your output in one step.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
But, I know that I won't be reading the standards... it's just mumbo jumbo to bring perfection and absolution in computing.
:|And honestly, the explanation I gave works on major of my programs.
Oh joy, yet another ignorant amateur touting the tired old argument of "it works for me, so I'm right".If I ever get a problem then I'll revise my ideas.
Well it's not just your problem, because you're trying to teach others. So when some tard's undefined behavior causes the plane you're riding to plow into a mountain because he listened to your advice, be sure to revise your ideas quickly. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
gonna wiki you soon to see if your name comes up. After all you could judge kanetkar so easily then you must be someone like steve wozniak.
Unless someone wrote an article about me (I doubt it), you won't find anything on Wikipedia. However, you're welcome to visit my website or continue reading my posts on Daniweb and CBoard (under the user name Prelude) and judge for yourself if I know what I'm talking about. Be sure to wiki Kanetkar too, as pages dedicated to him are rather critical of his work. Fifteen years ago I'd be less critical, but since he's completely failed to update his books to keep with the times, I see no reason to be tolerant.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401