The _cscanf() function

Hello i want to use _cscanf without the input character being echoed.For example when i press ENTER to finish the reading it echoes '\r' or when i was press backspace it echoes '\b'.But i dont want to input them.From microsoft site ( http://msdn.microsoft.com/en-us/library/tsbaaw16.aspx ) : "While _cscanf normally echoes the input character, it does not do so if the last call was to _ungetch."
How can i use that ? I tried :

1
2
3
4
5
6
7
8
9
10
#include <conio.h>
int main()
{
    int a,b,c;
    _ungetch('\r');
    _cscanf("%d%d%d",&a,&b,&c);
    _cprintf("<--The numbers should be there");
    return 0;
}



, but it does not work. Can someone show me how to do this?
Last edited on
OK i get it you cant.If _cscanf reads data from _ungetch it does not echo it,else it does.Pretty bad i wanted a function that reads data and leaves the cursor at the same position after finishing reading.Not inserting a newline or something like that...(like cin and scanf does...)
Last edited on
conio.h is a deprecated non-standard header and should never be used:
https://en.wikipedia.org/wiki/Conio.h
I dont understand why its called depreciated.Since its a microsoft product its suportted by them in every of their ide even in visual studio 2013.And believe me conio.h is used in pretty almost every console game ive seen.And what its so bad about it?Ive watched the source code of its functions and its seems it pretty much uses win api ,to get direct console input,output.Its windows.h bad and depreciated too?
And yeah about the non standard thing you are right unfortuanatly.On some compilers conio's functions dont behave like others compilers functions...
For example on borland one if i write:

#include <conio.h>
int main()
{
_cprintf("Hello World\n");
_getch();
_cprintf("See where the cursor is ?");
return 0;
}

the '\n' its not going to jump at the end of the line but its just going to move the cursor a line down.
To get the same effect you wanted you had to write "\n\r".

But on visual studio for example if i write the same thing its just going to jump at the end of the line auttomaticly, and microsoft said

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// crt_cprintf.c
// compile with: /c
// This program displays some variables to the console.

#include <conio.h>

int main( void )
{
    int         i = -16,
                h = 29;
    unsigned    u = 62511;
    char        c = 'A';
    char        s[] = "Test";

    // Note that console output does not translate \n as
    // standard output does. Use \r\n instead.
    //
    _cprintf( "%d  %.4x  %u  %c %s\r\n", i, h, u, c, s );
}



, that it acts like borland but again you dont need that '\r' because it just jumps to the beggining of the line anyway :(( ...
conio.h is a deprecated non-standard header and should never be used:



Why it is so ? Every important windows compiler has it, so you can say is standard on windows.
conio.h is not depreciated, it is deprecated. It is non-standard because it is not in any C or C++ standards.
windows.h is *not* standard either. but every windows programmer use it (even when using an external library/framework). How about that ? Or how about WinMain ? Is not a standard entry point either.
Yeah i know i wanted to tell that to him too.And conio.h is not deprecated! It may been out for some time but its functions are still very useful.Too bad though the functions form borland conio (clrscr,textcolor,textbackground....) are only availble in borland.Now if we wanted to do that we will have to write our own functions in WIN API, which is pretty hard for newbies to c++ to do that.If we had them anyone could easily play with the console.
Last edited on
Windows.h (the W is capitalized in all distributions of VC++) is not deprecated.
Ok i found a way

1
2
3
4
5
6
7
8
9
10
#include <conio.h>

int main()
{
    int a;
    _cscanf("%d",&a);
    _cputs("\b \b");
    _cprintf("You entered %d",a);
    return 0;
}


It works if the user inputs anything other than ENTER or backspace to finish reading.
Last edited on
Ok i found the best solution for this it works with enters and backspace too.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <conio.h>
#include <windows.h>

CONSOLE_SCREEN_BUFFER_INFO Con_Info;
HANDLE hConsoleOut=GetStdHandle(STD_OUTPUT_HANDLE);

short posx,posy;

#define Cscanf(x,y) \
    posx=wherex();\
    posy=wherey();\
    _cscanf(x,&y);\
    gotoxy(posx,posy);\
    _cprintf(x,y);\
    if(wherex()==80)\
    {\
        putch(' ');\
        gotoxy(wherey()-1,80);\
    }\
    else _cputs(" \b");

short wherex()
{
    GetConsoleScreenBufferInfo(hConsoleOut,&Con_Info);
    return Con_Info.dwCursorPosition.X+1;
}
short wherey()
{
    GetConsoleScreenBufferInfo(hConsoleOut,&Con_Info);
    return Con_Info.dwCursorPosition.Y+1;
}
void gotoxy(short x,short y)
{
    COORD Coord;
    Coord.X=x-1;Coord.Y=y-1;
    SetConsoleCursorPosition(hConsoleOut,Coord);
}
int main()
{
    int a;
    _cprintf("Please enter a number ");
    Cscanf("%d",a);
    _cprintf ("You entered %d",a);
    return 0;
}

Last edited on
Windows.h (the W is capitalized in all distributions of VC++) is not deprecated.

L B so what makes conio.h depreciated anyway ?
Depreciated != Deprecated

Depreciated:

In accountancy, depreciation refers to two aspects of the same concept:
the decrease in value of assets (fair value depreciation), and
the allocation of the cost of assets to periods in which the assets are used (depreciation with the matching principle).


Deprecated:

Deprecation is a status applied to a computer software feature, characteristic, or practice indicating it should be avoided, typically because it is being superseded.


(both from Wikipedia).

conio.h is deprecated because 'it should be avoided', mainly due to the inconsistencies between different compilers (and versions of the header). Better options are to use libraries like pdcurses, or even just to use the Win32 API. Contrary to what most people seem to think, the API isn't actually all that tough to learn or use, the only difficulty is the sheer number of options that are provided, and (for some) the fact that it uses a C API, rather than an object-oriented C++ one. However, for the former issue, MSDN is your best friend, and for the latter, either there are wrappers that can be used (such as MFC) or you could just accept the fact and live with it.
Topic archived. No new replies allowed.