c - 如何为具有不同条件的程序获取多个输入(整数和字符串)
问题描述
我尝试以不同的方式实现此代码,但它总是会中断(最后一个条件),所以我不知道问题是什么。
它应该读取integer
输入和on/off
输入,然后打印正确的结果
#include <string.h>
#include <stdio.h>
int main(void) {
printf("start");
int c;
char ch;
int light_id = 1;
//char on_off_str;
while (light_id > 0) {
printf("Enter number and on/off_str:\n");
//c = getchar();
//ch = getchar();
if (c = getchar() == 1) {
if (ch = getchar() == 'on') {
printf("1 and on");
}
if (ch = getchar() == 'off') {
printf("1 and off");
}
}
else if (c = getchar() == 2) {
if (ch = getchar() == 'on') {
printf("2 and on");
}
if (ch = getchar() == 'off') {
printf("2 and off");
}
}
else {
printf("break\n");
break;
}
}
return 0;
}
解决方案
推荐阅读
- flutter - 当我在 Stack(Positoned) 中使用 ListView 时出现问题
- python - 何时使用 int() 以及何时使用 var: int
- flutter - inappwebview中的全屏图标在颤动中太小了
- entitymanager - 何时以及为何在 MikroOrm 中使用 em.clear()
- coq - Coq 上的微积分
- c++ - 使用 C++ 读写 NtGlobalFlags
- python - 如何验证 pathlib.glob 的结果?
- python - 使用 PIL 和 tkinter 动态调整图像大小
- webpack - 如何使用 webpack 发出编译后的 scss 并获取输出 url?
- c# - blazor 中的 ComponentReferenceCapture 更新