2014-02-19 171 views
-1

因此,我的大多数程序都能正常工作。这是一个旨在使用泰勒级数估计正弦和余弦值的程序。一旦用户输入0,然后"Y""y"询问他们是否确定,该程序将被设计为退出。字符变量exit初始化为"n",然后在用户输入y时更改。但是循环不会退出。即使条件满足,C++ do-while循环也不会退出

#include <iostream> 
#include <cmath> 
using namespace std; 

double calculateFACT(int n); // function that calculates the factorial 
double calculateSIN(float, float); // function that approximates the sine 
double calculateCOS(float, float); // function that approximates the cosine 

int main() 
{ 
    int choice; // menu choice 
    double angle = 0; // angle user inputs, initialied to zero 
    double calc; // the calculated sine or cosine value 
    int order; // order approimation value 
    char exit = 'n'; // exits for yes 

    do { 
     cout << "MAIN MENU" << endl; 
     cout << "1. To enter the data." << endl; 
     cout << "2. To calculate the sin(x)" << endl; 
     cout << "3. To approximate the sin(x)" << endl; 
     cout << "4. To calculate the cos(x)" << endl; 
     cout << "5. To approximate the cos(x)" << endl; 
     cout << "6. To re-enter data." << endl; 
     cout << "Press 0 to quit." << endl; 
     cout << "Please make a choice: "; 
     cin >> choice; 
     cout << endl; 

     if (choice != 0 && 
      choice != 1 && 
      choice != 2 && 
      choice != 3 && 
      choice != 4 && 
      choice != 5 && 
      choice != 6) 
     { 
      cout << "Wrong Choice. Only options 1-6 are available." << endl << endl; 
     } 

     if (choice == 1) 
     { 
      if (angle == 0) 
      { 
       cout << "Please give a value for the angle: "; 
       cin >> angle; 
       cout << endl; 
      } 
      else cout << "Please use option 6 to enter a new angle." << endl << endl; 
     } 

     if (choice == 2) 
     { 
      if (angle == 0) 
      { 
       cout << "You have to enter a value first!" << endl << endl; 
      } 
      else 
      { 
       calc = sin(angle); 
       cout << "The sine of x is " << calc << endl << endl << endl; 
      } 
     } 

     if (choice == 3) 
     { 
      if (angle == 0) 
      { 
       cout << "You have to enter a value first!" << endl << endl; 
      } 
      else 
      { 
       cout << "Please give a value for the approximation order n: "; 
       cin >> order; 
       cout << "The approximation of sin(" << angle << ") is: " << calculateSIN(angle, order) << endl << endl; 
      } 
     } 

     if (choice == 4) 
     { 
      if (angle == 0) 
      { 
       cout << "You have to enter a value first!" << endl << endl; 
      } 
      else 
      { 
       calc = cos(angle); 
       cout << "The cosine of x is " << calc << endl << endl << endl; 
      } 
     } 

     if (choice == 5) 
     { 
      if (angle == 0) 
      { 
       cout << "You have to enter a value first!" << endl << endl; // cosine function not giving the right value 
      } 
      else 
      { 
       cout << "Please give a value for the approximation order n: "; 
       cin >> order; 
       cout << "The approximation of cos(" << angle << ") is: " << calculateCOS(angle, order) << endl << endl; 
      } 
     } 

     if (choice == 6) 
     { 
      if (angle == 0) 
      { 
       cout << "If this is the first time you run this program please choose option 1." << endl << endl; 
      } 
      else 
      { 
       cout << "Please give new angle: "; 
       cin >> angle; 
       cout << endl << endl; 
      } 
     } 

     if (choice == 0) 
     { 
      cout << exit; 
      cout << endl << endl << "Are you sure you want to quit? (Y/N): "; // Y/N option doesnt work 
      cin >> exit; 

     } 
     cout << exit; 
    } while (exit != 'Y' || exit != 'y'); 

    if (exit == 'Y' || exit == 'y') 
    { 
     cout << endl << "Now quitting.." << endl; 
     system("pause"); 
     return 0; 
    } 
} 

double calculateFACT(int n) 
{ 
    double nfact = 1; 
    for (int i = 2; i <= n; i++) 
     nfact *= i; 
    return nfact; 
} 

double calculateSIN(float angle, float order) 
{ 
    double sine = angle; 
    for (int i = 1; i < order; i++) 
    { 
     sine += pow(-1.0, i) * (pow(angle, 2 * i + 1))/calculateFACT(2 * i + 1); 
    } 
    return sine; 
} 

double calculateCOS(float angle, float order) 
{ 
    double cosine = 0; 
    for (int i = 0; i < order; i++) 
    { 
     cosine += pow(-1.0, i) * (pow(angle, 2 * i))/calculateFACT(2 * i); 
    } 
    return cosine; 
} 
+6

我认为它应该是'exit!='Y'&& exit!='y''。 – 2014-02-19 20:52:39

+1

'switch'语句在这里会更好,或者* table查找*。 –

+0

查看'toupper'和'tolower'函数,所以你只需要比较大写或小写;不是都。 –

回答

2

雷米贝尔回答了这个问题。你的代码说“如果用户没有输入'Y'或'y'”,继续运行。既然你只是在寻找一个角色,它将永远持续运行,因为角色不能同时是'Y'和'y'。

因此,while (exit != 'Y' && exit != 'y')基本上说“如果用户没有输入退出条件,我会继续执行。”

+0

此外,'while'后面的'if'语句是多余的,因为它是相同的条件。 –

+0

应该是一个评论... –

+0

你是对的,@πάνταῥεῖ,谢谢你的意见。我很抱歉。 – StephenH

4

我回答了一个类似的问题Why is my c++ code not working properly?。答案完全一样。你需要做exit != 'Y' && exit != 'y',否则它总会评估为真。

+0

ohhh好的抱歉没有看到这个问题。我没有得到这个逻辑,但现在它是有道理的。谢谢! – user3329916

相关问题