
-----------------------------------
Srlancelot39
Wed Mar 12, 2014 10:35 pm

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 some hours of testing, I think I have confirmed that it is an issue to do with scanf() (scanf_s() in my case specifically).

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 '&#9786;', printing it now from main().


Thanks in advance!

-----------------------------------
Insectoid
Wed Mar 12, 2014 11:05 pm

RE:Characters not read correctly by scanf()
-----------------------------------
Odd, it works fine for me (after commenting out _getch() and #include , since they don't exist on osx).

-----------------------------------
nullptr
Wed Mar 12, 2014 11:29 pm

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:

[code]
        char chr;
        //get character from user
        printf("Enter a character: ");
        char buf[2];
        scanf_s("%1s", buf);
        chr = buf[0];
[/code]

-----------------------------------
Srlancelot39
Wed Mar 12, 2014 11:56 pm

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 [code]chr = 'x';[/code] after the user input is read, the program will display 'x' and then 'y', as it should.
I have tried using [code]getch(chr);[/code][code]getch(&chr);[/code][code]_getch(chr);[/code]and[code]_getch(&chr);[/code]...none of which work either.  I am using _getch() because conio.h is requiring me to for some reason.

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

-----------------------------------
Dreadnought
Thu Mar 13, 2014 8:05 pm

Re: Characters not read correctly by scanf()
-----------------------------------
I'm pretty sure that _getch returns the character (rather than taking a reference).

[code]int _getch( void );[/code]
Source: http://msdn.microsoft.com/en-us/library/078sfkak.aspx

-----------------------------------
Insectoid
Thu Mar 13, 2014 8:10 pm

RE:Characters not read correctly by scanf()
-----------------------------------
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.

-----------------------------------
Srlancelot39
Fri Mar 14, 2014 12:17 pm

Re: Characters not read correctly by scanf()
-----------------------------------

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.

Gets a character from the console without echo.

Example
// crt_getch.c
// compile with: /c
// This program reads characters from
// the keyboard until it receives a 'Y' or 'y'.
 

#include 
#include 

int main( void )
{
   int ch;

   _cputs( "Type 'Y' when finished typing keys: " );
   do
   {
      ch = _getch();
      ch = toupper( ch );
   } while( ch != 'Y' );

   _putch( ch );
   _putch( '\r' );    // Carriage return
   _putch( '\n' );    // Line feed  
}


...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 :P

-----------------------------------
btiffin
Thu Apr 03, 2014 1:26 am

Re: Characters not read correctly by scanf()
-----------------------------------
A little late perhaps, but from the original listing

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

-----------------------------------
Srlancelot39
Thu Apr 03, 2014 2:14 pm

RE:Characters not read correctly by scanf()
-----------------------------------
Thanks for your solution!
I no longer need this solved, but thanks anyways!  Much appreciated :P

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.

-----------------------------------
btiffin
Sun Apr 06, 2014 12:20 pm

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

[code]
#define _CRT_SECURE_NO_DEPRECATE
[/code]

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

-----------------------------------
Srlancelot39
Tue Apr 08, 2014 6:59 am

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.
