PDA

View Full Version : C Error Help :\



Runescapian321
02-18-2008, 09:27 PM
I have to make a thing that solves quadratic equations. They gave me the algorithm, and I have to make the program. So here is what I have...


#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include <math.h>

int power(int num1,int num2)
{

int num3, num4,num5,num6, count;

count = 1;
num3 = num1;
num4 = num1;
num5 = num2;
num6 = num2;

if(num1<0)
{
num1=num1-num1-num1;
}
if(num2<0)
{
num2=num2-num2-num2;
}

while(count < num2)
{
num1 = num1 * num3;
count = count + 1;
num4 = num1;

}

if(num5<0)
{
if(num6=0)
{
num4=1;
}


}
else
{
if(num6==0)
{
num4=1;
}
return num4;
}




}

int step14(int b, int D, int a)
{
int x1,x2;
x1 = (-b + sqrt(D)) /2a;
x2 = (-b - sqrt(D)) /2a;
}

int solveit()
{
int a,b,c,D,x,x1,x2,num;

printf("Enter A: ");
a = GetInteger();
printf("Enter B: ");
b = GetInteger();
printf("Enter C: ");
c = GetInteger();

if (a == 0)
{
if (b == 0)
{
printf("no solution");
exit(0);
}
else
{
x = -c/b;
}
printf("the equation is not quadratic and the solution is: %d", x);
exit(0);
}
num = power(b,2);
D = num-4*a*c;
if (D<0)
{
printf("There is no real solution");
}
if (D==0)
{
step14(b,D,a);
exit(0);
}
x1 = -b/2a;
printf("One solution: %d", x1);
}


int main()
{
solveit();
}


But it gives me these Errors


1>c:\users\tadas\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(63) : error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(581): could be 'long double sqrt(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(533): or 'float sqrt(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(128): or 'double sqrt(double)'
1> while trying to match the argument list '(int)'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(63) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(63) : error C2146: syntax error : missing ';' before identifier 'a'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(64) : error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(581): could be 'long double sqrt(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(533): or 'float sqrt(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(128): or 'double sqrt(double)'
1> while trying to match the argument list '(int)'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(64) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(64) : error C2146: syntax error : missing ';' before identifier 'a'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(103) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\\documents\visual studio 2005\myprojects\quad\quad\quad.cpp(103) : error C2146: syntax error : missing ';' before identifier 'a'

I dont understand the C2059 error (I assume the C2146 will be fixed once I fix C2059), and the sqrt error confuses me. I tried making D a double and making it 'double sqrt(D)', but then it gave me a bunch of new errors.

And also a question with 'exit(0)'. When I use that will the program exit out of the procedure it's in, will it exit out of the program, or will it do something else entirely?

Thanks in advance :)

Wizzup?
02-18-2008, 09:30 PM
Well, I cannot help you alot, but I don't think you can do this:


int step14(int b, int D, int a)
{
int x1,x2;
x1 = (-b + sqrt(D)) /2a;
x2 = (-b - sqrt(D)) /2a;
}
/2a should be /2*a as far as I know... I Might be wrong though, I'm not a C++ expert.

Runescapian321
02-18-2008, 09:53 PM
Ooh yeah forgot about that, thanks. I fixed that exact same thing in another part of the program earlier but didn't realize it in there :duh:

I guess I'll try experimenting with the other error (with the sqrt)

EDIT:

Got it to compile but it's not working right....*sigh* time to fix....

ElPolloFeo92
02-22-2008, 01:59 AM
If you still needed an answer, exit( ) completely exits out of the program.

Runescapian321
02-22-2008, 05:16 AM
exit(0) seemed to work fine for me too. What does the number in the parentheses mean?

Yakman
02-22-2008, 12:12 PM
the error code
0 means the program exited successfully
1 often used for program exiting with failure, but any non-zero number is failure
but don't go higher then 255, cause some computers might not handle it very well

i think in the c standard library, there are two pre-defined macros called EXIT_SUCCESS and EXIT_FAILURE



#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1


so just do

exit(EXIT_SUCCESS)

or if you're in the main() method

return EXIT_SUCCESS