有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值
Can someone help me with making a while loop so I can keep putting in cin values after each calculation?
有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值吗?这是我的程序。我只是尝试进行一段时间循环以保持程序运行,但我不确定我应该在什么时候放置。
//This program calculates the roots of a quadratic equation.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
double a, b, c, root1, root2, disc, imaginarypart, realpart;
ofstream output ("E:result5.dat");
cout<< " This program calculates the roots of a quadratic equation. ";
cout << " ax^2 + bx + c = 0nn";
cout << "enter values for a, b, and c.";
cin >> a;
cin >> b;
cin >> c;
disc = (pow(b,2) -4*a*c);
if ( a == 0.0 && b == 0.0)
cout << "the equation has no real roots.";
else if ( a == 0.0 )
cout << " The equation is linear and has a single root x = " << -c/b <<endl;
else
{
if (disc > 0.0 )
{
root1 = (-b +sqrt((b*b) -4*a*c))/2*a;
root2 = (-b -sqrt((b*b) -4*a*c))/2*a;
cout << "The too roots are real and are X1 = " << root1 << " and X2 = " << root2 << endl;
}
else if (disc == 0.0)
{
cout<< "both roots are the same and are equal to " << -b/(2*a) << endl;
}
else
{
realpart = -b/(2*a);
imaginarypart = sqrt(-disc)/(2*a);
cout << " The roots are imaginary = " << endl;
cout << "x1 =" << realpart << "+" << imaginarypart << "i" << endl;
cout << "x2 =" << realpart << "-" << imaginarypart << "i" << endl;
}
system ("pause");
}
return 0;
}
尝试:
std::string aAsString;
while(true)
{
cout << "enter values for a, b, and c.; enter exit for quitting program";
std::cin >> aAsString;
if (aAsString == "exit")
break;
a = atof(aAsString.c_str());
// remainder of your code...
}
相关文章:
- C++映射有2个键,这样任何1个键都可以用来获取值
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 任何人都可以解释一下我是否需要 & 在第一个代码片段中
- 任何人都可以查明我的递归函数中的错误吗?
- Qt C++创建一个所有类都可以访问的全局变量
- 任何方法都可以在C++中比较数据结构中的许多方法
- 交叉和计算,任何人都可以解释代码吗?
- 我的合并排序没有给出正确的排序数组,任何人都可以解释一下吗?
- 任何人都可以解释这个初始值设定项代码吗?
- 如何使用C++共享目录,以便每个人都可以访问
- 任何人都可以在带有JNI的Android Studio上帮助我解决此错误,是否缺少一些内容?
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 任何人都可以检查这个回文 c++ 代码是否正确
- 任何机构都可以在运算符重载中解释这行代码
- 任何人都可以为我解释这个C
- C :对输入验证函数进行编程的性能影响可以对其进行编程,而每次都可以将其编程
- 有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值
- 任何比 pow() 更快的方法都可以在 C++ 中计算 10 的整数幂
- 在c++中,任何更好的方法都可以做到这一点,计算谜题的解决方案(49!结果,49个循环)