I'm trying to find the min/max of the number of years played and the min/max batting average of a list of players in an input file inputted in by the user that looks like this, the file is read and checked and then the program goes and gets the name, team, average, bats, hits.

Name Team Years at bats hits
Bob Astros 2 145 40
Sam Mets 1 100 35
Earl Giants 8 560 150
Joe Yankees 6 430 100
Mike Padres 3 265 95
John Twins 5 333 90
Frank Cardinals 7 450 80
Carl Athletics 4 300 105

Dev c++ is currently stuck on the very first if stmt and i'm not sure why.
Is there anything else you see wrong!!!! Please help not compiling!!!
It would be really appreciated!!!

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
 
ifstream infile;
 
int bats, hits, years;
string userfilename, name, team, minName, minimumYears, minTeam, minBats, 
minHits, maximumYears, //declaring each item needed
maxName, maxTeam, maxBats, maxHits, maximumAvg, minimumAvg;
double average;
 
average = (hits/bats); //computing the average
 
minimumYears = 100000;
maximumYears = -1;
maximumAvg = -1; //initializing the Avg/Years
minimumAvg = 2;
 
cout <<"Enter the name of the input file: \n"; //User inputs file
cin >> userfilename;
 
infile.open(userfilename.c_str());
if(infile.fail())
{
cout << "Error opening file\n"; //check to see if file
exit(1); //opens correctly
 
}
 
cout <<"-------------------------------------------------------\n";
cout << "Name" << "Team" << "Average" << "At Bats" << "Hits" << endl;
cout <<"-------------------------------------------------------\n";
 
infile >> name >> team >> years >> bats >> hits;//file gets items
 
cout << name << team << average << bats << hits << endl;
 
infile >> name >> team >> years >> bats >> hits;
 
while(!infile.eof()) //until end of file do the following
{
if (years < minimumYears ) //find the minimumYears played
{
minimumYears = years;
minName = name;
minTeam = team;
minBats = bats;
minHits = hits;
}
 
if ( years > maximumYears ) //find maximumYears played
{
maximumYears = years;
maxName = name;
maxTeam = team;
maxBats = bats;
maxHits = hits;
}
//Going get another line of players info
infile >> name >> team >> years >> bats >> hits; 
 
if ( average > maximumAvg)
{
maximumAvg = average;
maxName = name;
maxTeam = team;
maxBats = bats;
maxHits = hits;
 
}
if (average < minimumAvg)
{
minimumAvg = average;
minName = name;
minTeam = team;
minBats = Bats;
minHits = hits;
 
infile >> name >> team >> years >> bats >> hits;
}
 
cout << "Highest and Lowest Averages" << endl;
//print out info for the player with the maximumAvg
cout << maxName << maxTeam << maximumAvg << maxBats << maxHits << endl;
cout << minName << minTeam << minimumAvg << minBats << minHits << endl;
 
cout << "Most and Least Number of Years Played" << endl;
//Print out infor for max/min years played
cout << maxName << maxTeam << maximumYears << maxAvg << maxBats << maxHits << endl;
cout << minName << minTeam << minimumYears << minAvg << minBats << minHits << endl;
 
 
 
system("pause");
return 0;
}

Some points....

You should declare the following variables as ints instead of strings, so that you can compare values later on: minimumYears, maximumYears, maximumAvg, minHits, maxBats, maxHits , minimumAvg, minBats This should fix the majority of the errors. A question for you: why in the world are you mixing prefixes in your variables? You use "maximum" sometimes, and other times just "max". It's OK to do the longer version if you want, but be consistent! There's a couple of errors that occur because you got mixed up.

Lastly, please use code tags, they make your code much easier to read.

I've always found that it's best to start small, get it compiling then add bits here and bits there, and always make sure it keeps compiling...that helps with figuring out where an error is...if it compiled before you added your new code and it doesn't compile anymore, you know there's something wrong with your new code...
Have you tried doing it this way? It might be a huge help in the future...you might start with this one by commenting out code that you don't need, then try compiling, successively uncommenting code and recompiling until it won't compile, and you will have some code that has problems...process of elimination...hope that helps...

I've always found that it's best to start small, get it compiling then add bits here and bits there, and always make sure it keeps compiling...that helps with figuring out where an error is...if it compiled before you added your new code and it doesn't compile anymore, you know there's something wrong with your new code...
Have you tried doing it this way? It might be a huge help in the future...you might start with this one by commenting out code that you don't need, then try compiling, successively uncommenting code and recompiling until it won't compile, and you will have some code that has problems...process of elimination...hope that helps...

Definitely. Some general rules that I keep in mind when programming something:

  • Make sure that anything longer than a few pages is split into several functions (as long as this is possible, sometimes it isn't).
  • Never write more than one function at a time.
  • Test each function before continuing.

And it's probably a good idea to write even less than a function and test it when you're starting. It's never a bad idea to write a program that's too small. As you get better at coding, you can write longer sections of code that aren't bug-ridden, but even so, you should periodically test out the code sections you've written.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.