Computer Science Canada Characters not read correctly by scanf() |
Author: | Srlancelot39 [ Wed Mar 12, 2014 10:35 pm ] | ||
Post subject: | Characters not read correctly by scanf() | ||
This is a program I have written for a question in a lab. It gets a character from the user, displays it, passes it to a function that displays it, the function then returns the character after the user's character and this is printed from main(). My issue is that no matter what character I enter, it displays it as a space (except for the returned value from the function which is always some unexpected symbol). After
Output: Enter a character: a You entered ' ', printing it now from main(). You entered ' ', printing it now from function(). You entered ' ', printing it now from function(). The following ASCII character is '☺', printing it now from main(). Thanks in advance! |
Author: | Insectoid [ Wed Mar 12, 2014 11:05 pm ] |
Post subject: | RE:Characters not read correctly by scanf() |
Odd, it works fine for me (after commenting out _getch() and #include <conio.h>, since they don't exist on osx). |
Author: | nullptr [ Wed Mar 12, 2014 11:29 pm ] | ||
Post subject: | Re: Characters not read correctly by scanf() | ||
I'm not sure what could be causing your issue, but you could try this as a solution:
|
Author: | Srlancelot39 [ Wed Mar 12, 2014 11:56 pm ] | ||||||||||
Post subject: | RE:Characters not read correctly by scanf() | ||||||||||
@Insectoid Half of me feared that would be the case, the other half hoped it would be lol I hope it works for my prof lol @nullptr thanks for the help, but same output :s wtf is going on lol Anyone got Bill Gates' cell number on hand?... EDIT: Ok, I am 99.9% sure that the user input is not being read correctly. If I insert
I have tried using
EDIT2: Yes, I have tried using pointers. Exact same result. EDIT3: (very sorry for all the edits lol) I am using Visual Studio - Professional 2013 |
Author: | Dreadnought [ Thu Mar 13, 2014 8:05 pm ] | ||
Post subject: | Re: Characters not read correctly by scanf() | ||
I'm pretty sure that _getch returns the character (rather than taking a reference).
Source: http://msdn.microsoft.com/en-us/library/078sfkak.aspx |
Author: | Insectoid [ Thu Mar 13, 2014 8:10 pm ] |
Post subject: | RE:Characters not read correctly by scanf() |
Quote: I am using _getch() because conio.h is requiring me to for some reason.
Newer compilers (since 1989, so older compilers too) require this to comply with ANSI C standards. |
Author: | Srlancelot39 [ Fri Mar 14, 2014 12:17 pm ] | ||
Post subject: | Re: Characters not read correctly by scanf() | ||
Dreadnought wrote: I'm pretty sure that _getch returns the character (rather than taking a reference). According to the rest of the document, it seems like it takes input without echo. http://msdn.microsoft.com/en-us/library/078sfkak.aspx wrote: Gets a character from the console without echo. Example
...I don't know, I'm confused as hell as to why I'm having this problem lol Thanks for the help! I know this isn't the easiest of things to troubleshoot |
Author: | btiffin [ Thu Apr 03, 2014 1:26 am ] | ||||||
Post subject: | Re: Characters not read correctly by scanf() | ||||||
A little late perhaps, but from the original listing
try
Secure scanf REQUIRES the length for char data. I think that's because people that work for Microsoft believe that wchar was a good idea. Or ... avoid MS non-standard crap and try
Live free and die trying or Live easy and die pwned Cheers |
Author: | Srlancelot39 [ Thu Apr 03, 2014 2:14 pm ] |
Post subject: | RE:Characters not read correctly by scanf() |
Thanks for your solution! I no longer need this solved, but thanks anyways! Much appreciated As for scanf_s, my compiler will not let me use scanf. I tried using it initially (as that is what we were taught), but my compiler gives me an error about security and suggests that I use scanf_s. |
Author: | btiffin [ Sun Apr 06, 2014 12:20 pm ] | ||
Post subject: | Re: Characters not read correctly by scanf() | ||
Realizing that the immediate need is over, but, being old and crusty... As far as I know, only Microsoft has deprecated the scanf function. It's still very much alive in C land. If you ever need to do this again, try
at the top of the sources, then wield scanf like the big boys (hmm, or is that bug boys?). Is scanf dangerous? Can be, but scanf_s isn't implemented very often outside Windows, so scanf_s is far less portable. The MS tactic of embrace, enhance, extinguish is HIGHLY UNLIKELY to ever succeed when it comes to C. Cheers |
Author: | Srlancelot39 [ Tue Apr 08, 2014 6:59 am ] |
Post subject: | RE:Characters not read correctly by scanf() |
Sounds like a plan! Thanks! I've heard of using #define _CRT_SECURE_NO_DEPRECATE before, so I'm sure I won't forget to try it out next chance I get. |