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 Programming: getchar() won't accept '+' or '-' for input, but will accept '*' and '/'?

I'm trying to collect an operand (+,-,*,/) from input. When I attempt to do so, the * and / input is accepted, and the code works. When I input + or -, the default exception is thrown. What is going on?! Is there some sort of problem with the + or…
j-grimwood
  • 351
  • 3
  • 7
  • 19
-1
votes
1 answer

scanf_s not working what is the error? can any one solve this

#include int main(void) { int sum(), sub(), mul(), div(); char ans; printf("\ta) Add\n"); printf("\tb) Sub\n"); printf("\tc) Multiply\n"); printf("\td) Divition\n"); scanf_s("%c",&ans); printf("\nYour answer…
Deepak Play
  • 263
  • 2
  • 11
-1
votes
2 answers

Remove new line character from console

I have this loop, but when i hit enter after my character, it processes it and then processes the '\n' before asking for input again. Please!!!! Help int input; while (true){ input = getchar(); fflush(NULL); input =…
i_use_the_internet
  • 604
  • 1
  • 9
  • 28
-1
votes
1 answer

Extra/missing chars in scanf

this is my input function: int input( int array[500][500], int *x0, int *y0 ) { int x = 0; int y = 0; int err = 0; int temp = 0; char c; char s[2]; printf("Enter sizes:\n"); if (scanf("%d %d%c", x0, y0, &c) !=3 || …
-1
votes
2 answers

I don't get an output when I compile a program having getchar(), etc on Linux Mint on the terminal window

I'm relatively new to Linux Mint and somewhat trying to get back at programming. I'm trying to learn this concept of reading, copying,counting using C Programming by Brian W. Kernighan and Dennis M. Ritchie. I understood the concept of line…
-1
votes
1 answer

How i can stop reading values after EOF

I have little problem with my code. I try to make 'while' loop which will read all my input values and then stop when i clicked EOF. I have read that EOF in windows is CTRL+Z and so on but my 'while' doesn't want to stop and after all input values…
-1
votes
2 answers

I can't read the second string, no matter how many getchar i insert

This program need to read two strings, this two strings will be passed to the "confirm" function, they will be read and the the function will have to find a word in common. But in the main i cant read the "string2" string! No matter how many getchar…
Mateus Luiz
  • 756
  • 7
  • 20
-1
votes
1 answer

getchar to read from command line arguments

I want to get my program to use the command line arguments with getchar to then encode a message. My problem is that getchar is only paying attention to what I type after the program has executed. How can I make it read the command line arguments…
-1
votes
1 answer

How to error check when entering a float in c?

I have a program that prompts the user for an integer that is not negative or character. I want to prompt the user for a float instead. How do i do this? do { printf("Enter a number.\n"); } while(((scanf("%d%c", &x1, &term) != 2 || term !=…
-1
votes
1 answer

C - reading from stdin with a file?

So I have a text file that I'm using in the same directory as my C program and I'm using MinGW as the compiler. This is my input: ./program "hello" > helloworld.txt In my program in the main function, I have: #include int main(int argc,…
Laefica
  • 489
  • 1
  • 5
  • 13
-1
votes
1 answer

Why isn't getchar exiting?

I wrote a program to try and print a 2D table containing a graphical representation of user input using a 2D array in C. I compile and run this program, but not matter how much I press Return or Ctrl+D for EOF, getchar does not appear to yeilding to…
John Doe
  • 1
  • 1
-1
votes
1 answer

How to remove multiple trailing newlines after fgets()?

#include #include #include #include int main() { int upper=0; int digit=0; int i=0; char s[30]; char c; printf("Enter sentence: "); fgets(s, 30, stdin); //s[strlen(s) - 1] = '\0'; while(c=getchar()…
JonSnow
  • 67
  • 3
  • 14
-1
votes
2 answers

Problems in C code

I am trying to make a simple calculator in Turbo C(I have my own reasons to why I use Turbo C now) #include #define P printf int loop[] = {1, 1, 1, 1}; int num; char input[64]; void main() { int num1, num2; char x, y; …
-1
votes
2 answers

I am using while loop to enter characters one by one(total number of characters to be entered is unknown). How can I store my characters in an array:

User has to input a string of unknown length(<1000). So here I am using while loop, #define, getchar. What should I do to store the characters simultaneously? #include #define EOL '\n' int main() { int count=0; char c; …
Sunil Kumar
  • 390
  • 1
  • 7
  • 25
-1
votes
2 answers

Difference between storing the value in int and char returned from getchar function in C

While going through the book by Dennis Ritchie , I found that it is better to storing the value returned by getchar() function in C in integer type variable rather than character type variable. The reason it stated was that character type variable…
Amisha Bansal
  • 11
  • 1
  • 4