我正在编写一个程序供用户输入a,b,c以找到二次方程式。
请帮助我以便了解我的错误。
#include < iostream>
#include < cmath>
#include < string>
using namespace std;
//////Quadratic Formula///////
int a;
int b;
int c;
float top1;
float left1;
float left2
float under3;
float under4;
int main()
{
////////////////////User Input a, b, c///////////////////////
cout << "This program will allow you to input numbers for variables a, b, and c to find the quadratic equation" << endl;
cout << "Enter a number for a: ";
cin >> a;
if (a == 0) //If user enters 0 it will quit the program and give them error message.
{
cout << "0 is not a sufficient coefficient for this program.";
cout <<"Restart now and try again." << endl;
exit(0);
}
cout << "Enter a number for b: ";
cin >> b;
cout << "Enter a number for c: ";
cin >> c;
//////////////Calculations////////////
//////////////////b add
float top1 = (b*b)- 4*a*c);
float left1 = (-b) + sqrt(top1);
float under3 = (left1))/(2*a);
/////////////b subtract
float top1 = (pow(b,2) - 4*a*c);
float left2 = (-b) - sqrt(top1);
float under4 = (left2))/(2*a);
cout <<"The Answer for the Quadratic equation is:";
cout << under3(a, b, c,) <<endl; // function call
cout <<"The first Root Value = " << under3 << endl;
cout <<"The second Root Value = " << under4 << endl;
system("pause") ;
return 0;
}
//px = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)
//nx = (-b - sqrt(b * b - 4 * a * c)) / (2 * a)
答案 0 :(得分:0)
我对你的a == 0错误检查提出的一个稍微不相关的建议是使用do while语句而不是让你的程序关闭。它看起来像这样:
do
{
cout << "Enter a number for a: ";
cin >> a;
if (a==0)
{
cout << "Can't use 0 for a.";
}
} while (a==0);
这使得用户无需在输入无效号码时重新启动程序。只是一个建议。