c - 如果输入“q”,则在 C 中创建一个退出的函数
问题描述
我必须编写一个没有参数并返回一个 int 值的函数。该函数要求用户输入“q”或“Q”以退出。如果输入任何其他字符,程序将继续。
解决方案
像这样的东西...
#include <stdio.h>
char key;
int main()
{
quitAtQ();
return 0;
}
int quitAtQ() {
int number = 1; // enter your return number here
printf("Enter 'q' or 'Q' in order to quit\n");
scanf_s("%c", &key);
while ((strcmp("q", &key) != 0) && (strcmp("Q", &key) != 0)) {
printf("You have entered char '%c', program continues\n", key);
printf("Enter 'q' or 'Q' in order to quit\n");
scanf_s("%c", &key); // enter key will be read as next char
scanf_s("%c", &key); // next char input
}
return number;
}
推荐阅读
- java - Java包结构约定
- jenkins - Jenkins - 将文件下载到用户的浏览器作为构建后步骤
- eclipse - 如何为 Eclipse Luna 设置 c++11?
- json - Hello World 示例:安装/测试时出现“出现问题”
- android - react native如何检查内部对象是否存在?
- python - 根据可能出现多次的关键字拆分列表
- android - 如何在 Android 中使用 RxJava 响应 body()
- jquery - 未引用的“href”属性被异步加载阻止
- web - Service Worker 发送的重复发布请求
- excel - 在行中查找最高值并使用 VBA 在 excel 中复制