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

Why should I use getchar() like this in C code?

while(1) { printf("1. ADD\n2. VIEW LIST\n3. QUIT\n"); int menu = 0 ; menu = getchar()-48; getchar(); switch(menu) { case 1: addition(book,count++); break; case 2: viewList(book); break; …
Codingdumb
  • 11
  • 7
-2
votes
1 answer

getchar() workaround in c/c++ in order to redirect output

I have an executable which is written in c++ and i run it in cmd.exe on Windows When i run this executable and by looking at the source code, i can see that a use of getchar() function is made - The program asks the user to "click any key to…
Raz
  • 489
  • 3
  • 8
  • 17
-2
votes
3 answers

output Fibonacci number with getchar?

I have this assignment due today but i don't quite understand this question. Write a program that outputs Fibonacci numbers. This part I understand I have this it lets you input a number and it'll create a fubonacci sequence of that length. typedef…
-2
votes
4 answers

How do I save integers read by getchar() to an array?

I am trying to make a program that will use getchar() to read three integers and store it in an array but something is wrong in my code. #include int main( ) { printf("Please enter three digit number 100 to 999: "); int…
-2
votes
2 answers

Segmentation failed (core dumped), C

I have the following programme: #include #include #define MAX 100 //a) bool isPalyndrom(char c[], int cnt) { int i; char pal[MAX]; int j = 0; for (i = cnt - 1; i >= 0; i++) { pal[j] = c[i]; …
life_of_pi
  • 13
  • 4
-2
votes
3 answers

i get confused whit get character from user

I have 2 questions: Question 1= i search in the internet and i found that everyone told don't use getch() or getche() in c++. so how can i get a character from user ?? for example i wrote this code so how can i replace getch with another statement…
ssaeidd
  • 1
  • 4
-2
votes
3 answers

What is the getchar() used for in this c code?

struct Empregado v_empregados[10]; main() { int i; for(i=0; i<10; i++){ printf (“Nome e apelidos? “); gets (v_empregados[i].nome); printf (“Idade? “); scanf(“%d”,&v_empregados[i].idade); …
user5470811
-2
votes
1 answer

why I can't get a string from getchar()

I want to get a string by calling function getchar(), but there is something wrong. Below is my code int i, j, t; char a[N], *p = argv[1]; for (i=0; i<5; a[i]=t, i++){ if ((t = getchar()) == EOF) break; } a[i] = 0;
-2
votes
2 answers

How to Read Until Newline

So I feel like I don't really understand the getchar() function very well... What I thought the code would do is, if there happens to be a space it will just "eat" that space with the getchar() function. And if there happens to not be a "p" in the…
-2
votes
2 answers

Not taking input by getchar() or it might be error in do while?

My program below isn't taking input using getchar(). Instead, it ends after printing, "want to continue??(press y for yes and press n to reenter)". It doesn't take input after typing n or N. void main(){ int i,arr[]={55,10,23,11,35,8,9,20},n; …
-2
votes
3 answers

Program is not giving the expected output

#include #include int main() { enum loop {NO ,YES}; enum loop okloop = YES; int i=0; char s[8]; int lim=6; char c; while (okloop==YES) { if (i>=lim-1) okloop=NO; …
shubhamm
  • 15
  • 3
-2
votes
1 answer

The program control flow doesn't work as expected

This is a problem in C. The program Control flow is not as expected. It ask to enter the character in but fail to ask to enter character x. int foo(); int main(int argc, const char * argv[]) { foo(); return 0; } int foo(){ char…
-2
votes
2 answers

can't compare getchar != '/n' , get warning: multi-character character constant

When I compare getchar() != '/n' I get the warning: multi-character character constant. I wounder why? I code in C and use gcc as compiler. I thought that getchar read in one character and converted it to int for itself. So what's the meaning of…
-2
votes
2 answers

What does this do : while((c= getchar()) != '\n' && c != EOF);

while((c= getchar()) != '\n' && c != EOF); I was facing a problem by using gets() to enter strings. I found on google that it was keeping the value of '\n' in the input buffer. I searched here and found the above code to solve my problem. However I…
Adi
  • 89
  • 1
  • 1
  • 13
-2
votes
2 answers

What is the practical use of getchar and putchar?

What are the practical uses of these functions? I mean if we use them using while loop and EOF we get just what we type like if we give input "hello" we will get the output as "hello". So what's the point?