简体   繁体   English

为什么我的getchar()在这里不起作用?

[英]Why is my getchar() not working here?

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. 但是,最后,我想让程序稍作休息,要求用户仅按Enter键。 I supposed getchar() would work here but it doesn't even stop, it just continues to keep printing. 我以为getchar()可以在这里工作,但它甚至没有停止,只是继续打印。 I even tried to put a space after the scant formats like scanf("%s "). 我什至尝试在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. 所以有两件事,我该如何停止该程序在getchar()处请求输入,以及如何使它仅识别Enter键。

#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. 如果我是对的,则取决于您的操作系统,它将是“ 10”或“ 13”。

You might want to change your program to use getline. 您可能需要将程序更改为使用getline。 There are other examples on how to write a get line at How to read a line from the console in C? 如何从C语言的控制台中读取行的地方,还有其他关于如何编写get行的示例

When code calls scanf("%s", ... the program waits for input. 当代码调用scanf("%s", ... ,程序等待输入。

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() . 输入“ 123”并没有任何反应,因为stdin是缓冲输入并等待\\n因此系统没有将任何数据提供给scanf()

Then you type "\\n" and "123\\n" is given to stdin . 然后,您输入“ \\ n”,并将stdin赋予“ 123 \\ n”。

scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "123". scanf("%s",...)读取stdin并扫描可选的前导空白,然后扫描非空白“ 123”。 Finally it sees "\\n" and puts it back in stdin and completes. 最后,它看到“ \\ n” 并将其放回 stdin并完成。

Code calls scanf("%s", ... again. scanf() scans the "\\n" as part of its scanning optional leading white-space . Then it waits for more input. 代码再次调用scanf("%s", ... scanf()扫描“ \\ n”作为其扫描可选前导空白的一部分,然后等待更多输入。

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() . 键入“ 456”,由于stdin是缓冲输入并等待\\n因此没有任何反应,因此系统没有将任何数据提供给scanf()

Then you type "\\n" and "456\\n" is given to stdin . 然后输入“ \\ n”,将stdin赋予“ 456 \\ n”。

scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "456". scanf("%s",...)读取stdin并扫描可选的前导空白,然后扫描非空白“ 456”。 Finally it sees "\\n" and puts it back in stdin and completes. 最后,它看到“ \\ n” 并将其放回 stdin并完成。

Finally you call getchar() and puff, it reads the previous line's \\n from stdin . 最后,调用getchar()和puff,它从stdin读取前一行的\\n


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. 因此,我该如何停止该程序在getchar()处请求输入,以及如何使它仅识别Enter键。

Best approach: use fgets() 最佳方法:使用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 . fgets()检查NULL返回值对于测试封闭的stdin很有用。
Using strtol() has error checking advantages over atoi() . atoi()相比,使用strtol()具有错误检查优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM