变量未更新,而While循环不起作用

时间:2018-10-05 19:03:59

标签: 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;
}

1 个答案:

答案 0 :(得分:2)

'S'案例中您读了一个数字,而忘记在案例末尾添加break语句。这导致代码掉入Q情况,导致循环退出。

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

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

这也不正确:

scanf("%s", &input);

%s期望char *,但是您正在传递数组的地址(以char (*)[10]为前提)。直接传递数组,它将衰减为指向第一个元素的指针以产生正确的类型:

scanf("%s", input);

另外,将print_menuget_operand更改为返回void,因为您没有使用返回值,并且在前一种情况下没有包含返回值。

相关问题