So I've been making alot of progress in a program where I have two files.
(1) a file containing data for baseball players
(2) a file where the data outputs when ran
I included a menu in the program, but I am trying to figure out how I would change the info in the file.
Lets say I wanted to change the stats on a player how does one change those? I know I need to make another function, beyond that no idea. I figured out how to include functions as well as using the ifstream / ofstream. So progress is definitely being made.
For easier debugging purposes I separate my code into 3 files. The header file, and two source files.
**MAINPROGRAM.CPP**
#include "myFunctions.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char response;
ifstream in; // The input stream
ofstream out; // The output stream
in.open("baseball.txt"); //This will input to the file
out.open("baseballout.txt"); // This will output to the file
cout << "Baseball player data:" << endl << endl;
cout << "Please choose from one of the following menu options: " << endl << endl;
cout << "A - Print player info: " << endl;
cout << "B - Search player name: " << endl << endl;
cin >> response;
if (response == 'A', 'a')
{
cout << outputBaseballData;
}
else if (response != 'A' )
cout << "Thank you for using our system" << endl;
baseball playerInfo[10];
for (int i = 0 ; i < 10 ; i++)
{
inputBaseballData(playerInfo[i], in);
outputBaseballData(playerInfo[i], out);
}
system("pause");
in.close();
out.close();
return 0;
}
**MYFUNCTIONS.CPP**
#include "myFunctions.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void inputBaseballData (baseball& stats, ifstream& input)
{
input >> stats.Name;
input >> stats.HomeRuns;
input >> stats.Hits;
}
void outputBaseballData(baseball& stats, ofstream& output)
{
output << "Player name: "<< stats.Name << endl;
output << "\n# of Home runs: " << stats.HomeRuns << endl;
output << "\n# of Hits: " << stats.Hits << endl;
}
**MyFUNCTIONS.H (Header file) **
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
struct baseball
{
string Name;
int HomeRuns;
int Hits;
};
void inputBaseballData (baseball& stats, ifstream& input);
void outputBaseballData (baseball& stats, ofstream& output);
This post has been edited by xkaijinx: 19 February 2012 - 11:25 PM

New Topic/Question
Reply




MultiQuote



|