Solved

strip carriage returns with getchar?

Posted on 1997-05-01
9
481 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

803 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