C ++编程,无法理解错误

时间:2016-03-10 04:22:42

标签: c++ math

我是c ++编程的初学者,这只是我的第二个程序。我得到一个一致的错误“预期不合格的身份......之前...”idk意味着什么,无法解决它。这是在第21,27,29,33,35,38,40,43,45.48,54,56,59,61,64,66,70行。

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

int main ();

int a, b, c, x,y;
int discriminant;
double x1, x2;

int countdataisinvalid=0;
int countdataisvalid=0;
int countnolastterm=0;
int countonexvalue=0;
int countnomiddleterm=0;
int counttwoxterms=0;

while(!cin.eof)
    {
        a*x*x+b*x+c;
    }


    if (a==0),
        countdataisinvalid++;
        {
            cout << "A is 0, data invalid." << endl;
        }

    else if, (discriminant < 0),
        countdataisinvalid++;
        {
            cout << "The square is a negative number, data invalid."     <<              endl;
        }
    else,
        countdataisvalid++;
        {
            cout << " Data set is valid." << endl;
        }
    if (c==0),
        countnolastterm++;
        {
            cout << "C is 0, there is no last term." << endl;
        }
{
x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
x2 = (-b - sqrt(b*b-4*a*c))/(2*a);

cout.precision(3);
}
    if (x1==x2),
        countonexvalue++;
        {
            cout << "Only one x value." << endl;
        }
    else, if (x1==-x2),
        countnomiddleterm++;
        {
            cout << "There is no middle term." << endl;
        }
    else
        counttwoxterms++;
        {
            cout << "There are two x values." << endl;
        }

{
y = a*x1*x1+b*x1+c
y = a*x2*x2+b*x2+c

cout << "When x is " << x << "y is " << y << endl;
}

1 个答案:

答案 0 :(得分:1)

您的代码包含太多错误。你应该通过编写简单的程序再次学习C ++。

至少此代码会编译。

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

int main ()
{ // use { to begin definition of function, not ;

    // initialize ariables for in case the reading fails
    int a = 0, b = 0, c = 0, x = 0, y = 0;
    int discriminant = 0;
    double x1 = 0, x2 = 0;

    int countdataisinvalid=0;
    int countdataisvalid=0;
    int countnolastterm=0;
    int countonexvalue=0;
    int countnomiddleterm=0;
    int counttwoxterms=0;

    // you should read numbers instead of writing possibly infinite loop and meaningless statement
    cin >> a >> b >> c >> x >> discriminant >> x1 >> x2;


    if (a==0) // remove extra comma
        {
            countdataisinvalid++; // move this statement to right position
            cout << "A is 0, data invalid." << endl;
        }

    else if (discriminant < 0) // remove extra commas
        {
            countdataisinvalid++; // move this statement to right position
            cout << "The square is a negative number, data invalid."     <<              endl;
        }
    else // remove extra comma
        {
            countdataisvalid++; // move this statement to right position
            cout << " Data set is valid." << endl;
        }
    if (c==0) // remove extra comma
        {
            countnolastterm++; // move this statement to right position
            cout << "C is 0, there is no last term." << endl;
        }
    {
        x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
        x2 = (-b - sqrt(b*b-4*a*c))/(2*a);

        cout.precision(3);
    }
        if (x1==x2) // remove extra comma
            {
              countonexvalue++; // move this statement to right position
                cout << "Only one x value." << endl;
            }
        else if (x1==-x2) // remove extra commas
            {
                countnomiddleterm++; // move this statement to right position
                cout << "There is no middle term." << endl;
            }
        else
            {
            counttwoxterms++; // move this statement to right position
                    cout << "There are two x values." << endl;
            }

    {
        y = a*x2*x2+b*x2+c; // add semicolon and remove useless statement

        cout << "When x is " << x << "y is " << y << endl;
    }

    return 0; // add a statement to return some value
} // add this as end of definition of function