Solved

strip carriage returns with getchar?

Posted on 1997-05-01
9
489 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
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!

 

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

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!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

617 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