0

I just want to read in a char from stdin, if it is a space, do something, if it is a newline, do something else. What is happening with my current code is if I enter a space, then hit enter, the ' ' case is satisfied which is what I want, but the '/n' case is also satisfied after that. I don't want to read in the space and the newline, all I care about is the space. How do I fix this? Here is my code:

int input = getc(stdin); switch (input) { case 'q': return 1; break; case ' ': printLines(fp); break; case '\n': printLine(fp); break; default: getResponse(fp); break; } 
2
  • 1
    The code you have shown will behave as you wish. The problem is in the code surrounding it. Please show a complete test program that we can compile, run, and observe the same phenomenon you are. Commented May 29, 2013 at 0:50
  • May I recommend that you accept an answer from the ones below? Or you can update your question if none of the answers have addressed your problem. Commented May 30, 2013 at 11:34

4 Answers 4

1

If you are reading each user provided space or q in a seperate line, thus expecting a newline after each character, remember to discard it after parsing the character:

int input = getc(stdin); switch (input) { case 'q': getc(stdin); return 1; case ' ': getc(stdin); printLines(fp); break; case '\n': printLine(fp); break; default: getResponse(fp); break; } 

Note that with this solution, if the user enters two characters followed by enter, the program will discard the second character but process the first one and the enter. Thus you may need to implement discarding not only the second character but the whole line.

Sign up to request clarification or add additional context in comments.

Comments

1

Assume the following is your input to stdin:

a\n b\n \n c\n 

In order to distinguish whether or not a "line" of input is empty (only a '\n') or an actual character was input (a character followed by a '\n') you could either:

  • Store the previous char read from stdin, and if it was '\n' you could assume that an empty line was input (with the exception for the first time taking input). If not, you're still on the same line you were previously reading.

  • Continue reading from stdin after reading a char until you read the '\n', so that when you take input next time you won't read a '\n' unless an empty line was input.

Comments

0

Not sure if I understood your problem but this will print 1 if input contains ' ' (even if there has been a '\n' afterwards) and 2 if input has '\n' but no ' ':

#include <stdio.h> int foo() { return 1; } int bar() { return 2; } int main() { char input = 'c'; while(input != ' ' && input != '\n') input = getc(stdin); (input == ' ') ? printf("%d\n", foo()) : printf("%d\n", bar()); return 0; } 

The first time I typed some words separated by space and then Enter, the second time the input deliberately lacked a ' '.

$ ./a.out test test 1 $ ./a.out asdsadasdasdasdsadasddsda 2 $ 

5 Comments

You are missing to initalise input prior to accessing it the first time. This is Undefiend Behaviour, anything could happen. Consider modding input's declaration like this: char input = 0;. Alternativly you could change the while() {}; loop to become a do {} while(); loop.
It's not only "good practise", in the case of the answer's code it is essentially necessary to guarantee the code does what it should!
I agree that initialising it to something first is the correct way to write it and guarantee its behavior, how is this not good practice :) Apologies if my semantics are confusing, I'll remove the comment.
OT (english phrases): "It's not only good ..."" means: It is good, but that is not the only thing it is, it is also at least one other thing, in case of my usage of this phrase, it is also "essential necessary to guarantee defined behaviour ..." Ok?-)
I guess this looks like a good opportunity for me to link an SO post on variable initialisation in C, the OP may be interested :) stackoverflow.com/questions/1597405/…
-1
scanf("%c\n",&c); if(c==' ') { //the input was a space } if(c=='\n') { //the input was a newline } 

1 Comment

as far as I understood the problem, the input contains a character which may be either ' ' or '\n' and after that the user will hit enter on the keyboard. To consume that enter keypress, I have added that extra '\n'. Maybe thats not what was needed, but many times this causes problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.