有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值

Can someone help me with making a while loop so I can keep putting in cin values after each calculation?

本文关键字:计算 都可以 CIN 继续 循环 帮助 一个      更新时间:2023-10-16

有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用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...
}
相关文章: