c - 旗帜如何发挥作用?

标签 c

这是我的代码。我想让它工作,如果用户输入 y 它将继续循环,否则如果他们按 q 程序退出。我看到很多人使用标志,我认为这很棒,所以我尝试做一些事情,但我不知道为什么它不起作用。我尝试使用中断;并继续;但输出仍然不是我想要的。

#include <stdio.h>
int main(){



int broj1,broj2,zbir;
  char exit;
  int flag = 0;

  while(!flag){
  printf("Enter numbers: \n");
  scanf("%d %d",&broj1,&broj2);
  zbir = broj1+broj2;
  printf("N1 + N2 is: %d\n",zbir);

  printf("Type 'q' if you want to exit or 'y' to continue: ");
  scanf("%c",&exit);


  if(exit=='q')
  flag=1;

  if(exit=='y')
  flag=0;

}


  return 0;
}

最佳答案

你可以使用 exit = getch();,而不是 scanf,而且如果你使用 getch() ,用户不需要在输入 y 或 q 后按 Enter 键,如果你愿意的话要保留你的 scanf,你可以在它之前添加 fflush(stdin); (从上次 scanf 调用中删除 Enter)

关于c - 旗帜如何发挥作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097350/

相关文章:

c - 内联-c : "` Type` cannot be marshalled in a foreign call"

c - 将指针差异转换为 "int*"是什么意思?

c++ - 位排序/字节顺序 flac 解码

c++ - DeleteIPAddress 和 AddIpAddress 问题

c - 杀死系统调用C,权限

c - 用 C 将项目插入堆栈

c++ - IPv4 和 IPv6 地址的 connect() 行为

c++ - gdb 在 LLVM 之上?

c - 为什么在此示例中将 utmp 结构数组定义为 char?

c - 如何将 UTC 日期转换为 time_t