Quadratic Formula

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Quadratic Formula

 
0
  #1
Jul 24th, 2008
I am trying to create a program that gets the solution of a quadratic formula, but i keep getting an error and i cant figure it out.

1>quad.cpp(14) : error C2440: '=' : cannot convert from 'int (__cdecl *)(void)' to 'int'
1> There is no context in which this conversion is possible
1>quad.cpp(16) : error C2440: '=' : cannot convert from 'int (__cdecl *)(void)' to 'int'
1> There is no context in which this conversion is possible
1>quad.cpp(18) : error C2440: '=' : cannot convert from 'int (__cdecl *)(void)' to 'int'
1> There is no context in which this conversion is possible
1>quad - 3 error(s), 21 warning(s)

Here is the program
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "simpio.h"
  4. #include "strlib.h"
  5. #include "random.h"
  6. #include "math.h"
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int a, b, c, x, y, z;
  12.  
  13. printf("Enter A: ");
  14. a = GetInteger;
  15. printf("Enter B: ");
  16. b = GetInteger;
  17. printf("Enter c: ");
  18. c = GetInteger;
  19. z = (b*b-4*a*c)
  20. x = ((-b - z)/(2*a));
  21. y = ((-b + z)/(2*a));
  22.  
  23. if (x = y)
  24. {
  25. printf("There is one solution: %d", x);
  26. }
  27. else if (x != y)
  28. {
  29. printf("There are two solutions: %d and %d", x, y);
  30. }
  31. else
  32. {
  33. printf("There are no solutions");
  34. }
  35. system("pause");
  36. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Quadratic Formula

 
0
  #2
Jul 24th, 2008
There could be a possibility that if "GetInteger" is a function it is not of integer type. Otherwise if "GetInteger" is not a function then it is not declared
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Quadratic Formula

 
0
  #3
Jul 24th, 2008
pLike, you should really consider looking at on how to indentent you code first. Well the error message you get there is basiclly specifying that GetInteger cannot be assigned to the interger. It donst know what the RHS is? Seems to me like it is function or is it macro if there was no brackets ????

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a, b, c, x, y, z;
  6.  
  7. printf("Enter A: ");
  8. a = GetInteger();
  9.  
  10. printf("Enter B: ");
  11. b = GetInteger();
  12.  
  13. printf("Enter c: ");
  14. c = GetInteger();
  15.  
  16. z = ( b * b - 4 * a * c );
  17. x = ( ( -b - z ) / ( 2 * a ) );
  18. y = ( ( -b + z ) / ( 2 * a ) );
  19.  
  20. if ( x == y )
  21. printf("There is one solution: %d", x);
  22. else if ( x != y )
  23. printf("There are two solutions: %d and %d", x, y);
  24. else
  25. printf("There are no solutions");
  26.  
  27. system("pause");
  28. return 0;
  29. }

And the in the if statment, there two two equals opeator not the assignment operator if ( x == y )
ssharish
Last edited by ssharish2005; Jul 24th, 2008 at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Re: Quadratic Formula

 
0
  #4
Jul 25th, 2008
Ok... Thanks that solved that, but i have a new problem, i can't get the decimals with the number here is the improved program.
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "simpio.h"
  4. #include "strlib.h"
  5. #include "random.h"
  6. #include "math.h"
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int a, b, c, x, y, d;
  12.  
  13. printf("Enter A: ");
  14. a = GetInteger();
  15.  
  16. printf("Enter B: ");
  17. b = GetInteger();
  18.  
  19. printf("Enter C: ");
  20. c = GetInteger();
  21.  
  22. d = (b*b-4*a*c)^(1/2);
  23. x = ((-b - d)/(2.0*a));
  24. y = ((-b + d)/(2.0*a));
  25.  
  26. if (x == y)
  27. printf("There is one solution: %d\n", x);
  28. else if (x != y)
  29. printf("There are two solutions: %d and %d\n", x, y);
  30. else if (d <= 0)
  31. printf("There are no solutions.\n");
  32. else
  33. printf("There are no solutions.\n");
  34.  
  35. system("pause");
  36. return 0;
  37. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Quadratic Formula

 
0
  #5
Jul 25th, 2008
iostream isn't in C. I think you have quotes where you want <> in your #include for math.h, and you can't use "using namespace std" in C. Maybe you're not using a C compiler.

The problem with the decimals is that you've declared all of your variables as integers and integers can't hold decimals. Try declaring x, y, d as doubles or floats.

I'm also suspicious of this line:

  1. d = (b*b-4*a*c)^(1/2);

^ is the bitwise XOR operator and I think (1 / 2) is going to be 0 due to integer division.
Last edited by VernonDozier; Jul 25th, 2008 at 1:01 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Quadratic Formula

 
0
  #6
Jul 25th, 2008
You're getting C++ errors. C++ is a lot stricter. Try using a C compiler.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Quadratic Formula

 
0
  #7
Jul 25th, 2008
Originally Posted by Clockowl View Post
You're getting C++ errors. C++ is a lot stricter. Try using a C compiler.

You have that backwards. These lines:

  1. #include <iostream>
  2. using namespace std;

are fine with a C++ compiler, but cannot be used with a C compiler.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Quadratic Formula

 
0
  #8
Jul 25th, 2008
"cannot convert from" is a C++ (and in general, OOP) only error afaik.
Last edited by Clockowl; Jul 25th, 2008 at 11:16 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Quadratic Formula

 
1
  #9
Jul 25th, 2008
>You're getting C++ errors. C++ is a lot stricter. Try using a C compiler.
So your advice is to ignore the warning and let the problem silently continue. Brilliant.

>"cannot convert from" is a C++ (and in general, OOP) only error afaik.
You get it in C as well if you do something stupid...just like C++.
Last edited by Narue; Jul 25th, 2008 at 11:25 am.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Quadratic Formula

 
0
  #10
Jul 25th, 2008
Originally Posted by Clockowl View Post
"cannot convert from" is a C++ (and in general, OOP) only error afaik.

Ah, OK, you are referring to these lines, perhaps?

  1. x = ((-b - d)/(2.0*a));
  2. y = ((-b + d)/(2.0*a));

I get a warning in C++ (not an error) which says "Warning: Converting from 'int' to 'double'". I don't get the "cannot convert from" error. It converts it using the C++ compiler, but throws out the warning and it's the programmer's decision about whether to pay attention to the warning or not. Maybe it's an error instead of a warning if you have certain compiler flags. In this case, it's a warning that the OP should definitely pay attention to and not ignore. You're right, though, given that this is the C forum and hence the OP should be using a C compiler, there will be no warning whatsoever on these conversions, or at least there wasn't on my C compiler.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC