LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-22-2003, 03:25 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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!
 
Old 12-22-2003, 03:48 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-22-2003, 03:50 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
hmm, thanks again infamous.
i just removed the getchar(), and used scanf() instead.
thanks.
 
Old 12-23-2003, 05:53 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() satellite Programming 10 12-24-2017 02:47 PM
Noob question about putchar and getchar zombi3 Programming 2 12-20-2003 05:38 PM
about getchar() captainstorm Programming 6 10-11-2003 05:14 AM
getchar() NSKL Programming 18 01-11-2003 11:19 AM
getchar() Winter Programming 6 05-11-2002 02:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration