sách gpt4 ăn đã đi

c - 例 1-8 C 编程语言

In lại Tác giả: Walker 123 更新时间:2023-11-30 15:46:09 25 4
mua khóa gpt4 giày nike

我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码

#include 
#include
#include

/*
*
*/
int main() {
int nl,nt,nb;
int c;
printf("\nHello and Welcome :D\nThis is the 'answer' to Ex 1-8 in TCPG\nRemember,to end input,type in %d\n[Press to continue]\n",EOF);
getch();
for(nl = 0,nb = 0,nt = 0;(c = getchar()) != EOF; ) // When getchar is replaced by getche() it goes into the loop but doesnt exit the loop
{
putchar(c);
if(c == '\n')
{
nl++;
}
else if(c == '\t')
{
nt++;
}
else if(c == ' ')
{
nb++;
}
}
printf("\nYou typed in %d blanks, %d tabs and wrote %d lines\n[Press to exit]",nb,nt,nl);
getch();



return (EXIT_SUCCESS);
}

它只是不进入循环!当 getchar() 替换为 getche() 时,它进入循环但不退出:(如果你没猜到,putchar(c);只是为了确认已经进入循环我尝试了 Ctrl + D 和 Ctrl + Z我使用的是 Windows 8谢谢大家:)

编辑:我首先使用了 switch case 结构,但我认为我应该按照这本书去,我刚刚看到它在我输入一点后说 RUN FAILED (退出值 -1,总时间:5s)...有任何想法吗?谢谢大家:)(再次:D)

câu trả lời hay nhất

首先,您不应该使用或 getch/getche,它们不是标准 C。当您在循环中使用 getche 时,您的问题是 MS Windows 上的此函数不处理文件结束控件。

一般来说,第一个 getchar() 调用只有在您按下第一行的 Enter 键后才会返回,因此,如果您期望它立即“进入循环”,那么您就错了。只需继续输入并按 Enter 键即可。

关于c - 例 1-8 C 编程语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18544252/

25 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress