...

View Full Version : I need C help writting a program



Ultima-X97
09-22-2009, 06:13 AM
#define _CRT_SECURE_NO_WARNING
#include <stdio.h>
#include <math.h>
void main(void)
{
float a,b,c;
printf("Enter A \n");
scanf("%f",&a);
printf("Enter B \n");
scanf("%f",&b);
printf("Enter C \n");
scanf("%f",&c);
}
if(A==0)
{
x=-c/b;
printf("%f",x);
}
else
{
disc = b*b-4*a*c
if(disc==0)
{
x=-b/2a;
printf("%f",x);
}
else
{
if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("%f %f", x1, x2)
}
else
printf("No real roots");
}
}



I get 7 errors when I try to excute this...how would I go about making this work. I am a bit new to C.

Errors:

Error 4 error C2059: syntax error : 'if' c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 16 quad
Error 5 error C2143: syntax error : missing ';' before '{' c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 17 quad
Error 6 error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 17 quad
Error 7 error C2059: syntax error : 'else' c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 21 quad
Error 8 error C2143: syntax error : missing ';' before '{' c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 22 quad
Error 9 error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 22 quad
Error 10 error C2059: syntax error : 'bad suffix on number' c:\users\owner\documents\visual studio 2008\projects\quad\quad\quad.cpp 26 quad

Fou-Lu
09-22-2009, 06:46 AM
Its trying to interpret the if and else as individual functions, which of course have invalid signatures (not to mention they use construct names). Move that into the main method.

Next, A is undefined, I'm thinking its supposed to be a.
And there are a few others that are undefined, the first is disc, second is x, third is x1 and final is x2. Remember in c that these all need to be predeclared.

Things like 2a and -c won't work either, you'll need to specify it as 2 * a and c * -1 (or ~c + 1) respectively. I'm pretty certain about the -c one not working, though it has been awhile since I've used C.

Ultima-X97
09-22-2009, 04:10 PM
Thank you, you were a big help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum