Solved

strip carriage returns with getchar?

Posted on 1997-05-01
9
478 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now