FAQ Database Discussion Community


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

c++,loops,duplicates,do-while,getchar
The code below is printing "Menu" twice when input is different from 3. do{ puts("Menu"); option = getchar(); } while (option != '3'); ...

Wait for press enter in C inside a while loop?

c,while-loop,keypress,getchar
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...

skipped trouble with getchar and scanf

c,ubuntu,scanf,getchar
I recently started to program in C and im having trouble with this code: #include <stdio.h> #include <stdlib.h> #define PI 3.1416 int main () { float x; int y; x = PI; printf("Enter y: "); scanf(" %i", &y); printf("The new value of y is: %i.\n\n",y); x = x * y;...

K&R 1-9 // using getchar in a loop inside a loop

while-loop,eof,getchar,kernighan-and-ritchie
I have this simple program: main() { int c; while ((c = getchar()) != EOF) { if (c == ' ') { while ((c = getchar()) == ' '); putchar(' '); if (c == EOF) break; } putchar(c); } } As I understand it, there are two loops. The condition...

getchar() in assembly language

c,gcc,assembly,getchar
This is a simple C program: #include <stdio.h> int main(){ getchar(); return 0; } and this is the output of gcc command "gcc prog.c -S": .file "prog.c" .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 andl $-16,...

getchar() not working in c

c,while-loop,char,scanf,getchar
getchar() is not working in the below program, can anyone help me to solve this out. I tried scanf() function in place of getchar() then also it is not working. I am not able to figure out the root cause of the issue, can anyone please help me. #include<stdio.h> int...

Get just one char from long keypress python curses [accepted] [closed]

python,keypress,ncurses,curses,getchar
Hi I'm writing a program in python curses and I need to get just one char from a long keypress. (In other words what I need is that if i keep pressing down a key my program just has to get the char with the function getchar() just once). I...

Verify that the expression getchar() != EOF is 0 or 1 [duplicate]

c,eof,getchar
This question already has an answer here: Why doesn't getchar() wait for me to press enter? 9 answers Problem Verify that the expression getchar() != EOF is 0 or 1. Approach I have tried to write a program which will first take an input other than EOF and thus...

Why is getchar() being skipped?

c,io,mingw,mingw32,getchar
This is my code below, which I was working on. The output is this: Enter Nums: 20 4 OP: Which option was that? The op = getchar(); part is being entirely ignored. Why? I'm using gcc 4.6.2 MinGW. #include <stdio.h> int add(int num1, int num2) { return num1 + num2;...

Why doesnt getchar() stop reading strings in C?

c,string,input,scanf,getchar
I want the user to be able to input a number n and then the program will read n strings from the user. But now the program wont stop reading strings. #include <stdio.h> #define STRING_MAX 10 int main (void) { char string[STRING_MAX]; int count; int total; int n, i; int...

Scanning values in C till hit a new-line char, '\n'

c,arrays,algorithm,scanf,getchar
How can I scanf() the integer values I enter into an array until I hit enter. I believe I can use getchar() != '\n'. but how do I loop through the line ? Suppose my input is 20 21 2 12 2. I want an array that has all those...

Exit loop when 'Q' is entered

c,while-loop,getchar
I want to have a program in c where user is asked for input at the beginning of the loop and end the while loop if the user hits Q. I am in search of efficient code and without fflush() call as well.[I consider user can input 'a', 'abc', 'ab2c'...