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

C Programming: Name arranger! Coding newb?

Complete newbie here. 2nd day in my intro to programming class, so be gentle. We're programming in C btw. Our assignment was to prompt the user to give us a name in the format: John Smith, and then print it back out to them like so; Smith, J. To add…
user1145538
  • 641
  • 2
  • 9
  • 14
2
votes
5 answers

getchar() doesn't work well?

I wrote this code in C++, and I used getchar() to puase the console, but I did not see any effect of using that function, here is the code: #include #include//to pause console screen using namespace std; //function prototypes int…
Aan
  • 12,247
  • 36
  • 89
  • 150
2
votes
7 answers

C: using pointer as string: unpredictable behavior

I'm writing a C program to find the longest line in the user's input and print the line's length and the line itself. It succeeds at counting the characters but unpredictably fails at storing the line itself. Maybe I'm misunderstanding C's memory…
Jordan
  • 4,510
  • 7
  • 34
  • 42
2
votes
0 answers

How to read a char with Golang in Windows without requiring return key?

In my Go program which I run on MS Windows I want to get a confirmation for a 'delete file' action with the keys y/n/a, and it's going to be repeated a big number of times. And I do not want to have to press enter for each time. I've tried answers…
2
votes
1 answer

What is the use of 'getchar()' in this program?

The code is attached. int main{ int i, n; printf("This program prints a table of squares.\n"); printf("Enter number of entries in table: "); scanf_s("%d", &n); getchar(); for (i = 1; i <= n; i++) { …
2
votes
2 answers

C getchar loop to get integer input from user - [how to improve]

Is there more elegant way to do this task? Program asks user for integer and repeats if non-digital characters are entered. To exit loop two conditions expected: a) all entered characters are digits b) last character is '\n' Short solutions like…
YukaMax
  • 31
  • 3
2
votes
1 answer

getline function definition in K&R

The following is getline implementation in K&R on page 29 , # define MAXLINE 1000 int getLine(char s[], int lim) { int c ,i ; for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c!= '\n'; ++i) s[i] = c; if(c == '\n'){ …
2
votes
3 answers

Why does getchar() give \n even though the buffer is empty?

I have the following function that should loop and take multiple lines from the user (names and dates). The user needs to enter an empty line to stop the loop. while ((line = getLineFromUser()) != NULL) { token = getNameAndDate(&concert,…
Eli Freid
  • 33
  • 7
2
votes
2 answers

Little character counter program using getchar() returns twice as much as the expected result

How come this little program returns twice as much as the expected result ? int counter=0; while(getchar()!=EOF)++counter; printf("%d\n",counter); The goal of this program is to print the number of input characters it gets from the keyboard until…
Kode1000
  • 111
  • 3
2
votes
0 answers

counting number of characters from input

Below is my code to count number of characters. #include int main() { int count=0; /*better to initialise a large number */ int c; while(getchar() != EOF) { count++; } …
Suzanno Hogwarts
  • 323
  • 2
  • 3
  • 12
2
votes
1 answer

Why does getchar() return more than one character?

While trying different things with getchar I figured out that it usually only safes on character in an variable. Somehow when I use a while loop the behaviour changes and it returns more characters if the input is more than one. Here is my example…
Nizde
  • 21
  • 2
2
votes
1 answer

getchar() and input buffer

an example from a book: #include
Aiden Choi
  • 142
  • 7
2
votes
2 answers

Why does (while .. getchar()) does not write to my file, in C?

I need to write a program that asks the user to enter strings, each string ends when the user presses 'Enter'. The program needs to receive the file name as a parameter, the file should be opened and closed for each operation and for every string…
NoobCoder
  • 513
  • 3
  • 18
2
votes
3 answers

puts(), gets(), getchar(), putchar() function simultaneously use in the program

I have a confusion related to using puts(), gets(), putchar() and getchar() simultaneously use in the code. When I have run the below code, it is doing all steps: taking the input, printing the output, again taking the input, printing the…
Priyanka
  • 21
  • 4
2
votes
2 answers

Strange 10 value gets printed when I print inputed characters by their ASCII decimal code

#include #include int main() { int end; while(( end = getchar() ) != EOF ){ printf("%d\n",end); } system("pause"); return 0; } I want to print the ASCII codes of characters with this code but…