一个程序问题;错在哪啊,我找了很久也不知道,我是初学者。

来源:百度知道 编辑:UC知道 时间:2024/06/20 04:09:00
#include <iostream>
#include <cmath>
using namespace std;
void dongying(double a, double b , double c )
{

cout << (-b + sqrt(b * b - 4 * a * c)) / (2 * a) << " " << (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
}

int main(int argc, char** argv) {

double a;
double b;
double c;
double x = b * b - 4 * a*c;
cout << "please enter the three numbers:";
cin >> a;
cin >> b;
cin >> c;
if (x >= 0) {
cout <<"the answer is:"<<dongying(a,b,c);
} else cout << "there is no answer to the question.";

return (EXIT_SUCCESS);
}

因为dongying(a,b,c)返回值为void,所以无法显示。
把cout <<"the answer is:"<<dongying(a,b,c);
改为 cout <<"the answer is:";dongying(a,b,c);

double x = b * b - 4 * a*c;
放在 cin >> c;后面试试

主要错误在,cout里面不能直接输出函数,(下面我标出了错误的)

#include <iostream>
#include <cmath>
using namespace std;
void dongying(double a, double b , double c )
{

cout << (-b + sqrt(b * b - 4 * a * c)) / (2 * a) << " " << (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
}

int main(int argc, char** argv) {

double a;
double b;
double c;

cout << "please enter the three numbers:";
cin >> a;
cin >> b;
cin >> c;
double x = b * b - 4 * a*c;
if (x >= 0) {
cout<<"the answer is:"<<endl;
dongying(a,b,c);//把函数写在