I working on a C project in Eclipse environment the code is correct and executable but the output
lines are not in order the program ask user to
enter a number from 1-5 then asks for a name then street
but nothing appear on console screen unless i entered these values
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char name[20], address[30];
char c;
printf("How do you feel on a scale of 1-5?");
c = getchar();
printf("Enter name: ");
scanf("%s", &name);
printf("Enter your address: ");
scanf("%s", &address);
printf("Entered Name: %s\n", name);
printf("Entered address:%s\n", address);
printf("You said you feel: ");
putchar(c);
return EXIT_SUCCESS;
}
The problem is that stdout is line buffered (when going to a console), so unless you print a newline character, the output will remain buffered and not be displayed (OK, there's going to be a maximum size that can be buffered put that's just detail, your small amount of output will remain in buffer).
The two solutions that occur to me are, use fflush (stdout); after your first 3 printf calls, this will cause the stdout buffer to be flushed to the console, and should resolve your problems.
You could also turn off buffering of stdout, see setvbuf for how to do this, but I think, placing this call near the start of main (before any output) should work (untested):
setvbuf (stdout, NULL, _IONBF, 0);
The problem is your second scanf is capturing the \n character of the previous input, try to use fgets in order to avoid this behaviour.
Related
When I try to run this code it just runs indefinitely. I have tried to use scanf_s but that did not change anything.
#include <stdio.h>
int main (void)
{
int height, length, width, volume, weight;
printf("enter height of box: ");
scanf(" %d", &height);
printf("enter length of box: ");
scanf(" %d", &length);
printf("enter width of box: ");
scanf(" %d", &width);
volume=height*width*length;
weight=(volume+165)/166;
printf("volume (cubic inches): %d\n", volume);
printf("dimensional weight (pounds): %d\n", weight);
return 0;
}
The first print in your code (and similarly the others):
printf("enter height of box: ")
By default, this line will put the text into a buffer. The buffer is written to terminal ("flushed") only if either one of the following happens:
The buffer gets full (can be 1024 bytes, 16384, or any other number).
There is a newline at the end of the text (assuming the output is to the terminal).
Note that this is a common behavior, but not guaranteed by the standard. Read Is stdout line buffered, unbuffered or indeterminate by default? to cover this subject.
Some C implementations will automatically flush stdout when you read from stdin, but many implementations don't.
Read Does reading from stdin flush stdout? to cover this subject.
So you can see your prompt if you change the line to:
printf("enter height of box: \n");
A second option, is to force a flush, so that the buffer is written to the output:
printf("enter height of box: ");
fflush(stdout);
A third option is to disable buffering completely. For that, you can use setbuf:
void setbuf( FILE* stream, char* buffer );
buffer -
pointer to a buffer for the stream to use. If NULL is supplied, the buffering is turned off. If not null, must be able to hold at least BUFSIZ characters
So, simply add the following at the beginning of the program, and all stdout buffering will be turned off:
setbuf(stdout, NULL);
there doesn't seem to be any mistakes in the code. i tried running it on Ubuntu And OnlineGDB. The Code is working just fine. It Can be your compiler's fault.
You should check what "scanf" is returning.
you can check it using this:- (here d is your integer)
if (scanf("%d", &d) == 1)
...all OK...
else
...EOF or conversion failure...
If you have several conversions, check that they all completed.
There may be a problem cause you might not be entering any value as input.
In all other cases, the code seems to work fine.
I wanna reproduce the terminal behavior when the input is just a new line (keeps printing the same string), but don't know how to do it.
Example: When the user just inputs a new line, the terminal keeps printing the directory, until a real command is inserted
int main()
{
char userInput[1024];
while (1)
{
printf("directory »» ");
scanf("%[^\n]" , userInput); // This scanf doesn't work
while (userInput[0] == '\n') // If the input is only a new line char, keep asking for more inputs and printing the directory
{
printf("directory »» ");
scanf(" %[^\n ]" , userInput); // This scanf doesn't work
}
//Input isn't a NewLine, process the input
process_Input_Function(userInput); //Isn't empty, search for my created commands
}
}
At the first enter press, it enters the loop, reproduce 1 time, and then the scanf doesn't detect new lines anymore, it just skips and waits to a real string.
What can i type inside of the scanfto detect a new line input and keep printing that string till a real command is inserted?
I tried with scanf("%c"...) but the problem with a char, is that i can't process the whole string command, if isn't empty
First of all, your two scanf calls are different. The first one is
scanf("%[^\n]", userInput);
which looks for anything that's not a newline, as you wish to do.
But the second one is
scanf(" %[^\n ]", userInput);
which is also looking for a space before the input, followed by any character that is also not a newline or a space. Thus, scanf is waiting for the space.
IMHO, the best way to recreate this behavior is going to be in the parsing step, after you have gotten the command from the command line. Essentially, your command input loop would look like this:
char *userInput = NULL;
size_t n = 0;
while (true) {
// print the prompt
printf(">");
// get the line
ssize_t userInputLength = getline(&userInput, &n, &stdin);
// parse the input, using a function you wrote elsewhere
parse(userInputLength, userInput);
}
(Note the use of POSIX getline() instead of scanf. This is a more recent standard library function that does exactly the task of getting a line of user input, and also allocates the buffer using malloc and realloc so that you don't have to care about buffer overflows or even sizing the buffer at all.)
The user input function wouldn't care that the userInput portion was blank. The function that would care is the parse function, which will simply interpret a blank userInput string as "do nothing" and continue on its merry way.
Hmm, the code I gave pretty much does that with one exception, it doesn't display a prompt each time...
Is this what you mean:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // For the memset()
int main() {
char userInput[1024];
while (1) {
printf("»»» ");
fgets(userInput, 1024, stdin);
while (userInput[0] == '\n')
{
printf(">>> ");
memset(userInput, '\0', 1024);
fgets(userInput, 1024, stdin);
}
// Your command can be accessed from here //
printf("Command entered: %s\n", userInput);
printf("Input isn't a NewLine\n");
}
}
I changed the scanf() to fgets() to read from stdin so that we don't overwrite the buffer.
so I was messing with the read functions fgets and scanf and with the printing functions write and printf with the following code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
printf("Enter an integer: ");
int n = 0; scanf("%d",&n);
printf("The input number is: %d\n", n);
printf("Please enter a string now: ");
char buffer[200];
read(0,buffer,200);
printf("The input string is: %s", buffer);
printf("which is: %s\n", buffer);
printf("Now please enter another message: ");
fgets(buffer,200,stdin);
write(1,buffer,200);
return 0;
}
I would get these errors:
1-After the first scanf, it won't just show me the message to input the string.
2-What I write now it's what it's going to be saved in the string.
3-It will skip the last fgets...
An example of output:
Which doesn't make any sense at all; I would like to get an output like this:
Enter an integer: 15
The input number is: 15
Please enter a string now: This is the message1
The input string is: This is the message1 which is: This is the message1
Now please enter another message: This is the message2
This is the message2
Thanks for your help!
Interesting question. Mixing standard I/O (scanf(), fgets()) and file descriptor I/O (read()) on the same underlying file descriptor (0 aka standard input; stdin as a file stream) is at best problematic. You will get odd-ball effects.
At the file stream level, there is some synchronization between stdin and stdout when the input comes from a terminal; pending output on stdout is often flushed by the library. When you use read(), there is no such synchronization. That's why the prompt doesn't appear until after you hit return.
When you type 1 for the number, you also supply a newline. Standard I/O buffers the newline; it is kept so that the next file stream operation can read it. Then you read a line with read(). This does not know about the standard I/O buffer, so it waits for a new line of input from the terminal. You need to capture how much data was read because the input was not null terminated; that's a service provided by the standard I/O library, not the low-level read() function.
When you then call fgets(), it reads the newline that was buffered (not processed by scanf() when reading the integer) and returns with the empty line.
Note that if you had any buffered output waiting on standard output (e.g. you had used printf("Beginning of line: "); with no newline), then the output from write() would appear before the information buffered on stdout.
Using fread() and fwrite() would give you direct binary I/O (no null termination, for example), but would use the same I/O buffers as functions such as printf() and fgets(). You get hybrid behaviour when using these — it is normally best to use either fread()/fwrite() or the text I/O functions on a single file stream, and not both, but mixing them is permissible, relatively simple to understand, and occasionally useful.
So, what you see is all explainable, but it is hard work doing the explaining. It is also a clear indication of why you should not, in general, mix file stream and file descriptor I/O on the same underlying file descriptor — especially not standard input.
Why not use scanf with some better formatting:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]) {
int n, i;
char buffer[200], ch;
printf("Enter an integer: ");
scanf("%d",&n);
printf("The input number is: %d\n", n);
printf("Please enter a string now: ");
scanf(" %[^\t\n]s",buffer);
printf("The input string is: %s", buffer);
printf("which is: %s\n", buffer);
printf("Now please enter another message: ");
scanf(" %[^\t\n]s",buffer);
printf("%s", buffer);
return 0;
}
read(0,buffer,200);
reads up to 200 characters but it won't add a terminating null character to buffer. Hence the following call to printf results in undefined behavior.
You need to add code to capture the return value of the function and make sure to null terminate the string.
int n = read(0, buffer, 199); // Leave one for the terminating null character
if ( n == -1 )
{
// Deal with the error.
}
else
{
// null terminate the string.
buffer[n] = '\0';
}
Here is a loop that repeatedly gets two characters from stdin and outputs them.
char buf[2];
while (1)
{
printf("give me two characters: ");
fflush(stdout);
read(0, buf, 2);
printf("|%c%c|\n", buf[0], buf[1]);
}
The problem is that when the ENTER key is struck on the terminal, a newline character is inserted and remains in the stdin buffer, and will be grabbed on the next call to read. Ideally, I would like to have a clear stdin buffer ever time I call read, with none of the previous garbage left over. Can you recommend a good solution to this?
I have tried various library calls such as fgets, however they encounter the same issue. I was considering using fpurge to clear the buffer manually, but I was told this was not a good solution.
The issue here is that leftover input should be treated as garbage, and thrown away. However when I make the next call to read I have no way of distinguishing the leftover input from the new input.
Thanks for the input.
You can add getchar(); to read the more '\n':
#include <stdlib.h>
#include <stdio.h>
char buf[2];
main() {
while (1)
{
printf("give me two characters: ");
fflush(stdout);
read(0, buf, 2);
getchar();
printf("|%c%c|\n", buf[0], buf[1]);
}
}
give me two characters: ab
|ab|
give me two characters: xy
|xy|
In my program I'm just calculating the costs of things. However, at the end I want a little break at the program asking for the user to just press the Enter button. I supposed getchar() would work here but it doesn't even stop, it just continues to keep printing. I even tried to put a space after the scant formats like scanf("%s ").
So two things how do I stop the program to ask for input at getchar() and how do I make it recognize just a enter button.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char hotels_houses[5];
int houses, hotels, cost;
printf("Enter amount of houses on board: \n");
scanf("%s", hotels_houses);
houses = atoi(hotels_houses);
printf("Enter amount of hotels on board: \n");
scanf("%s", hotels_houses);
hotels = atoi(hotels_houses);
printf("Cost of houses: %d\n", houses);
printf("Cost of hotels: %d\n", hotels);
cost = (houses *40) + (hotels * 115);
puts("Click enter to calculate total cash ");
getchar(); /* just a filler */
printf("Total cost: %d\n", cost);
return(0);
}
My best guess is that it is retrieving the remaining newline after the user has entered their input. You can print out the return value to verify. If I'm correct, it'll be "10" or "13" depending on your OS.
You might want to change your program to use getline. There are other examples on how to write a get line at How to read a line from the console in C?
When code calls scanf("%s", ... the program waits for input.
You type "123" and nothing happens yet as stdin is buffered input and waits for a \n thus the system has not given any data to scanf().
Then you type "\n" and "123\n" is given to stdin.
scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "123". Finally it sees "\n" and puts it back in stdin and completes.
Code calls scanf("%s", ... again. scanf() scans the "\n" as part of its scanning optional leading white-space. Then it waits for more input.
You type "456" and nothing happens yet as stdin is buffered input and waits for a \n thus the system has not given any data to scanf().
Then you type "\n" and "456\n" is given to stdin.
scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "456". Finally it sees "\n" and puts it back in stdin and completes.
Finally you call getchar() and puff, it reads the previous line's \n from stdin.
So how to do I stop the program to ask for input at getchar() and how do I make it recognize just a enter button.
Best approach: use fgets()
char hotels_houses[5+1];
// scanf("%s", hotels_houses);
fgets(hotels_houses, sizeof hotels_houses, stdin);
houses = atoi(hotels_houses);
...
// scanf("%s", hotels_houses);
fgets(hotels_houses, sizeof hotels_houses, stdin);
hotels = atoi(hotels_houses);
...
puts("Click enter to calculate total cash ");
fgets(bhotels_houses, sizeof hotels_houses, stdin); // do nothing w/hotels_houses
printf("Total cost: %d\n", cost);
Checking for a NULL return value from fgets() is useful to test for a closed stdin.
Using strtol() has error checking advantages over atoi().