Solved

strip carriage returns with getchar?

Posted on 1997-05-01
9
484 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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

828 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