Unix Programming - unix "getch()" question ,why my not print right char?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > unix "getch()" question ,why my not print right char?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author unix "getch()" question ,why my not print right char?
key9

2006-05-20, 1:15 pm

Hi all

Here's my test app - I want press a key ,display on screen
but it didn't print right.
Help me please;






//use -lstdc++

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int mygetch( ) { // getch()
struct termios oldt,newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}

int main(int argc, char *argv[])
{
char ch_;

while(true)
{
ch=mygetch();

printf("%c",&ch_); // this will always print a blank -- why?
//printf("%d",&ch_); // this will always print -1073744410 -- why?
//std::cout << ch_; // this will print right char. also, std::cout <<
'\b' will not cause a backspace.

}

}



thank you very much

key9


Wayne C. Morris

2006-05-20, 1:15 pm

In article <e4ndgg$rik$1@news.yaako.com>, "key9" <iamkey9@126.com> wrote:

> Here's my test app - I want press a key ,display on screen
> but it didn't print right.
> Help me please;
>

[snip]
> ch=mygetch();
>
> printf("%c",&ch_); // this will always print a blank -- why?
> //printf("%d",&ch_); // this will always print -1073744410 -- why?


You're giving printf() the address of ch_ instead of its value.
moi

2006-05-20, 1:15 pm

Wayne C. Morris wrote:
> In article <e4ndgg$rik$1@news.yaako.com>, "key9" <iamkey9@126.com> wrote:
>
>
>
> [snip]
>
>
>
> You're giving printf() the address of ch_ instead of its value.


Plus: you have two variables, one called "ch", the other "ch_" .

HTH,
AvK
key9

2006-05-21, 1:18 am

omg.
thank you very much!


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com