c - 我应该在 scanf() 中同时包含 fflush(stdin) 和空格,还是只包含其中之一就足够了?

标签 c function shared-libraries

用户输入数字后,我的计算器应用程序无法正常工作。所以我在 scanf() 中的 %d 后面添加了 fflush(stdin) 和一个空格。我的问题是我应该只包含 fflush(stdin) 和 %c 之后的空格还是只有其中一个选项就足够了?我正在使用 Macbook,因此我担心如果我漏掉其中一个,它就无法在其他操作系统上运行。

#include <stdio.h> 
#include <stdlib.h> 

//PROTOTYPE
int operationSwitch(); //Function to calculate total of operand1 and operand2.
int menu(); //Function for main menu.
int iResume(); //Function to continue the program or not.


char  iOperation    = '\0'; //choices (1-7)
float operand1      = 0; //number 1
float operand2      = 0; //number 2
float operandTotal  = 0; //total

int   testPrime, counter, prime = 0; 

char cContinue   = '\0'; 
char symbol      = '\0'; 


int main(){
    menu();
return 0;
}


int menu (){

  do {
      printf("\nPlease choose an operation: ");
      printf("\n(1) Addition\n ");
      printf("\n(2) Subtraction\n ");
      printf("\n(3) Multiplication\n ");
      printf("\n(4) Division\n ");
      printf("\n(5) Modulus (integers only)\n ");
      printf("\n(6) Test if Prime (integers only)\n ");
      printf("\n(7) Exit\n ");
      printf("\nChoose (1-7):\t");
      fflush(stdin);
      scanf(" %c", &iOperation);
  } while (iOperation < 49 || iOperation > 55 );


  if (iOperation != 54 && iOperation != 55){ //ASCII 55 == 7)
    printf("\nEnter number 1:\t");
    fflush(stdin);
    scanf(" %f", &operand1);
    printf("\nEnter number 2:\t");
    fflush(stdin);
    scanf(" %f", &operand2);
  }
    operationSwitch();

  /*** RESULTS ***/
  if ( symbol == '/' && operand2 == 0) {
    printf("\n\t-----| Answer: %.2f / %.2f = Undefined |-----\n", operand1,operand2);
  } else if (iOperation != 54) {
    printf("\n\t-----| Answer: %.2f %c %.2f = %.2f |-----\n", operand1, symbol, operand2, operandTotal);
  }


      iResume();

  return 0;
} //End Menu

/*********************************
  SWITCH FUNCTION
*********************************/
int operationSwitch() {

  switch (iOperation) {
    case 49://1 Addition Ascii 49 == 1 43 == +
      operandTotal = operand1 + operand2;
      symbol = '+';
      break;

    case 50: //2 Substraction
      operandTotal = operand1 - operand2;
      symbol = '-';
      break;

    case 51: //3 Multiplication
      operandTotal = operand1 * operand2;
      symbol = '*';
      break;

    case 52: //4 Division
      operandTotal = operand1 / operand2;
      symbol = '/';
      break;

    case 53: //5 Modulus
      operandTotal = (int)operand1 % (int)operand2;
      symbol = '%';
      break;

    case 54: //6
      prime = 1;
      printf("\nEnter number to be tested for prime: ");
      fflush(stdin);
      scanf(" %d", &testPrime);

        for(counter = 2; counter <= (testPrime/2); counter++ )
        {
        if(testPrime % counter == 0)
          {
              prime = 0;
              break;
          }
        }
        if (prime == 1) {
                printf("\n\t| %d is a prime number. |\n", testPrime);
        }
        else {
            printf("\n\t| %d is not a prime number. |\n", testPrime);
            printf("\n\t| %d * %d = %d \n", testPrime/counter, counter , testPrime);
        }
      break;

    case 55:
      printf("\nGood Bye\n");
      exit(0);
      break;

    default:
      printf("\nYou entered: %c - Please try again ", iOperation );
      break;
  } //End Switch iOperation

  return 0;
} //End Switch Function

/*********************************
  RESUME FUNCTION
*********************************/
int iResume() {
  printf("\nWould you like to try again?\nPress \'Y\' to go to menu,\nor any key to quit?\t");
  fflush(stdin);
  scanf(" %c", &cContinue);

  if (cContinue == 'y' || cContinue == 'Y'){
    menu();
  } else {
    printf("Good Bye!\n" );
  }
  return 0;
}

最佳答案

fflush(stdin) 是未定义的行为。

int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

关于c - 我应该在 scanf() 中同时包含 fflush(stdin) 和空格,还是只包含其中之一就足够了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52205697/

相关文章:

c - 我需要将指针从结构单独写入文件吗?

c - 为什么 sem_wait 在 mac OSX 上不等待信号量?

c - 如何在 Linux 中删除 ^M ^J 字符

MySQL 函数不返回正确的结果

shared-libraries - 共享库以什么顺序初始化和完成?

c - 二维数组 - C 语言菜单

javascript - 将功能连接到 PC 文件夹中显示的图片

php - 在 PHP 函数中哪里返回 false?

linux - 使用 setuid 的可执行文件找不到共享库

c++ - "/usr/lib/libstdc .so.6: version ` GLIBCXX_3.4.1 5' not found"是什么意思,我该如何解决?