To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Other Languages > C and C++

Reply Post New Thread
 
Thread Tools Display Modes
Old Oct 5th, 2004, 10:48 PM   #1
aaronrus
New Member
 
Join Date: Oct 04
Posts: 4
aaronrus is an unknown quantity at this point (<10)
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.
aaronrus is offline   Reply With Quote
Old Oct 6th, 2004, 01:24 AM   #2
aaronrus
New Member
 
Join Date: Oct 04
Posts: 4
aaronrus is an unknown quantity at this point (<10)
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 */
aaronrus is offline   Reply With Quote
Old Oct 6th, 2004, 12:30 PM   #3
CornedBee
Kitten
 
CornedBee's Avatar
 
Join Date: Aug 01
Location: In a microchip!
Posts: 11,594
CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)
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.
CornedBee is offline   Reply With Quote
Old Oct 6th, 2004, 03:02 PM   #4
Technocrat
Frenzied Member
 
Technocrat's Avatar
 
Join Date: Jan 00
Location: I live in the 1s and 0s of everyones data streams
Posts: 1,024
Technocrat is an unknown quantity at this point (<10)
[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]
__________________
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.
Technocrat is offline   Reply With Quote
Old Oct 6th, 2004, 08:25 PM   #5
aaronrus
New Member
 
Join Date: Oct 04
Posts: 4
aaronrus is an unknown quantity at this point (<10)
Quote:
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.
aaronrus is offline   Reply With Quote
Old Oct 7th, 2004, 10:24 AM   #6
Technocrat
Frenzied Member
 
Technocrat's Avatar
 
Join Date: Jan 00
Location: I live in the 1s and 0s of everyones data streams
Posts: 1,024
Technocrat is an unknown quantity at this point (<10)
[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;
__________________
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.
Technocrat is offline   Reply With Quote
Old Oct 7th, 2004, 10:37 AM   #7
aaronrus
New Member
 
Join Date: Oct 04
Posts: 4
aaronrus is an unknown quantity at this point (<10)
Quote:
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.
aaronrus is offline   Reply With Quote
Reply

Go Back   VBForums > Other Languages > C and C++


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:06 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.