Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-22-2003, 03:25 PM
|
#1
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
getchar() question
C code:
Code:
char a, s, *p;
int i, j;
struct menu selection;
printf (" Enter algorithm number: ");
a = getchar();
p = &a;
j = (int)atoi(p);
if (j > i+1 && j <= 0) {
printf ("Invalid algorithm selection : Run again\n");
exit (1);
}
else
selection.algo_num = j-1;
printf (" Enter sort order [(a)scending || (d)escending)]: ");
s = getchar();
if (s != 'a' && s != 'd') {
printf ("\nInvalid selection of sort order: Run again\n");
exit (1);
}
else
selection.sort_order = s;
when i run this, i get a 'stop' at "Enter Algorithm number: ", and it reads in the character.
At the next printf, after printing " Enter sort order [(a)scending || (d)escending)]: ", it doesnt 'stop' to do the getchar();
why would this be?
thanks again!
|
|
|
12-22-2003, 03:48 PM
|
#2
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
the newline is still in the input stream from the last getchar(). remember stdin is really just a big ol stream of bytes. every time a user pushes [enter] that is read as a newline. so you could fflush(stdin) if u wantd.
|
|
|
12-22-2003, 03:50 PM
|
#3
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Original Poster
Rep:
|
hmm, thanks again infamous.
i just removed the getchar(), and used scanf() instead.
thanks.
|
|
|
12-23-2003, 05:53 PM
|
#4
|
Member
Registered: May 2002
Posts: 964
Rep:
|
To get somethuing like the DOS getch() function where you push a key and the value is returned without having to hit return:
Code:
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
/*------------------------------------------------*/
int getch(void) {
int c=0;
struct termios org_opts, new_opts;
int res=0;
//----- store old settings -----------
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
//------ restore old settings ---------
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
return(c);
}
|
|
|
All times are GMT -5. The time now is 06:33 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|