c - 变量不更新,而循环不工作

标签 c while-loop switch-statement hex octal

我正在研究一个累加器,它要求不同的输入并根据这些输入更新累加器中的值。但是,当我要求输入十进制、十六进制或八进制时,我的循环似乎没有运行。有人可以看一下并提出一些修复建议吗?谢谢你!我还想以某种方式在我的 print_menu() 函数中使用 while 循环来检查输入是否有效。有什么建议吗?

#include <stdio.h>
#include <string.h>
short get_operand(char mode);
void print_acc(short acc);
char print_menu(void);
int main(void);

int main(){
  char mode = 'D';
  short acc = 8;
  char input[10];
  char option;
  char valid_input[7] = "OHDCSQ";

  while (mode != 'Q'){
      print_acc(acc);
      print_menu();
      scanf("%s", &input);
      printf("%s\n", input);
      option = (char) toupper(input[0]);

  switch(option){
      case 'O':
          mode = 'O';
          printf("mode\n");
          printf("Mode is Octal\n");
          break;

      case 'H':
          mode = 'H';
          printf("Mode is Hexadecimal\n");
          break;

      case 'D':
          mode = 'D';
          printf("Mode is Decimal\n");
          break;

      case 'C':
          acc = 0;
          break;

      case 'S':

          get_operand(mode);
          if (mode == 'H'){
              scanf("%hx", &acc);
              printf("%hx", acc);
              print_acc(acc);
          }

          else if (mode == 'O'){
              scanf("%ho", &acc);
              printf("%ho", acc);
              print_acc(acc);

          }
          else{
               scanf("%hd", &acc);
               printf("%hd", acc);
               print_acc(acc);
          }

      case 'Q':
         mode = 'Q';
         printf("\n");
         break;

    }
    //return acc;
   }


}
void print_acc(short acc){
      printf("\n");
      printf("****************************************\n");
      printf("* Accumulator:                         *\n");
      printf("*   Hex     :   %04hx                   *\n", acc);
      printf("*   Octal   :   %08ho               *\n", acc);
      printf("*   Decimal :   %06hd                      *\n", acc);
      printf("****************************************\n");

}

char print_menu(void){
      printf("\n");
      printf("Please Select one of the following options:\n");
      printf("O Octal Mode\n");
      printf("H Hecadecimal Mode\n");
      printf("D Decimal Mode\n");
      printf("\n");
      printf("C Clear Accumulator\n");
      printf("S Set Accumulator\n");
      printf("\n");
      printf("Q Quit\n");
      printf("\n");
      printf("Option: ");

}

short get_operand(char mode){
   switch(mode){
      case 'H':
          printf("Enter Hex value:");

          break;
      case 'O':
          printf("Enter Octal value:");
          break;
      default:
          printf("Enter decimal value: ");
          break;
   }
   return mode;
}

最佳答案

在读取数字的'S' 案例中,您忘记在案例末尾添加break 语句。这会导致代码落入到导致循环退出的Q 情况。

添加break,循环将按预期继续:

  case 'S':
      ...
      break;
  case 'Q':
      ...

这也是不正确的:

scanf("%s", &input);

%s 需要一个 char *,但您传递的是一个数组的地址(a char (*)[10]成为总统)。直接传入数组,这将衰减到指向第一个元素的指针以产生正确的类型:

scanf("%s", input);

此外,更改 print_menuget_operand 以返回 void,因为您没有使用返回值并且在前一种情况下失败包括一个。

关于c - 变量不更新,而循环不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52671882/

相关文章:

c - 将两个 C 结构打包成一个 uint8_t 指针以写入 DataFlash

c - ARM 汇编 memcpy 等价物

c - 在用户不退出的情况下,在 C 中查找两个数字的总和、差等

javascript - Switch Case 在 IE 中抛出错误

C# 处理多个 if/else if 语句的更好方法

c - 从环境变量生成 #include 宏

c++ - ONVIF 身份验证错误?

java - 在我的 while 循环中加入最小值和最大值检查器?

javascript - 在给定的时间间隔内尽可能多次地调用函数

python - Python 中的并行 while 循环