solved

Pages: 12
I try to do a C++ program to solve any Grade 2 Ecuation. (I used Dev-C++ 4.9.9.2)

The code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a,b,c,delta,x1,x2,x;
    cout<<"Grade 2 Ecuation"
    cout<<"Give a=";
    cin>>a;
    cout<<"Give b=";
    cin>>b;
    cout<<"Give c=";
    cin>>c;
    if (a!=0) 
    {
              delta=b*b-4*a*c;
              cout<<"delta="<<delta<<endl;
              if (delta<0) 
              {
                           cout<<"x doesnt match R, x matches C."<<endl;
              }
              else if (delta=0)
                   {
                               x1=x2=-b/a;
                               cout<<"x1=x2="<<x1<<endl;
                   }
                   else
                   {
                        x1=(0-b-sqrt(delta))/2*a;
                        cout<<"x1="<<x1<<endl;
                        x2=(0-b+sqrt(delta))/2*a;
                        cout<<"x2"<<x2<<endl;
                   }
             }
    else if (b!=0)
         {
                  x=-c/b;
                  cout<<"x="<<x<<endl;
         }
         else if (c=0) cout<<"Undeterminated ecuation\n";
              else cout<<"Impossible ecuation\n";
    }
    system("pause");
    return 0;


Is the Dev-C++, the problem ??
Is my code ok ?
and last question:
What is the syntax for sqrt in <cmath> ?
Last edited on
A. No.
B. No.
C. http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

Back to B: Lines 27 to 30 should be inside {}, and lines 27 and 29, you can't put -b. You CAN however do 0-b, which is what you want.
oh my god, you saved me thanks.

but my dev c++ still gives me error
row 34 expected primary-expression before "else"
and
row 34 expected `;' before "else"

( the error is at lines with ELSE... line 34,39, and 40)

but i dont know why it gives me this error ...
Last edited on
Your code essentially looks like this to the compiler:
1
2
3
4
5
if
else if
else
else if
else if


You should fix your indentation and you will see that you have an else if after an else.
I solve it, but now it gives me an annoing error at system("pause");
also....
else if (c=0) cout<<"Undeterminated ecuation\n";

looks like you meant c==0

(and you're spelling equation wrong)
Last edited on
im not speaking english well, >.< as im from another country and im not so good in math terms in english too.
For single words you can use a translator ;)
anyway at the end of program, i try to add the system("pause"); and the return 0;
but when i try to compile with them it doesnt work >.<
P.S.: it works without them but i cant see the result.
Last edited on
Hello, can anyone HELP ME !?!
I cant do system("pause"); or return 0; to my program ... how can i stop it from closing fast ??
i saw that post. i tryed the
1
2
3
#include<limits>
 cout << "Press ENTER to continue...";
 cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

DOESNT WORK FOR MEH !?!

error on compile: expected constructor, destructor, or type conversion before '<<' token `,' or `;' before '<<' token

EVEN IF I ADD , and ; or << IT GIVES ME SAME MESSAGE !?!
Last edited on
PLZ REPLY BACK! I DIDNT SOLVED THE PROBLEM YET !?!
Last edited on
Are you sure you added the #include <limits> at the top of your file, and the cout and cin.ignore calls at the end of your int main()-function?

Also, 0-b can be written as -b (since - is a unary operator, too). A smart compiler should pick this out, but it's a bad practice, typing things you don't need.
Last edited on
this is my actuall stage of program, and my SYSTEM("PAUSE"); doesnt work

P.S.: the messages are wrote in my language. Im not in a good mood to translate all >.<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a,b,c,delta,x1,x2,x;
    cout<<"Ecuatia de gradul 2.\n";
    cout<<"Introduceti a=";
    cin>>a;
    cout<<"Introduceti b=";
    cin>>b;
    cout<<"Introduceti c=";
    cin>>c;
    if (a!=0) 
    {
              delta=b*b-4*a*c;
              cout<<"delta="<<delta<<endl;
              if (delta<0) 
              {
                           cout<<"x nu apartine R, x apartine C."<<endl;
              }
              else if (delta==0)
                   {
                               x1=x2=-b/a;
                               cout<<"x1=x2="<<x1<<endl;
                   }
                   else 
                   {
                        x1=(0-b-sqrt(delta))/2*a;
                        cout<<"x1="<<x1<<endl;
                        x2=(0-b+sqrt(delta))/2*a;
                        cout<<"x2"<<x2<<endl;
                   }
              }
    else if (b!=0)
         {
                  x=-c/b;
                  cout<<"x="<<x<<endl;
         }
         else if (c==0) cout<<"Ecuatie nedeterminata\n";
              else cout<<"Ecuatie imposibila\n";
    }
    system("pause");
    return 0;


and the error sais:
1
2
3
4
row 43: expected constructor, destructor, or type conversion before '(' token
row 43: expected `,' or `;' before '(' token 
row 44: expected unqualified-id before "return" 
row 45: expected declaration before '}' token 


and it gives me 1-2 more like this !!

question: WHY ???
Last edited on
PLEASE, I BEG YOU, CAN YOU TELL ME WHAT IS WRONG IN MY PROGRAM ?? PLEASE !?!
WHAT DOES CONSTRUCTOR, DECONSTRUCTOR MEAN ???
IS THERE REALLY NEEDED ???

P.S.: I USE DEV-C++ 4.9.9.2

HELP ME BEFORE I GO CRAZY !?!
Because you did not end the main() function call, add a } to the end of your program. (Because of this, your program will not recognize the main function, and will state that return is unexpected and what not.
What Galik said. (Didn't notice the } above the system call, try indenting your code better).
Last edited on
try including <cstdlib> and moving line 42 to after line 44.
THANKS !
Pages: 12