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

C++ do...whille loop duplicate prints with getchar function

The code below is printing "Menu" twice when input is different from 3. do{ puts("Menu"); option = getchar(); } while (option != '3');
rplaurindo
  • 1,277
  • 14
  • 23
-1
votes
3 answers

Using getchar() to store numbers

I have an assignment where I'm supposed to make a simple arithmetic calculator in C that takes an input of a number, an operator, and a second number and performs the operation and outputs it. Sounds pretty simple right? I'm limited to using…
OmriSama
  • 302
  • 5
  • 11
-1
votes
1 answer

How does getchar_unlocked() work?

My question is based on a CodeChef problem called Lucky Four. This is my code: int count_four() { int count = 0; char c = getchar_unlocked(); while (c < '0' || c > '9') c = getchar_unlocked(); while (c >= '0' && c <= '9') { if (c ==…
-1
votes
1 answer

C - Read from input stream, tab as spaces

I'm a bit stuck and am hoping that someone can take a quick look to find what I'm doing wrong. I want to have the tabs count as spaces for output and not tabs. In this case, I'm using 3 spaces = 1 tab. I'm assuming that it may be something to do…
user3594736
  • 91
  • 1
  • 2
  • 12
-1
votes
2 answers

how to extract specific characters from a string

i am working on a project in which i have to read data from a file and store that data into a string array. String array position "0" should have first 13 characters of the file, string array position "1" should have next 13 characters of the…
Asad
  • 49
  • 1
  • 5
-1
votes
2 answers

creating a getchar function with read( , , ) error

I wrote read this code from the K&R book. But i compile it i get an error: gcc: error: getchar.c: No such file or directory gcc: fatal error: no input files compilation terminated. Code: #include #include int…
HELP PLZ
  • 41
  • 1
  • 9
-1
votes
3 answers

K&R Exercise 1-9 solution C programming

The task is to make a C program that replaces multiple blanks with a single blank and I found this solution on another StackOverflow question: int c; while ((c = getchar()) != EOF) { if (c == ' ') { while ((c = getchar()) == ' ') …
TheEyesHaveIt
  • 980
  • 5
  • 16
  • 33
-1
votes
2 answers

Can anyone please explain this output with a slight modification in the usual getchar_unlocked function()?

I wanted to change the general getchar_unlocked program to print the position of space when the second number is entered.So i introduced 2 more variables r and a, such that r gets incremented every time a non space character is input and a gets r's…
APD
  • 159
  • 2
  • 12
-1
votes
3 answers

Struggling with getchar() function in C

I am trying to replace multiple blanks with a single blank. #include #include #define MAXLINE 500 main() { char text[MAXLINE]; int i; for(i = 0; (text[i] = getchar()) != EOF && text[i] != 'x'; i++) { …
Jonas Hoffmann
  • 315
  • 7
  • 19
-1
votes
2 answers

Segmentation fault when checking if (char == '\n')

I have been struggling with this for over an hour now and I can't seem to find out why I get this error. int inp, count; char numBuff[21]; count = 0; while((inp=getchar()) != EOF) { // get Value (last field) printf("input is '%c'\n", inp); …
Geoff
  • 256
  • 2
  • 5
  • 20
-1
votes
5 answers

Not getting correct variable assignment from getchar in C

I'm writing a simple calculation program, however the only string handling functions I can use are getchar and putchar. Right now I'm just trying to assign the numbers from input to variables, but when I print the variable it's some random number.…
user1681673
  • 368
  • 1
  • 6
  • 28
-2
votes
1 answer

Function that gets char instantly without pressing enter

I'm new to programming, and I try to program a menu in C++. I want it to be in a while loop, breaking when ESC is pressed, but I want the character to be read instantly without having to press Enter. while (breaker != 27) { //menu based on…
Radek
  • 11
  • 1
-2
votes
2 answers

Why the characters in buffer cannot enter a while loop?

why there is no "*" in output? the input is : abcde[enter key] #include int main(void){ char ch; while ((ch=getchar( ))== 'e') printf(" * "); return 0; } I was wondering that the abcd'\n' will be stored in buffer…
pipi
  • 53
  • 3
-2
votes
1 answer

Why doesn't getchar() wait for input?

I'm writing a Brainfuck interpreter in C. When my program encounters a , in the Brainfuck program, it runs getchar() and saves the return value in the current memory cell. However, when getchar() is run, it immediately returns EOF. I think this…
user14189755
-2
votes
1 answer

remove extra characters from a string line in c

I had an assignment to write a program that takes 2 arguments as string by getchar and saves in variables. The 2nd string defines the length of the 1st string, which means that if the first string has 23 characters and the 2nd one has 13, the code…
Rosha. R
  • 31
  • 5