问题
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);
}
回答1:
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?
回答2:
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()
.
来源:https://stackoverflow.com/questions/22082838/why-is-my-getchar-not-working-here