Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The program should give output in the following way:
Enter a character
g
Successfully entered!
g
#include <stdio.h>
int main() {
int c,d ;
printf("Enter a character\n");
c = getchar();
printf("Successfully entered!\n");
putchar(c);
return(0);
}
But when it is run, it waits for the user to enter the input before asking for it and then prints out in the following way:
g
Enter a character
Successfully entered!
g
You may put a fllush after the printf before getchar
printf("Enter a character\n");
fflush(stdout);
c = getchar();
fflush is meant to be called on an output stream. This is an excerpt from the C standard:
int fflush(FILE *ostream);
ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to use the symbolic constant in the condition or test of a conditional loop. Here is what I tried:
#include <stdio.h>
#define TEST 0
void main() {
int c;
while ((c = getchar()) != TEST)
putchar(c);
}
I want to exit the program when I enter 0 as input. But it keeps copying my input even if I enter a 0.
Can you guys help me understand why this happens?
If you want the program to stop when you type 0, you should compare the return value of getchar() to the character constant '0' instead of the number 0. Note that you should also check for EOF to avoid an infinite loop at end of file:
#include <stdio.h>
#define TEST '0'
int main() {
int c;
while ((c = getchar()) != EOF && c != TEST)
putchar(c);
return 0;
}
Note however that if the program reads from a terminal, because the input is buffered both by the standard stream and by the terminal, the '0' byte will be be made available to the program until you hit the enter key.
Also note that the program will store on the first 0 byte, neven if this byte is part of a number such as 1024 or a word Hell0.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When running the program and entering the text, it will ask me again and again to insert the input, until I give an input of one character only.
NOTE: it isn't in any sort of cycle loop, I don't know what the problem is. (I'm using cs50 library for the get char).
char nameless[] = { get_char("Insert the text here: ")};
char nameless[] = {get_char("Insert the text here: ")};
If you look up get_char(), it clearly states
Prompts user for a line of text from standard input and returns the equivalent char; if text does not represent a single char, user is reprompted.
You need to enter only one character. Not a line of characters or 'text' as you call it.
Enter char: a
That's it.
Here's a working code example:
#include <cs50.h>
int main(void)
{
// attempt to read character from stdin
char c = get_char("Enter char: ");
// ensure character was read successfully
if (c == CHAR_MAX)
{
return 1;
}
char next = get_char("You just entered %c. Enter another char: ", c);
if (next == CHAR_MAX)
{
return 1;
}
printf("The last char you entered was %c\n", next);
}
You can look it up at CS50 Library for C
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The first ScanF takes both inputs when I type a character and an integer is expected.
see image below:
As you can see when I type "55ape" for ScanNum_A it sets ScanNum_B = 0.
Why does this happen?
It happens because
ape is still left in the input stream and your second scanf tries to read it but fails as it was expecting integer but found chars.
You can clear the input buffer after your first scanf as below.
//first scanf
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
//second scanf
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I’m starting to learn from "The C Programing Language" and one of the codes in the book is not working for me. This code suppose to count the number of characters using getchar().
Here is my code:
#include <stdio.h>
int main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
return 0;
}
I try to run it and write some characters but when I press ENTER, it only starts a new line. It's like it’s never getting out of the loop.
A newline is not an EOF. You’re confusing EOF and EOL.
When your press ENTER, getchar() receives a newline: \n, and your program counts it just like any other character.
Try pressing CTRL + D (Linux terminal) or CTRL + Z (Windows terminal) to send an empty input to your program, thus ending it.
You can also write your input to a file, and give this file to your program as input, like this:
./your_program < your_file
When your input comes from a file, an EOF is automatically sent to your program when reaching the end of the file. That’s because there is not more output to get from the file.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c", &i);/* i know this must be reversed for next input but I want to know the impact of this code if we use scanf type char for integer data type*/
scanf("%d", &ch);
printf("%c %d", ch, i);
return 0;
}
No Error but why don't get input for second scanf() statement
You declared i to be an int and ch to be a char
In your scanf you have %c for i and %d for ch while %c is for chars and %d for ints.
So just turn them around and you'll be fine.
With printf you have the correct format.