|
|
#1 |
|
New Member
Join Date: Oct 04
Posts: 4
![]() |
compile error, someone please pioint out my mistake
hey guys, im in my first c programming class, and this si my first program using functions. heres the code i wrote, and the compiler error i get is:
:In function 'main': :87: parse error before 'int' heres the code.. please, reply to me by email as son as you can find the error, or my AIM. [edited by sunburnt] /* this program keeps track of batting statistics for baseball, it reads the nameof the batter,the overall numberof hits, at-bats, and homeruns. it willreadthe numbers for all of these in a loop, then it will stop at -1 hits. the output will be the batter's name, the totals for all 3, and the computed batting average shown to 3 decimal places. program written by Aaron Russell */ #include <stdio.h> /* declare variables for main method */ int numbHits, numbAtbats, numbHomeruns; float battingAverage; /* this function reads in all the data to be inputed, including Hits, at-bats, and homeruns */ void inputs( int *hits, int *atbats, int *homeruns ) { /* declare and initialize stat counters, a for hits, b for at bats, c for homeruns */ int a=0, b=0, c=0; /* input the overall no. of hits, at-bats, and homeruns */ printf( "Enter overall hits, at-bats, and homeruns:\n" ); scanf( "%d%1d%1d, hits, atbats, homeruns" ); /* do/while loop to enter in the additional batting statistics, and end when -1 is entered for hits */ do { /* while loop to check for input errors, make sure no. of hits and at-bats always exceeds no. of homeruns */ while ( c <= a && a <= b && c <= b ) { /* counter action adds additonal hits, at-bats, and homeruns to overall stats for each variable */ hits += a; atbats +=b; homeruns +=c; /* input the no. of hits, at-bats, and homeruns */ printf( "Enter hits, at-bats, and homeruns:\n" ); scanf( "%d%1d%1d, &a, &b, &c" ); } /* end while */ } while ( a != -1 ); /* end do/while */ } /* end inputs function */ /* function average calculates batting average */ float average( int hits, int atbats ) { /* declare bataverage as floating point */ float bataverage; /* calculate the batting average */ bataverage = (float) hits / atbats; /* return to indicate what average calculates */ return bataverage; } /* end function average */ /* main function incorporates the two previous functions to output a single line taht summarizes the data inputed for that player, and his batting average */ int main () { /* variable to store the name */ char name; /* input the name of the batter */ printf( "Enter name:\n" ); scanf( "%c, &name" ); /* intitialize ints and float for displaying hits, at-bats, homeruns*/ int numbHits = 0; int numbAtbats = 0; int numbHomeruns = 0; float battingAverage; /* generate input data using function inputs */ inputs( &numbHits, &numbAtbats, &numbHomeruns ); /* calculate average using function average */ battingAverage = average( numbHits, numbAtbats ); /* Prints the summary line that has the results for Name, hits, at-bats, homeruns, and batting average */ printf( "%c has %d hits, %d at-bats, %d home runs, and a batting average of %1.3f\n", name, numbHits, numbAtbats, numbHomeruns, battingAverage ); return 0; } /* end main function */ Last edited by sunburnt; Aug 7th, 2006 at 12:57 PM. |
|
|
|
|
|
#2 |
|
New Member
Join Date: Oct 04
Posts: 4
![]() |
my bad guys.. heres the updated code.. made a few mistakes in the first post:
#include <stdio.h> /* this function reads in all the data to be inputed, including Hits, at-bats, and homeruns */ void inputs( int *hits, int *atbats, int *homeruns ) { /* declare and initialize stat counters, a for hits, b for at bats, c for homeruns */ int a=0, b=0, c=0; /* input the overall no. of hits, at-bats, and homeruns */ printf( "Enter overall hits, at-bats, and homeruns:\n" ); scanf( "%d%1d%1d", hits, atbats, homeruns ); /* do/while loop to enter in the additional batting statistics, and end when -1 is entered for hits */ do { /* while loop to check for input errors, make sure no. of hits and at-bats always exceeds no. of homeruns */ while ( c <= a && a <= b && c <= b ) { /* counter action adds additonal hits, at-bats, and homeruns to overall stats for each variable */ hits += a; atbats +=b; homeruns +=c; /* input the no. of hits, at-bats, and homeruns */ printf( "Enter hits, at-bats, and homeruns:\n" ); scanf( "%d%1d%1d", &a, &b, &c ); } /* end while */ } while ( a != -1 ); /* end do/while */ } /* end inputs function */ /* function average calculates batting average */ float average( int hits, int atbats ) { /* declare bataverage as floating point */ float bataverage; /* calculate the batting average */ bataverage = (float) hits / (float) atbats; /* return to indicate what average calculates */ return bataverage; } /* end function average */ /* main function incorporates the two previous functions to output a single line taht summarizes the data inputed for that player, and his batting average */ int main () { /* variable to store the name */ char name[49]; /* input the name of the batter */ printf( "Enter name:\n" ); scanf( "%s, &name" ); /* intitialize ints and float for displaying hits, at-bats, homeruns*/ int numbHits = 0; int numbAtbats = 0; int numbHomeruns = 0; float battingAverage; /* generate input data using function inputs */ inputs( &numbHits, &numbAtbats, &numbHomeruns ); /* calculate average using function average */ battingAverage = average( numbHits, numbAtbats ); /* Prints the summary line that has the results for Name, hits, at-bats, homeruns, and batting average */ printf( "%s has %d hits, %d at-bats, %d home runs, and a batting average of %1.3f\n", name, numbHits, numbAtbats, numbHomeruns, battingAverage ); return 0; } /* end main function */ |
|
|
|
|
|
#3 |
|
Kitten
Join Date: Aug 01
Location: In a microchip!
Posts: 11,594
![]() ![]() ![]() ![]() ![]() ![]() |
Please use [code][/code] tags when posting code.
__________________
All the buzzt CornedBee"Writing specifications is like writing a novel. Writing code is like writing poetry." - Anonymous, published by Raymond Chen Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question. |
|
|
|
|
|
#4 |
|
Frenzied Member
Join Date: Jan 00
Location: I live in the 1s and 0s of everyones data streams
Posts: 1,024
![]() |
[edited by sunburnt]
Code:
scanf( "%s", &name ); //<<------ FIX [Edited by MartinLiss]
__________________
MSVS 6, .NET & .NET 2003 Pro I HATE MSDN with .NET & .NET 2003!!! Check out my sites: http://www.filthyhands.com http://www.techno-coding.com Last edited by sunburnt; Aug 7th, 2006 at 12:57 PM. |
|
|
|
|
|
#5 | |
|
New Member
Join Date: Oct 04
Posts: 4
![]() |
Quote:
Last edited by MartinLiss; Aug 7th, 2006 at 12:37 PM. |
|
|
|
|
|
|
#6 |
|
Frenzied Member
Join Date: Jan 00
Location: I live in the 1s and 0s of everyones data streams
Posts: 1,024
![]() |
[Edited by MartinLiss]
Here is what you can do to exit the loop Code:
char cMore;
printf("Enter More: ");
scanf(" %c", &cMore );
....
while ( cMore != 'n' && cMore != 'N');
Code:
*(hits) = a; or *(hits) += a;
__________________
MSVS 6, .NET & .NET 2003 Pro I HATE MSDN with .NET & .NET 2003!!! Check out my sites: http://www.filthyhands.com http://www.techno-coding.com Last edited by MartinLiss; Aug 7th, 2006 at 12:37 PM. |
|
|
|
|
|
#7 | |
|
New Member
Join Date: Oct 04
Posts: 4
![]() |
Quote:
Last edited by sunburnt; Aug 7th, 2006 at 12:58 PM. |
|
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|