HELP I'm having compile errors.
Author |
Message |
William Byr #1 / 12
|
 HELP I'm having compile errors.
I'm a math and physics major, but they say I have to take a C class to graduate, so here's my problem: I wrote this program and I get these errors: program2.c(17) : warning C4013: 'While' undefined; assuming extern returning int program2.c(18) : error C2143: syntax error : missing ';' before '{' program2.c(22) : warning C4013: 'If' undefined; assuming extern returning int program2.c(23) : error C2146: syntax error : missing ';' before identifier 'package_cost' program2.c(23) : error C2065: 'package_cost' : undeclared identifier program2.c(23) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data program2.c(25) : error C2146: syntax error : missing ';' before identifier 'package_cost' program2.c(25) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data program2.c(27) : error C2146: syntax error : missing ';' before identifier 'package_cost' program2.c(27) : error C2065: 'weight' : undeclared identifier program2.c(27) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data program2.c(32) : error C2146: syntax error : missing ';' before identifier 'package_cost' program2.c(32) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data Error executing cl.exe. Here's the code that I've written: /* William Byrd */ /* Program #2 */ #include <stdio.h> #define MEDIUM 20.00 /*This is the base charge for medium packages */ #define LARGE 36.25 /*This is the base charge for large packages */ int main () { int length, width, height, volume, total_volume=0, package_cnt=0; /*Init integer vars */ float total_weight=0, package_weight, package_charge=0, total_charge=0; /*Init float vars*/ printf("************************************************************\n"); printf("Enter package weight in pounds (-1.0 to end):"); scanf ("%f",&package_weight); While (package_weight != -1.0) { printf("Enter package dimensions in inches:"); scanf("%d%d%d", &length, &width, &height); If (package_weight > 10.0) /*These IF statements determine cost*/ package_cost = ((package_weight - 10) * 2.00) + LARGE; If (package_weight > 5.0 && package_weight <= 10.0) package_cost = ((package_weight - 5) * 3.25)+MEDIUM; If (package_weight <= 5.0) package_cost = weight * 4.00; volume = length * width * height; /*Calculate the volume of package*/ If (volume > 2000) /*Determine if extra volume charge applies*/ package_cost = package_cost + ((volume - 2000) * .001); printf("Charge = $%.2f",package_cost); printf("************************************************************\n"); package_cnt ++; /*These next lines increase the counters & totals */ total_weight = total_weight + package_weight; total_volume = total_volume + volume; total_charge = total_charge + package_charge; printf("Enter package weight in pounds (-1.0 to end):"); scanf ("%f",&package_weight); } printf("\n"); printf("Total number of packages = %d\n",package_cnt); printf("Total weight of packages = %f.1\n",total_weight); printf("Total volume of packages = %d\n",total_volume); return 0; Quote: }
If it's not obvious by the name, this is only my 2nd program in C. I'm sure there are errors in the code, but I just can't find them. Please feel free to respond to my regular e-mail. Just drop the nospam part out. Thanks, Bill
|
Mon, 22 Nov 2004 04:00:10 GMT |
|
 |
Kevin Hand #2 / 12
|
 HELP I'm having compile errors.
Quote:
> I'm a math and physics major, but they say I have to take a C class to > graduate, so here's my problem: > I wrote this program and I get these errors: > program2.c(17) : warning C4013: 'While' undefined; assuming extern returning > int > program2.c(18) : error C2143: syntax error : missing ';' before '{' > program2.c(22) : warning C4013: 'If' undefined; assuming extern returning > int > program2.c(23) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(23) : error C2065: 'package_cost' : undeclared identifier > program2.c(23) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(25) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(25) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(27) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(27) : error C2065: 'weight' : undeclared identifier > program2.c(27) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(32) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(32) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > Error executing cl.exe. > Here's the code that I've written: > /* William Byrd */ > /* Program #2 */ > #include <stdio.h> > #define MEDIUM 20.00 /*This is the base charge for medium packages */ > #define LARGE 36.25 /*This is the base charge for large packages */ > int main () > { > int length, width, height, volume, total_volume=0, package_cnt=0; /*Init > integer vars */ > float total_weight=0, package_weight, package_charge=0, total_charge=0; > /*Init float vars*/ > printf("************************************************************\n"); > printf("Enter package weight in pounds (-1.0 to end):"); > scanf ("%f",&package_weight); > While (package_weight != -1.0) > { > printf("Enter package dimensions in inches:"); > scanf("%d%d%d", &length, &width, &height); > If (package_weight > 10.0) /*These IF statements > determine cost*/ > package_cost = ((package_weight - 10) * 2.00) + LARGE; > If (package_weight > 5.0 && package_weight <= 10.0) > package_cost = ((package_weight - 5) * 3.25)+MEDIUM; > If (package_weight <= 5.0) > package_cost = weight * 4.00; > volume = length * width * height; /*Calculate the volume of package*/ > If (volume > 2000) /*Determine if extra volume charge applies*/ > package_cost = package_cost + ((volume - 2000) * .001); > printf("Charge = $%.2f",package_cost); > printf("************************************************************\n"); > package_cnt ++; /*These next lines increase the counters & totals */ > total_weight = total_weight + package_weight; > total_volume = total_volume + volume; > total_charge = total_charge + package_charge; > printf("Enter package weight in pounds (-1.0 to end):"); > scanf ("%f",&package_weight); > } > printf("\n"); > printf("Total number of packages = %d\n",package_cnt); > printf("Total weight of packages = %f.1\n",total_weight); > printf("Total volume of packages = %d\n",total_volume); > return 0; > } > If it's not obvious by the name, this is only my 2nd program in C. I'm sure > there are errors in the code, but I just can't find them. Please feel free > to respond to my regular e-mail. Just drop the nospam part out.
case matters, 'If' you didn't notice 'While' coding in C.
|
Mon, 22 Nov 2004 04:14:13 GMT |
|
 |
William Byr #3 / 12
|
 HELP I'm having compile errors.
Oh yeah....I remember something like that being mentioned in class....I guess I have to get over that instinct to hit the shift key... Thanks! Quote:
> > I'm a math and physics major, but they say I have to take a C class to > > graduate, so here's my problem: > > I wrote this program and I get these errors: > > program2.c(17) : warning C4013: 'While' undefined; assuming extern returning > > int > > program2.c(18) : error C2143: syntax error : missing ';' before '{' > > program2.c(22) : warning C4013: 'If' undefined; assuming extern returning > > int > > program2.c(23) : error C2146: syntax error : missing ';' before identifier > > 'package_cost' > > program2.c(23) : error C2065: 'package_cost' : undeclared identifier > > program2.c(23) : warning C4244: '=' : conversion from 'double ' to 'int ', > > possible loss of data > > program2.c(25) : error C2146: syntax error : missing ';' before identifier > > 'package_cost' > > program2.c(25) : warning C4244: '=' : conversion from 'double ' to 'int ', > > possible loss of data > > program2.c(27) : error C2146: syntax error : missing ';' before identifier > > 'package_cost' > > program2.c(27) : error C2065: 'weight' : undeclared identifier > > program2.c(27) : warning C4244: '=' : conversion from 'double ' to 'int ', > > possible loss of data > > program2.c(32) : error C2146: syntax error : missing ';' before identifier > > 'package_cost' > > program2.c(32) : warning C4244: '=' : conversion from 'double ' to 'int ', > > possible loss of data > > Error executing cl.exe. > > Here's the code that I've written: > > /* William Byrd */ > > /* Program #2 */ > > #include <stdio.h> > > #define MEDIUM 20.00 /*This is the base charge for medium packages */ > > #define LARGE 36.25 /*This is the base charge for large packages */ > > int main () > > { > > int length, width, height, volume, total_volume=0, package_cnt=0; /*Init > > integer vars */ > > float total_weight=0, package_weight, package_charge=0, total_charge=0; > > /*Init float vars*/
printf("************************************************************\n"); Quote: > > printf("Enter package weight in pounds (-1.0 to end):"); > > scanf ("%f",&package_weight); > > While (package_weight != -1.0) > > { > > printf("Enter package dimensions in inches:"); > > scanf("%d%d%d", &length, &width, &height); > > If (package_weight > 10.0) /*These IF statements > > determine cost*/ > > package_cost = ((package_weight - 10) * 2.00) + LARGE; > > If (package_weight > 5.0 && package_weight <= 10.0) > > package_cost = ((package_weight - 5) * 3.25)+MEDIUM; > > If (package_weight <= 5.0) > > package_cost = weight * 4.00; > > volume = length * width * height; /*Calculate the volume of package*/ > > If (volume > 2000) /*Determine if extra volume charge applies*/ > > package_cost = package_cost + ((volume - 2000) * .001); > > printf("Charge = $%.2f",package_cost);
printf("************************************************************\n"); Quote: > > package_cnt ++; /*These next lines increase the counters & totals */ > > total_weight = total_weight + package_weight; > > total_volume = total_volume + volume; > > total_charge = total_charge + package_charge; > > printf("Enter package weight in pounds (-1.0 to end):"); > > scanf ("%f",&package_weight); > > } > > printf("\n"); > > printf("Total number of packages = %d\n",package_cnt); > > printf("Total weight of packages = %f.1\n",total_weight); > > printf("Total volume of packages = %d\n",total_volume); > > return 0; > > } > > If it's not obvious by the name, this is only my 2nd program in C. I'm sure > > there are errors in the code, but I just can't find them. Please feel free > > to respond to my regular e-mail. Just drop the nospam part out. > case matters, 'If' you didn't notice 'While' coding in C.
|
Mon, 22 Nov 2004 05:05:40 GMT |
|
 |
Eric G. Mille #4 / 12
|
 HELP I'm having compile errors.
Quote: > While (package_weight != -1.0)
s/While/while/g Note: It is generally a really bad idea to compare floating point numbers for equality/inequality due to the imprecise nature of the data type. Better to use something like " while (package_weight > 0.0) ". You probably want to be checking your scanf() calls return the number of expected conversions as well.
|
Mon, 22 Nov 2004 12:59:55 GMT |
|
 |
William Byr #5 / 12
|
 HELP I'm having compile errors.
I didn't know of another way to compare it. The assignment has for us to have -1.0 as the sentinal value. I'm also not really sure what you mean by checking the scanf conversions. Are you talking about putting something like: scanf("%.1f"&varname); to limit the number of input items?
Quote:
> > While (package_weight != -1.0) > s/While/while/g > Note: It is generally a really bad idea to compare floating point numbers for > equality/inequality due to the imprecise nature of the data type. Better to > use something like " while (package_weight > 0.0) ". You probably want to > be checking your scanf() calls return the number of expected conversions
as well.
|
Mon, 22 Nov 2004 15:34:44 GMT |
|
 |
Marcus Brut #6 / 12
|
 HELP I'm having compile errors.
Quote:
> I'm a math and physics major, but they say I have to take a C class to > graduate, so here's my problem: > I wrote this program and I get these errors: > program2.c(17) : warning C4013: 'While' undefined; assuming extern returning > int > program2.c(18) : error C2143: syntax error : missing ';' before '{' > program2.c(22) : warning C4013: 'If' undefined; assuming extern returning > int > program2.c(23) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(23) : error C2065: 'package_cost' : undeclared identifier > program2.c(23) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(25) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(25) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(27) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(27) : error C2065: 'weight' : undeclared identifier > program2.c(27) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > program2.c(32) : error C2146: syntax error : missing ';' before identifier > 'package_cost' > program2.c(32) : warning C4244: '=' : conversion from 'double ' to 'int ', > possible loss of data > Error executing cl.exe. > Here's the code that I've written: > /* William Byrd */ > /* Program #2 */ > #include <stdio.h> > #define MEDIUM 20.00 /*This is the base charge for medium packages */ > #define LARGE 36.25 /*This is the base charge for large packages */ > int main () > { > int length, width, height, volume, total_volume=0, package_cnt=0; /*Init > integer vars */ > float total_weight=0, package_weight, package_charge=0, total_charge=0; > /*Init float vars*/ > printf("************************************************************\n"); > printf("Enter package weight in pounds (-1.0 to end):"); > scanf ("%f",&package_weight); > While (package_weight != -1.0)
[Snip] float numbers cannot be compered for equality or inequality. The above statement works (also replace While with while), because -1.0 can be represented exactly in binary system. It's really much better to do the following while ( fabs(package_weight+1.0)<EPSILON)... and in general if you want to compare two floats a, b for equality just you can do something like fabs(a-b)<EPSILON where you have to define somewhere the desired precision EPSILON marcus brutus. -------------- et tu brute?
|
Mon, 22 Nov 2004 18:39:15 GMT |
|
 |
Ben Pfaf #7 / 12
|
 HELP I'm having compile errors.
Quote:
> > Note: It is generally a really bad idea to compare floating point numbers > for > > equality/inequality due to the imprecise nature of the data > type. [...] > I didn't know of another way to compare it. The assignment has for us to > have -1.0 as the sentinal value.
It should be fine in that case. Floating-point comparisons are inaccurate in general, but if you're not performing arithmetic before the comparison then it should be fine. -- "Given that computing power increases exponentially with time, algorithms with exponential or better O-notations are actually linear with a large constant." --Mike Lee
|
Mon, 22 Nov 2004 23:36:52 GMT |
|
 |
Mark McIntyr #8 / 12
|
 HELP I'm having compile errors.
On Thu, 06 Jun 2002 07:34:44 GMT, "William Byrd" Quote:
>I didn't know of another way to compare it. The assignment has for us to >have -1.0 as the sentinal value. I'm also not really sure what you mean by >checking the scanf conversions. Are you talking about putting something >like: >scanf("%.1f"&varname); >to limit the number of input items?
you can also check the return from scanf, to see if it scanned enough items. But in general, avoid scanf. fgets followed by sscanf or something similar is much safer. -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Tue, 23 Nov 2004 05:47:28 GMT |
|
 |
Eric G. Mille #9 / 12
|
 HELP I'm having compile errors.
Quote: > I didn't know of another way to compare it. The assignment has for us to > have -1.0 as the sentinal value. I'm also not really sure what you mean by > checking the scanf conversions. Are you talking about putting something > like: > scanf("%.1f"&varname); > to limit the number of input items?
<nitpick>Please don't top post. Comments should follow the context they refer to</nitpick> For the scanf family: int read_datafile_line (FILE *ifp, int *id, double *x, double *y) { int howmany; howmany = fscanf (ifp, "%d %lf %lf", id, x, y); if (howmany < 3) { /* Error Will Robinson */ } else { /* Mostly okay, *but* maybe overflow errors... */ } return howmany == 3; Quote: }
|
Tue, 23 Nov 2004 11:03:09 GMT |
|
 |
Martin Ambuh #10 / 12
|
 HELP I'm having compile errors.
Quote:
> I'm a math and physics major, but they say I have to take a C class to > graduate, so here's my problem: > I wrote this program and I get these errors:
Start by learning to spell: Quote: > program2.c(17) : warning C4013: 'While' undefined; assuming extern returning
'while' is not spelt 'While'. Quote: > program2.c(22) : warning C4013: 'If' undefined; assuming extern returning > int
'if' is not spelt 'If'.
|
Tue, 23 Nov 2004 15:02:45 GMT |
|
 |
those who know me have no need of my nam #11 / 12
|
 HELP I'm having compile errors.
in comp.lang.c i read: Quote: >Groovy hepcat William Byrd was jivin' on Wed, 05 Jun 2002 20:00:10 GMT >in comp.lang.c. >>Please feel free to respond to my regular e-mail. > No. Post here, read here! Please don't ask for direct e-mail without >good reason.
`feel free to e-mail' is permission to respond by e-mail, which is differnt than a request to respond by e-mail. -- bringing you boring signatures for 17 years
|
Thu, 25 Nov 2004 13:14:50 GMT |
|
 |
Albert van der Hor #12 / 12
|
 HELP I'm having compile errors.
<SNIP a program about weighting.> Quote: >> While (package_weight != -1.0) > [Snip] > float numbers cannot be compered for equality or inequality. > The above statement works (also replace While with while), because > -1.0 can be represented exactly in binary system. > It's really much better to do the following > while ( fabs(package_weight+1.0)<EPSILON)...
While in general this is good advise, you are not paying attention. Antigravity is not here yet. -1.0 can't be anything but a sentinel value. So a better advice is : " /* A value that indicates an invalid weight. (Weights are positive) */ #define INVALID -1.0 ... while ( INVALID != package_weight ) " -- Groetjes Albert -- Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS To suffer is the prerogative of the strong. The weak -- perish.
|
Sat, 04 Dec 2004 08:12:18 GMT |
|
|
|