Characters not read correctly by scanf()
Author |
Message |
Srlancelot39
|
Posted: 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 some hours of testing, I think I have confirmed that it is an issue to do with scanf() (scanf_s() in my case specifically).
code: | /*
Name: Sean Sciberras
Course: PRG155H
Date: March 12, 2014
Program Description: Passes a character to a function and returns the character after it.
*/
#include<stdio.h>
#include<conio.h>
//declare functions
char function(char);
//main program
void main()
{
char chr;
//get character from user
printf("Enter a character: ");
scanf_s("%c", &chr);
printf("\nYou entered '%c', printing it now from main().", chr);
//pass character to function
function(chr);
printf("\nThe following ASCII character is '%c', printing it now from main().", function(chr));
_getch();
}
//function
char function(char chr1)
{
printf("\nYou entered '%c', printing it now from function().", chr1);
//return next character
return (chr1 + 1);
} |
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! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: 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). |
|
|
|
|
|
nullptr
|
Posted: 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:
code: |
char chr;
//get character from user
printf("Enter a character: ");
char buf[2];
scanf_s("%1s", buf);
chr = buf[0];
|
|
|
|
|
|
|
Srlancelot39
|
Posted: 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 after the user input is read, the program will display 'x' and then 'y', as it should.
I have tried using and...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
|
|
|
|
|
Insectoid
|
Posted: 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. |
|
|
|
|
|
Srlancelot39
|
Posted: 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
c: | // crt_getch.c
// compile with: /c
// This program reads characters from
// the keyboard until it receives a 'Y' or 'y'.
#include <conio.h>
#include <ctype.h>
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 |
|
|
|
|
|
btiffin
|
Posted: 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
code: |
scanf_s("%c", &chr);
|
try
code: |
scanf_s("%c", &chr, 1);
|
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
code: |
scanf("%1c", &chr);
|
Live free and die trying or Live easy and die pwned
Cheers |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Srlancelot39
|
Posted: 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. |
|
|
|
|
|
btiffin
|
Posted: 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
code: |
#define _CRT_SECURE_NO_DEPRECATE
|
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
|
Posted: 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. |
|
|
|
|
|
|
|