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
3
votes
2 answers

C Program closes after first getchar()

Why does the following snippet closes after the first input?: #include int main( ) { int a; int b; printf( "Enter a first value :"); a = getchar( ); printf( "You entered: "); putchar( a ); printf( "\n Enter a second…
max fraguas
  • 438
  • 1
  • 6
  • 12
3
votes
0 answers

Redirect C stdin to read from C++ stringstream

I have some code that was provided to me written in C that I'm using in a C++ project. I need to add some functionality, along with unit tests. Unfortunately it's hard coded to use getchar and other C IO functions in a way that isn't very easy to…
anderspitman
  • 9,230
  • 10
  • 40
  • 61
3
votes
3 answers

Need help understanding how to use get char to type out sentences in terminal

#include #include int row; int col; int input; int offset; char ascii[7][580] = { " * * * * * * ** ** * * * * *** ***** ***** ***** ***** ***** *****…
Dominic Baker
  • 51
  • 1
  • 10
3
votes
4 answers

C - getchar() doesn't read the second character of an input correctly

I am trying to use getchar() to read all the digits of an input number and store them in an array. But every time I run the program something goes wrong with the second digit. Here is my code: int ch = 0; int digits[0]; int i = 0; while ((ch =…
yixiaoyx
  • 63
  • 6
3
votes
2 answers

Having trouble counting lines in output

Here is the code : #include int main(void) { int nextChar; int numLines = 0; while ((nextChar = getchar())!= EOF) { if (nextChar == '\n') { ++numLines; } } printf("The\nsky\nis\nblue\n"); …
MDrake
  • 39
  • 2
3
votes
2 answers

String input using getchar()

The following code uses getchar() to accept a line of input. #include #include int main() { char *rawString = (char *)malloc(200*sizeof(char)); char *rawStringInitial = rawString; char c; c=getchar(); while(c!='\n') { …
user191776
3
votes
2 answers

Why does the Linux(?)-Terminal not "consume" '\n' EOF?

From what I've read, the Linux terminal (in default settings) buffers the input and only sends it after receiving either EOF or '\n'. When I loop c = getchar(); and check each c for being EOF (end then break) I need to do CTRL-D twice in order to…
ljrk
  • 751
  • 1
  • 5
  • 21
3
votes
5 answers

getchar() continues to accept input after including Ctrl+Z in same line

Simple c program to accept and print the character. int c; while((c=getchar())!=EOF) { putchar(c); } I am not getting why it accept input when I press Ctrl+Z at the end of line ex Hello(press Ctrl+Z) hello (some symbol) but it work properly…
Flicks Gorger
  • 195
  • 1
  • 1
  • 9
3
votes
5 answers

Loop through user input with getchar

I have written a small script to detect the full value from the user input with the getchar() function in C. As getchar() only returns the first character i tried to loop through it... The code I have tried myself is: #include int…
mysql lover
  • 49
  • 1
  • 1
  • 3
3
votes
2 answers

Understanding getchar() in the character counting program in C

This is a follow-up question of my previous question. There is already a similar question asked(question). But I don't get what I want to know from that answer. From the previous question I come to know that if I type a lot of characters, then they…
user31782
  • 7,087
  • 14
  • 68
  • 143
3
votes
4 answers

getchar() in C is completed without pressing Enter

From my previous post, I come to know that getchar() completes only when we press Enter. Let's consider this code: #include main() { getchar(); getchar(); getchar(); getchar(); getchar(); } I expected it to run like this: I…
user31782
  • 7,087
  • 14
  • 68
  • 143
3
votes
5 answers

Removing multiple blanks using putchar and getchar in C

Problem: Write a program that receives textual input using getchar() and outputs the string, having removed multiples blanks. Here's how I wrote the pseudo-code: While each input character is received before reaching EOF, do the following: 1)…
Omid
  • 2,617
  • 4
  • 28
  • 43
3
votes
1 answer

How does the STDIN buffer and getchar() pointer change during successive calls?

Given input in the stdin buffer, when successive calls to getchar() are performed, does the pointer move along the memory address of the stdin buffer, allowing getchar() to retrieve the value at each address? If so, once they have been retrieved are…
jma1991
  • 345
  • 1
  • 3
  • 15
3
votes
5 answers

Why does this getchar() loop stop after one character has been entered?

#include int main() { char read = ' '; while ((read = getchar()) != '\n') { putchar(read); } return 0; } My input is f (followed by an enter, of course). I expect getchar() to ask for input again, but instead…
Pieter
  • 31,619
  • 76
  • 167
  • 242
3
votes
1 answer

Strange C behavior occured when getting multiple characters from an nonblocking IO using getchar() and select()

I'm doing some experience on unix nonblocking IO programming. I put a select() and getchar() function in an infinite loop, expecting that if I input multiple characters to stdin and press enter, all of this characters will be printed out one by one…
Nmzzz
  • 2,462
  • 3
  • 19
  • 18