1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int ARRAY_SIZE = 2 ; // declare my constant for array size to make it easier to change amount of players being entered
// struct to hold MLB player stats
struct playerStats
{
string playerFirstName[ARRAY_SIZE] ; // array containing player's first names
string playerLastName[ARRAY_SIZE] ; // array containing player's last names
int home_runs[ARRAY_SIZE] ; // array containing number of home runs
int hits[ARRAY_SIZE] ; // array containing number of hits
}tempStats; // variable of struct
// function prototypes
void intro() ;
void readInOption(int selection, ifstream& fin, ofstream& fout) ;
playerStats playerInfo() ;
void dataOutput(playerStats myPlayers) ;
void openFiles(ifstream& fin, ofstream& fout) ;
void closeFiles(ifstream& fin, ofstream& fout) ;
void dataOutputToFile(playerStats myPlayers, ofstream& fout) ;
int main()
{
ifstream fin ;
ofstream fout ;
int selection = 0 ;
intro() ; // calls the introduction function
readInOption(selection, fin, fout) ; // menu option for data input
//dataOutput(playerStats myPlayers) ;
system ("PAUSE") ;
return 0 ;
}
//functions
// introduction function
void intro()
{
cout << " This program allows the user to enter in the stats of 10 MLB players." << endl << endl ;
} // end intro function
// menu function that allows user to pick whether they want to input the data manually or input it by file
void readInOption(int selection, ifstream& fin, ofstream& fout)
{
cout << " How would you like to enter your data?" << endl << endl
<< " 1. Manually" << endl
<< " 2. By file" << endl << endl
<< " Enter selection: " ;
cin >> selection ;
if (selection == 1)
{
playerInfo() ;
}
else if (selection == 2)
{
openFiles(fin, fout) ;
}
else
{
cout << endl << " Enter a valid selection." << endl << endl ;
readInOption(selection, fin, fout) ;
}
}// end readInOption function
// this function enters data into the playerStats struct
playerStats playerInfo()
{
playerStats tempStats ;
// loops to allow the entry of data in relation to the array size
for(int i=0 ; i < ARRAY_SIZE ; i++)
{
cout << endl
<< "Enter the player's first name: ";
cin >> tempStats.playerFirstName[i] ;
cout << "Enter the player's last name: ";
cin >> tempStats.playerLastName[i] ;
cout << "Enter the player's number of homeruns: ";
cin >> tempStats.home_runs[i] ;
cout << "Enter the player's number of hits: ";
cin >> tempStats.hits[i] ;
cout << endl ;
}
return tempStats ;
}// end playerStats function
// outputs data to the terminal
void dataOutput(playerStats myPlayers)
{
// outputs complete data from the struct
for(int i=0 ; i < ARRAY_SIZE ; i++)
{
cout << myPlayers.playerFirstName[i] << " "
<< myPlayers.playerLastName[i]
<< " has "<< myPlayers.home_runs[i]
<< " homeruns and "
<< myPlayers.hits[i] << " hits" << endl ;
}
}// end dataOutput function
// outputs data to a file
void dataOutputToFile(playerStats myPlayers, ofstream& fout)
{
// this section is to be used when a menu is set up to ask user if they want to save to a file
/*string filename ;
cout << "Enter the location of your output file: " ;
cin >> filename ;
fout.open(filename.c_str(), ios::app) ; // Open output file and allow it to append */
for(int i=0 ; i < ARRAY_SIZE ; i++)
{
fout << myPlayers.playerFirstName[i] << " "
<< myPlayers.playerLastName[i]
<< " has "<< myPlayers.home_runs[i]
<< " homeruns and "
<< myPlayers.hits[i] << " hits" << endl ;
}
}
void openFiles(ifstream& fin, ofstream& fout)
{
string filename ;
cout << "Enter the location of your input file: " ;
cin >> filename ;
fin.open(filename.c_str()) ; // Open input file
while( !fin.eof() )
{
}
cout << "Enter the location of your output file: " ;
cin >> filename;
fout.open(filename.c_str(), ios::app) ; // Open output file and allow it to append
} // end openFiles function
void closeFiles(ifstream& fin, ofstream& fout)
{
fin.close() ; // close input file
fout.close() ; // close output file
} // end closeFiles function
|