unix "getch()" question ,why my not print right char?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > unix "getch()" question ,why my not print right char?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-20-06 06: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







[ Post a follow-up to this message ]



    Re: unix "getch()" question ,why my not print right char?  
Wayne C. Morris


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-20-06 06: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.





[ Post a follow-up to this message ]



    Re: unix "getch()" question ,why my not print right char?  
moi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-20-06 06: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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-21-06 06:18 AM

omg.
thank you very much!







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:56 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register