gpt4 book ai didi

在 C 中创建一个函数,如果输入 "q",该函数就会退出

转载 作者:行者123 更新时间:2023-11-30 19:30:24 25 4
gpt4 key购买 nike

我必须编写一个没有参数并返回 int 值的函数。该函数要求用户输入“q”或“Q”才能退出。如果输入任何其他字符,程序将继续。

最佳答案

这是实现我认为你所说的内容的一种方法。

#include<stdio.h>
#include<stdlib.h> /* The library that is needed for the function exit() */
int get_your_char(void);
int main(void)
{
int x =get_your_char(); /* Here you call your function and you assign the returned integer to x */
printf("%d",x);
return 0;
}
int get_your_char(void)
{
char ch;
int your_value=666; /* Let's assume that this is the value that you want to return if the user won't quit */
ch=getchar(); /* Here you read the charachter */
if(ch == 'q' || ch == 'Q')
exit(EXIT_SUCCESS); /* EXIT_SUCCESS usually is the value 0(zero) and makes the program terminate normally*/
else
return your_value;
}

关于在 C 中创建一个函数,如果输入 "q",该函数就会退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com