Questions tagged [getchar]

Anything related to C or C++ standard library functions `getchar` (C) or `std::getchar` (C++). These functions are used to read a single character from the standard input stream `stdin`.

Anything related to C or C++ standard library functions getchar (defined in <stdio.h> C standard header) or std::getchar (defined in <cstdio> C++ standard header). These functions are used to read a single character from the standard input stream stdin.

See CPPreference.com:

926 questions
4
votes
3 answers

Wait for press enter in C inside a while loop?

I'm writing a C program and I need to wait for the user to press any key to continue. When I use getchar(); it waits for the Enter key to be pressed. But when I use it inside a while loop, it doesn't work. How can I make my code wait for any key to…
H.R. Shadhin
  • 156
  • 1
  • 4
  • 12
4
votes
3 answers

Input string with getchar

I am trying to read a string into a char array with a length chosen by the user. The problem is that getchar() doesn't stop reading until the user manually enters a newline by pressing enter, based on my code. I have read through other's threads,…
LukeFi
  • 51
  • 1
  • 1
  • 5
4
votes
2 answers

Command prompt closing forcefully

I am a newbie to C. I typed the following code in Visual Studio Express Dekstop 2014 edition but the output is forcefully closing in command prompt. I tried to add getchar(); not once not twice but thrice also but nothing changes. I also tried to…
Coldplay
  • 133
  • 2
  • 2
  • 8
4
votes
1 answer

Confusion about how a getchar() loop works internally

I've included an example program using getchar() below, for reference (not that anyone probably needs it), and feel free to address concerns with it if you desire. But my question is: What exactly is going on when the program calls getchar()? Here…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
4
votes
4 answers

Why is getchar() not working for me?

I've just started programming c and I'm working through The C Programming Language by Brian W.Kernighan and Dennis M.Richie. One of the first examples is character counting and the following program is given, but when I enter a string no result it…
Nicolas
  • 75
  • 4
4
votes
3 answers

How do I get a program using getchar to run?

I'm a total C n00b trying to teach myself C off K&R. My question is kind of embarrassingly elementary. OK, here goes: I can't get programs using getchar to give the kind of output I expected. If you happen to have K&R on hand, I'm stuck on exercise…
Bad Request
  • 3,990
  • 5
  • 33
  • 37
4
votes
2 answers

Detecting key press using getchar()

When I run the following program: int main() { getchar(); return 0; } And I press any of the arrow keys like ↑ (up arrow) on the console I get ^[[A. I want to know what this means. Specifically, I want to know what ^[ means.
Alex_ban
  • 221
  • 2
  • 11
4
votes
4 answers

print multiple lines by getchar and putchar

Im a beginner learning The C Programming language and using Microsoft visual C++ to write and test code. Below program in C from text(section 1.5.1) copy its input to its output through putchar() and getchar(): #include int main(void) { …
user2593692
  • 43
  • 1
  • 4
4
votes
3 answers

C++ getchar() is there data still waiting to be read

I am implementing a key reader program in c/c++. I am using linux. I know that the unbuffered getchar function will return little data values of keys. For all ASCII keys (a-z, A-Z, 1-9, punctuation, enter, tab, and ESC) there will be a single value…
Blue Ice
  • 7,888
  • 6
  • 32
  • 52
4
votes
2 answers

how to handle the input buffer in c

I'm new to c programming and I'm facing this problem with my program I have a loop that gets a char form the input buffer while(c = getchar()){ if(c == '\n') break; if(c == '1') Add(); if(c == '2') getInput(); // this is where the…
4
votes
3 answers

Reading three characters per loop iteration in C?

This is making no sense to me, but hopefully one of you understand why it's doing this. I have an assignment that requires three characters be read with getchar() as the three integers next to one another are relevant to each other, so I set up a…
user1661781
  • 327
  • 2
  • 16
3
votes
2 answers

How can I generate an EOF (or an ASCII 0) in a visual studio debug console?

I have a console-mode program running on Windows. The program calls getchar() in a loop unitl either an EOF or a 0 is returned. I'd like to enter one of the following as a test vector while running the debugger: "abc\0" or "abc\EOF I can't…
AShelly
  • 34,686
  • 15
  • 91
  • 152
3
votes
4 answers

strange behavior of printf() inside a while loop

Can some one explain me why I see a double input of the printf() function the while loop: #include #include int main(){ int x = 0; while ( x != 'q'){ printf("\nEnter a letter:"); x=getchar(); …
oz123
  • 27,559
  • 27
  • 125
  • 187
3
votes
1 answer

Is there an elegant way to handle the '\n' that gets read by input functions (getchar(), fgets(), scanf()) in C?

I am trying a simple exercise from K&R to append string2 at the end of string1 using pointers. In case of overflow i.e. buffer of string1 can't contain all of string2 I want to prompt the user to re-enter string2 or exit. I have written the…
limbo1927
  • 55
  • 4
3
votes
1 answer

why 'scanf' & 'getchar' adds newline to the input?

I'm trying to test this simple look up table, but can't understand why both scanf and getchar add the newline character ('\n') to the input: const int arr[10] = {1,0,5,7,6,4,8,2,9,3}; char digit; printf("enter digits please\n"); digit =…
Ramonsito
  • 31
  • 1