二次方程式C++

二次方程式C++,c++,c++11,C++,C++11,我正在写一个程序,让用户输入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 und

我正在写一个程序,让用户输入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)

对于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);

这使得用户不必在输入无效数字时重新启动程序。只是一个建议。

这不是问题。当前代码有问题吗?是否有错误消息?是否要查看代码?创建全局变量,然后创建同名的新局部变量。删除全局变量,仅使用局部变量。不要在后面加空格,我建议你使用更少的变量,把计算合并成一个块,就像你在底部注释出来的那样。如果你的代码正常工作,你应该请求代码审查来了解你做错了什么;缺少分号,括号在多个位置不匹配。我的错误应为“或;”浮点数top1的前标记=bb-4*ac;谢谢你们,你们帮了大忙。问题是退出条件重复了两次。@NeilKirk会吗?我没有仔细检查编译器上的代码,但我很确定它能正常工作。OP当然必须删除他当前的错误检查。一般来说,两次使用相同的逻辑是个坏主意。如果代码和条件非常重要,有人更新了其中一个,但忘记了更新另一个,该怎么办?@NeilKirk我很难跟上。你说有两次是什么逻辑?你是指我的if和Do while条件吗?我认为对于这个程序来说,如果你有这两个,那没关系,因为a永远不可能等于0,否则四元方程就不起作用了。@NeilKirk我很好奇你会怎么做。