c++ - 到达选项 0 时,如何停止循环返回菜单

标签 c++

这是一个用于计算的菜单驱动程序。

#include <iostream>
#include <cmath>

using namespace std; 
void Choices(); 
int x; 
int n; 
float fact; 
float cosh(float x, int n);
float sinh(float x, int n);
float factorial( int n ); 


int main () 
{   char exit;
char choice; 
bool option1hasrun = false; 

while (exit != 'y' || exit != 'Y')

{
    Choices();
    cin >> choice; 

    if (choice == '1' && option1hasrun == false) {
        cout << "Please give a value for x: ";
        cin >> x; 
        cout << "Please give a value for the approximation order n: ";
        cin >> n; 
        cout << endl;
        option1hasrun = true; 
    }


    else if (choice == '2' && option1hasrun == true)
    {

        cout << "The hyperbolic sinh of x is: " << sinh(x) << endl;
        cout << "Using Taylor series is it: " << sinh(x, n) << endl;
    }   

    else if (choice == '3' && option1hasrun == true)
    {

        cout << "The hyperbolic cosh of x is: " << cosh(x) << endl;
        cout << "Using Taylor series is it: " << cosh(x, n) << endl;
    }   

    else if (choice == '4' && option1hasrun == true)
    {
        cout << "old value of x = " << x << endl;
        cout << "old approximation = " << n << endl;
        cout<< "Please give new value of x: " ; 
        cin >> x; 
        cout << endl;
        cout << "Please give new n: " ; 
        cout << endl;
        cin >> n; 
    }


    if ((choice=='2' || choice=='3' || choice=='4') && option1hasrun==false)
    {
        cout << "You have to enter a value first!\n\n"; 
    }


    if (choice<'0' || choice > '4')
    {
        cout << "Wrong choice. Only options 1-4 are available.\n\n";
    }


    else if (choice == '0' && option1hasrun==true)
    {
        cout << "Are you sure you want to quit? (Y/N) ";
        cin >> exit;
        if (exit =='y' || exit == 'Y') {
        cout << "bye bye!!";
    }
    }

/*输入Y后我想让程序停止,但是目前还在继续循环*/ }

return 0; 

}

float factorial( int n )
{
float fact = 1;

for ( int i = 1; i <= n; i++ )
{
    fact = fact * i;
}

return fact;
}

float sinh(float x, int n)
{
float sum = 0;


for ( int i = 0; i <= n; i++ )
{
    sum = sum + pow(x, 2*i +1)/factorial(2*i +1); 

}

return sum;
}


float cosh(float x, int n)
{
float sum = 0; 

for ( int i = 0; i <= n; i++ )
{
    sum = sum + pow(x, 2*i)/factorial(2*i); 

}

return sum;
}

void Choices ()
{
cout << "MAIN MENU" << endl;
cout << "1. To enter the data. " << endl;
cout << "2. To calculate and approximate the sinh(x)" << endl;
cout << "3. To calculate and approximate the cosh(x) " << endl;
cout << "4. To modify data. " << endl;
cout << "0. to quit." << endl << endl; 


cout << "Please make a choice :";
 }

谁能帮我弄清楚为什么程序在选择 0 后继续循环?

最佳答案

输入的字符不能同时为'y'和'Y'。 尝试将退出条件更改为:

while (exit != 'y' && exit != 'Y')

关于c++ - 到达选项 0 时,如何停止循环返回菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23901178/

相关文章:

c++ - 在另一个类函数中使用一个类的变量

c++ - const char* 变量作为函数的参数

c++ - 将 Eigen::SparseMatrix<double> 转换为 deal.ii::SparseMatrix<double>?

c++ - 为变量赋值后获取垃圾值,不明白为什么

c++ - 性能对象属性与范围变量

c++ - 如何在调用 return 后删除 [] 内存

c++ - 如何在 C++ 中实现多个 COM 接口(interface)?

C++:fopen() 返回空文件句柄

c++ - 如何使用 stb_truetype 在 directx9 中渲染文本?

java - C++对象转java,将对象传递给java后属性不正确(int,double)