所以我的大部分程序都运行正常。这是一个旨在使用泰勒级数估计正弦和余弦值的程序。该计划旨在在用户输入0
后退出,然后在被询问是否确定时"Y"
或"y"
退出。 char变量exit
初始化为"n"
,然后在用户输入y
时更改。但是循环并没有退出。
#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;
}
答案 0 :(得分:4)
我回答了一个名为Why is my c++ code not working properly?的类似问题。答案完全一样。您需要exit != 'Y' && exit != 'y'
,否则它将始终评估为真。
答案 1 :(得分:2)
因此,while (exit != 'Y' && exit != 'y')
基本上说“如果用户没有输入退出条件,我将继续执行。”