|
-
Oct 5th, 2004, 10:48 PM
#1
Thread Starter
New Member
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.
-
Oct 6th, 2004, 01:24 AM
#2
Thread Starter
New Member
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 */
-
Oct 6th, 2004, 12:30 PM
#3
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.
-
Oct 6th, 2004, 03:02 PM
#4
Frenzied Member
[edited by sunburnt]
Code:
scanf( "%s", &name ); //<<------ FIX
Your going to have a couple of other issues after that. Like an infinite loop.
[Edited by MartinLiss]
Last edited by sunburnt; Aug 7th, 2006 at 12:57 PM.
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

-
Oct 6th, 2004, 08:25 PM
#5
Thread Starter
New Member
Originally posted by CornedBee
Please use [code][/code] tags when posting code.
well, that was a typo, that wasnt an actual problem, and yeah yrou right about the infinite loop.. can you correct my logic? [Edited by MartinLiss]
Last edited by MartinLiss; Aug 7th, 2006 at 12:37 PM.
-
Oct 7th, 2004, 10:24 AM
#6
Frenzied Member
[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');
You are also going to want to change where you are putting values into your pointers to be like this:
Code:
*(hits) = a;
or
*(hits) += a;
Last edited by MartinLiss; Aug 7th, 2006 at 12:37 PM.
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

-
Oct 7th, 2004, 10:37 AM
#7
Thread Starter
New Member
Originally posted by Technocrat
[edited by sunburnt]
Here is what you can do to exit the loop
Code:
char cMore;
printf("Enter More: ");
scanf(" %c", &cMore );
....
while ( cMore != 'n' && cMore != 'N');
You are also going to want to change where you are putting values into your pointers to be like this:
Code:
*(hits) = a;
or
*(hits) += a;
ill try that..
Last edited by sunburnt; Aug 7th, 2006 at 12:58 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|