Appending Vectors?

New to C++, have a project where I have to read an input file with baseball statistics on it (done), but then place those values into an array. Figured vectors would be better, but really new to them, not really sure how they work.

My code is posted below. Not only do I get an error when I use "string names" in the function arguments, but I get an error when I try to use "names.push_back(firstName)".

I basically want to make it so that every time this loop iterates (goes through one line in the text file), it stores all of the different variables in different arrays/vectors.

Here's the code. I'm COMPLETELY lost! Also, what are some good ways to retain changes in the values of variables in a while loop? If you see, I had to use increments and decrements to do so.

// main.cpp
// Baseball Statistics
//
//
//
// Step 1: Vector/Dynamic Array Length. Use to set the number of elements to numbers of players (iterations).
// Step 2: Store information in different arrays.
// Step 3: Sort (Insertion) by last name, or stats?
// Step 4:
//
//
//---------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <vector>

using namespace std;

//---------------------------------------------------------------------------------------------------------------------

void openFile (string fileName, string saveFile); //Opens and Displays Statistics File

//---------------------------------------------------------------------------------------------------------------------

int main()

{

vector<string> names (1);
string fileName;
string saveFile;
openFile(fileName, saveFile, names);

} // end main()

//---------------------------------------------------------------------------------------------------------------------

void openFile (string fileName, string saveFile, string names)

{

cout << "What Is The Name Of The Next File You Would Like To Open? File Names Are Case-Sensitive. Enter 'Exit' To Exit At Any Time." << endl;
cin >> fileName;
if (fileName == "Exit")
exit(0);
cout << "What Is The Name Of The Next File You'd Like To Save To? Do NOT Use The Same Location As The File You Want To Open. Enter 'Exit' To Exit At Any Time." << endl;
cin >> saveFile;
if (saveFile == "Exit")
exit(0);

while (saveFile == fileName)
{
cout << "Error. Please Try Again. Be Sure To Not Create A File With The Same Name As An Existing File." << endl;
cout << "What Is The Name Of The Next File You Would Like To Open? File Names Are Case-Sensitive. Enter 'Exit' To Exit At Any Time." << endl;
cin >> fileName;
if (fileName == "Exit")
exit(0);
cout << "What Is The Name Of The Next File You'd Like To Save To? Do NOT Use The Same Location As The File You Want To Open. Enter 'Exit' To Exit At Any Time." << endl;
cin >> saveFile;
if (saveFile == "Exit")
exit(0);
}
cout << "\tOpening File . . . \n";


//---------------------------------------------------------------------------------------------------------------------

while (true)
{

ifstream inFile(fileName); //Opens File That User Inputted
ofstream outFile(saveFile); //Writes Name, AB, OBP To New File

if (inFile) //Checks If File Exists
{

string firstName;
string lastName;
int atBats, hits, walks, hitByPitch, sacrificeFly;
float totalOBP = 0;
float numPlayers = 0;
float averageOBP = 0;
float maxOBP = 0;
float minOBP = 1;
float incCount = 0;
float incCount2 = 0;


//---------------------------------------------------------------------------------------------------------------------

while(inFile >> firstName >> lastName >> atBats >> hits >> walks >> hitByPitch >> sacrificeFly)
{

cout << firstName << ' ' << lastName << ' ' << atBats << ' ' << hits << ' ' << walks << ' ' << hitByPitch << ' ' << sacrificeFly;
float obp = (float)(hits + walks + hitByPitch) / (float)(atBats + walks + hitByPitch + sacrificeFly);
cout << ' ' << fixed << setprecision (3) << obp << endl;

//---------------------------------------------------------------------------------------------------------------------

outFile << firstName << ' ' << lastName << ' ' << atBats << ' ' << fixed << setprecision (3) << obp << ' ' << endl;

totalOBP += obp;
++totalOBP;
++numPlayers;

names.resize(numPlayers);
names.push_back(firstName);

if (maxOBP < (obp + incCount))
{
maxOBP = obp + incCount;
++maxOBP;
++incCount; //Must increment to retain value when while loop ends. See below.

}

if (minOBP > (obp + incCount2))
{
minOBP = obp + incCount2;
++minOBP;
++incCount2;
}



} // end while

totalOBP = totalOBP - numPlayers;
averageOBP = totalOBP / numPlayers;
maxOBP = maxOBP - incCount;
minOBP = minOBP - incCount2;

outFile << "The Average OBP Is: " << averageOBP << " . The Max OBP Is: " << maxOBP << " . The Min OBP Is: " << minOBP << ". " << endl;

inFile.close();
outFile.close();


} // end if (inFile)

//---------------------------------------------------------------------------------------------------------------------

else
cout << "Error. Please Enter A Valid File Name. Make Sure To Use Extensions." << endl;
cout << "What Is The Name Of The File You Would Like To Open? File Names Are Case-Sensitive. Enter 'Exit' To Exit At Any Time." << endl;
cin >> fileName;
if (fileName == "Exit")
exit(0);
cout << "What Is The Name Of The Next File You'd Like To Save To? Do NOT Use The Same Location As The File You Want To Open. Enter 'Exit' To Exit At Any Time." << endl;
cin >> saveFile;
if (saveFile == "Exit")
exit(0);
while (saveFile == fileName)
{
cout << "Error. Please Try Again. Be Sure To Not Create A File With The Same Name As An Existing File." << endl;
cout << "What Is The Name Of The Next File You Would Like To Open? File Names Are Case-Sensitive. Enter 'Exit' To Exit At Any Time." << endl;
cin >> fileName;
if (fileName == "Exit")
exit(0);
cout << "What Is The Name Of The Next File You'd Like To Save To? Do NOT Use The Same Location As The File You Want To Open. Enter 'Exit' To Exit At Any Time." << endl;
cin >> saveFile;
if (saveFile == "Exit")
exit(0);
}

cout << "\tOpening File . . . \n";




//---------------------------------------------------------------------------------------------------------------------

} // openFile ()



}
Topic archived. No new replies allowed.