Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,940 people online right now. Registration is fast and FREE... Join Now!




Program needs finishing arithmetic help

 
Reply to this topicStart new topic

Program needs finishing arithmetic help, No matter what values i put in the end will always be one

wowhesawesome
6 Dec, 2007 - 11:01 AM
Post #1

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 14


My Contributions
the idea of the program is to take in an amount of baseball players with their at bats and hits values for 1 game. In the end it needs to total up the values and come up with a team batting average. Now when indivually calculating the average values for each player, there is no error, but when the team average needs to be calculated the answer always comes to 1.000. Where did i go wrong?


CODE

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
   string name,team,posi;
   int j;
   double avg,atbats,hits,teamavg,teamatbats,teamhits;
   const int LOOP_LIMIT=3;

   cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(3);


    cout<<"Enter the team name"<<endl;
    getline(cin, team);


    for(j=1;j<=LOOP_LIMIT; ++j)
    {
        cout<<"Enter the player name"<<endl;
        getline(cin, name);
        cout<<"Enter the position played"<<endl;
        getline(cin, posi);
        cout<<"Enter the player's at bats:"<<endl;
        cin>>atbats;
        cout<<"Enter the player's hits:"<<endl;
        cin>>hits;
        cout<<endl;
        cout<<name<<endl;
        avg=(hits/atbats);
        cout<<avg<<endl;
        cout<<endl;
        cout<<"Player # "<<j<<endl;
        cin.ignore();
    }
    cout<<team<<endl;
    cout<<endl;
    teamatbats=(teamatbats+atbats);
    teamhits=(teamhits+hits);
    teamavg=(teamhits/teamatbats);
    cout<<"The team's batting average is: "<<teamavg<<endl;
    return 0;
}

*edit: In the future use code tags. Please and thank you. code.gif

This post has been edited by Martyr2: 6 Dec, 2007 - 12:32 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Program Needs Finishing Arithmetic Help
6 Dec, 2007 - 12:44 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi,

You just needed to include the summary statements IN the for loop. That and you should initialize your variables because not all compilers will initialize them to zero or what not. Also you might want to think about checking for divide by zero errors if they ever arise. But the main problem was not including the lines where you summarize in the for loop.

CODE

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
   string name,team,posi;
   int j;

   // Always make sure you initialize your variables.
   // In VC++ this will be flagged as an error for not initializing before using.

   double avg = 0.0,atbats = 0.0,hits = 0.0,teamavg = 0.0,teamatbats = 0.0,teamhits = 0.0;
   const int LOOP_LIMIT=3;

   cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(3);


    cout<<"Enter the team name"<<endl;
    getline(cin, team);


    for(j=1;j<=LOOP_LIMIT; ++j)
    {
        cout<<"Enter the player name"<<endl;
        getline(cin, name);
        cout<<"Enter the position played"<<endl;
        getline(cin, posi);
        cout<<"Enter the player's at bats:"<<endl;
        cin>>atbats;
        cout<<"Enter the player's hits:"<<endl;
        cin>>hits;
        cout<<endl;
        cout<<name<<endl;
        avg=(hits/atbats);
        cout<<avg<<endl;
        cout<<endl;
        cout<<"Player # "<<j<<endl;
        cin.ignore();
    
        // These have to be inside the for loop to accumulate.
        teamatbats=(teamatbats+atbats);
        teamhits=(teamhits+hits);
    }

    teamavg=(teamhits/teamatbats);

    cout<<team<<endl;
    cout<<endl;
    cout<<"The team's batting average is: "<<teamavg<<endl;
    return 0;
}


Enjoy!

"At DIC we be baseball averaging code ninjas!" decap.gif

This post has been edited by Martyr2: 6 Dec, 2007 - 12:45 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month