C++ 当到达选项0时,如何停止循环返回菜单
这是一个菜单驱动的计算程序C++ 当到达选项0时,如何停止循环返回菜单,c++,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
#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!!";
}
}
#包括
#包括
使用名称空间std;
无效选择();
int x;
int n;
浮动事实;
浮点数cosh(浮点数x,整数n);
浮点数sinh(浮点数x,整数n);
浮点阶乘(intn);
int main()
{char exit;
字符选择;
bool option1hasrun=false;
while(exit!=“y”| exit!=“y”)
{
选择();
cin>>选择;
if(choice='1'&&option1hasrun==false){
cout>x;
cout>n;
cout输入的字符不能同时为“y”和“y”。
尝试将退出条件更改为:
while (exit != 'y' && exit != 'Y')
输入的字符不能同时为“y”和“y”。
尝试将退出条件更改为:
while (exit != 'y' && exit != 'Y')
@阿美:您的exit
变量在第一次运行时未初始化。请同时查找std::tolower
或std::toupper
,这样您只需检查“y”或“y”,而不必同时检查两者。@SleuthEye:我面临的另一个问题是[cout@ThomasMatthews:谢谢你,托马斯。我一定会查清楚的。@May:x
是一个int
。当你做cin>>x
时,你的程序只希望找到一个整数(它是,但不包括
)。然后遵循cin>
语句,然后阅读您键入的其余内容,混淆您的代码。@May:yourexit
变量在第一次运行时未初始化。还要查找std::tolower
或std::toupper
,这样您只需检查“y”或“y”,而不必同时检查两者。@SleuthEye:我面临的另一个问题是r[cout@ThomasMatthews:谢谢你,托马斯。我一定会查清楚的。@May:x
是一个int
。当你做cin>>x
时,你的程序只希望找到一个整数(它是,但不包括
)。然后按照cin>>
语句阅读您键入的其余内容,混淆您的代码。