Solved

strip carriage returns with getchar?

Posted on 1997-05-01
9
486 Views
Last Modified: 2012-05-07
I currently am using gets but keep getting a message saying that it is unsafe. I found another way to get keyboard input with getchar but it also takes the carriage return causing me to miss the next question. How do I get rid of the carriage returns?

Example:
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
 char answers[1];
 char answer2[1];
 
 int  i;
 
 for (i=0; i<1; ++i)
  {
   printf("Enter (y/n)" );
   answers[i] = getchar();
  }
 
 for (i=0; i<1; ++i)
  {
   printf("Enter (y/n)" );
   answer2[i] = getchar();
  }
 
 printf("You answered: ");
 for (i=0; i<1; ++i)
   if (answers[i] == 'y')
     printf("yes, ");
   else
     printf("no, ");
 
 printf("\n");
 
 return EXIT_SUCCESS;
}

0
Comment
Question by:drewg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 4

Accepted Solution

by:
emmons earned 25 total points
ID: 1250198
Use getch() instead of getchar()
It seems to do the trick.


0
 

Author Comment

by:drewg
ID: 1250199
When I use getch I get this compile error.

 Undefined symbol _getch referenced from text segment
0
 
LVL 4

Expert Comment

by:emmons
ID: 1250200
What compiler are you using?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:drewg
ID: 1250201
What ever one that comes with BSDI v2.1
cc is what I am using.
I found a getch utility in one of Ritchie's books that goes like this:

char buf[BUFSIZE];      /* buffer for ungetch */
int bufp = 0;           /* next free position in buf */
 
getch() /* get a (possibly pushed back) character */
{
        return((bufp > 0) ? buf[--bufp] : getchar());
}
 
ungetch(c)      /* push character back on input */
int c;
{
        if (bufp > BUFSIZE)
                printf("ungetch: too many characters\n");
        else
                buf[bufp++] = c;
}

I included it in my original program and I still have the addition line feed.


0
 
LVL 4

Expert Comment

by:emmons
ID: 1250202
I am not sure what is going on with BSDI.
From what I am reading here, getch() should always read exactly one character from the console. It should not even want a newline. It reads the character and moves on.
I just tried the same piece of code in Borland and Microsoft, and both act the same way. If you use getchar() it eats the newline, so I would say that the code in Mr.. Ritchies book is (and it pains me to say this) not completely correct.
I am looking at the source code (which is 28K and has a lot of MS specific stuff in it, and it specifically talks about "RAW" input from the keyboard) for getch(), and at the include files which define getc() and getchar():
#define getc( _stream) ->_cnt >= 0 ? 0xff & *(_stream)->_ptr++ : _filbuf( _stream))
#define getchar() getc(stdin)

0
 

Author Comment

by:drewg
ID: 1250203
Sorry, you are now getting ahead of my abilities. Iwas only looking for a simple way to input character.

What about fgets?


0
 
LVL 4

Expert Comment

by:emmons
ID: 1250204
fgets( buffer, 2, stdin)
works ok on my machine, but you do have to hit ENTER after the letter that you type in.
0
 

Author Comment

by:drewg
ID: 1250205
Thank you.
0
 
LVL 4

Expert Comment

by:emmons
ID: 1250206
You are welcome. Good luck!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question