2014-02-19 3 views
-1

그래서 대부분의 프로그램이 정상적으로 작동합니다. Taylor 시리즈를 사용하여 사인 값과 코사인 값을 추정하도록 설계된 프로그램입니다. 이 프로그램은 사용자가 0을 입력하면 종료되도록 설계되었으며, 그런 다음 질문이있을 경우 "Y" 또는 "y"이 표시됩니다. char 변수 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 ''라고 생각합니다. –

+1

'switch' 문은 여기 또는 * 테이블 조회 *에서 더 좋을 것입니다. –

+0

'toupper'와'tolower' 함수를 검토하면 대문자 또는 소문자 만 비교하면됩니다. 둘 다 아닙니다. –

답변

2

remyabel이 질문에 대답했습니다. 코드에 "사용자가 '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'을해야합니다. 그렇지 않으면 항상 true로 평가됩니다.

+0

ohhh ok 그 질문을 보지 못해 죄송합니다. 나는 그것에 논리를 얻는 것을 wasnt한다, 그것은 지금 비록 감각이있다. 감사! – user3329916

관련 문제