Posting the full code or at least giving us a description of exactly what you are trying to do would certainly help.
It looks like you want associate the user's inputted file name with the input object.
If file is an std::string, then you will want to change input.open("C:\\myfile.txt",ios::in); to input.open(file.c_str(), ios::in);.
It also looks like you want to pull a set of values from the input stream (id, name, age, gender, country), then output those values, then repeat for the next set of those values. It is difficult to correct your code though, as we cannot see the whole picture.
const int TOTAL_REG = 20; //equal to the number of registrations
using namespace std;
struct Athlete {
int aID;
string aName;
int aAge;
char aGender;
string aCountry;
};
struct Event {
int eID;
string eName;
string eRecord;
};
struct Registration {
int rID;
Athlete rAthlete;
Event rEvent;
};
//function prototypes
void discard_line(ifstream &input);
void ReadRegistration(Registration *regPtr, int &size);
int main()
{
Registration regs[TOTAL_REG];
Registration regPtr[TOTAL_REG];
int size;
int total_regs;
char file[100];
int menu;
ifstream input;
char c;
cout << "Welcome to Athlete Registration and Result Program" << endl;
cout << "\nMenu: \n1 - Read and Display registration file \n";
cout << "2 - List all athletes by event \n";
cout << "3 - List all athletes by country \n";
cout << "4 - Read and display result file \n";
cout << "5 - List medals counts by country \n";
cout << "6 - Display medal tally \n";
cout << "7 - Exit \n\n";
cout << "Please Enter menu option: ";
cin >> menu;
system("PAUSE");
return 0;
}
//discard_line function can be used to discard any unneeded line from an input file
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
//Definition of function to read registration.
void ReadRegistration(Registration *regPtr, int &size)
{
for (int i = 0; i < size; i++)
{
cout << (*regPtr).rID << " " << (*regPtr).rAthlete.aID << " " << (*regPtr).rAthlete.aName << " " << (*regPtr).rAthlete.aAge << " " << (*regPtr).rAthlete.aGender << " " << (*regPtr).rAthlete.aCountry << " " << (*regPtr).rEvent.eID << " " << (*regPtr).rEvent.eName << " " << (*regPtr).rEvent.eRecord;
}
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
constint TOTAL_REG = 20; //equal to the number of registrations
usingnamespace std;
struct Athlete {
int aID;
string aName;
int aAge;
char aGender;
string aCountry;
};
struct Event {
int eID;
string eName;
string eRecord;
};
struct Registration {
int rID;
Athlete rAthlete;
Event rEvent;
};
//function prototypes
void discard_line(ifstream &input);
void ReadRegistration(Registration *regPtr, int &size);
int main()
{
Registration regs[TOTAL_REG]; /** Unused? **/
Registration regPtr[TOTAL_REG];
int size;
int total_regs;
char file[100];
int menu;
ifstream input;
char c; /** Unused **/
cout << "Welcome to Athlete Registration and Result Program" << endl;
cout << "\nMenu: \n1 - Read and Display registration file \n";
cout << "2 - List all athletes by event \n";
cout << "3 - List all athletes by country \n";
cout << "4 - Read and display result file \n";
cout << "5 - List medals counts by country \n";
cout << "6 - Display medal tally \n";
cout << "7 - Exit \n\n";
cout << "Please Enter menu option: ";
cin >> menu;
if (menu == 1)
{
cout << "Enter Registration filename: " << endl;
cin >> file;
input.open(file);
if (input.fail())
{
cerr << "Filename error" << endl;
}
discard_line(input);
total_regs = 0;
int i;
for (i = 0; !input.eof(); i++)
{
/** regPtr is an array, so to access individual Registration objects,
you can use regPtr[index_number].
**/
input >> regPtr[i].rID >>
regPtr[i].rAthlete.aID >>
regPtr[i].rAthlete.aName >>
regPtr[i].rAthlete.aAge >>
regPtr[i].rAthlete.aGender >>
regPtr[i].rAthlete.aCountry >>
regPtr[i].rEvent.eID >>
regPtr[i].rEvent.eName >>
regPtr[i].rEvent.eRecord;
}
size = i - 1;
ReadRegistration(regPtr, size);
total_regs++;
input.get(c);
input.close(); /** Forgot (). **/
}
// system("PAUSE"); /** Should be avoided. On my system this didn't work **/
cin.get(); /** This should work just to see the output. **/
return 0;
}
//discard_line function can be used to discard any unneeded line from an input file
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
//Definition of function to read registration.
void ReadRegistration(Registration *regPtr, int &size)
{
for (int i = 0; i < size; i++)
{
/** I added a newline at the end to more easily view the output. **/
cout << regPtr[i].rID << " " <<
regPtr[i].rAthlete.aID << " " <<
regPtr[i].rAthlete.aName << " " <<
regPtr[i].rAthlete.aAge << " " <<
regPtr[i].rAthlete.aGender << " " <<
regPtr[i].rAthlete.aCountry << " " <<
regPtr[i].rEvent.eID << " " <<
regPtr[i].rEvent.eName << " " <<
regPtr[i].rEvent.eRecord << '\n';
}
}
int i;
for (i = 0; !input.eof(); i++)
{
/** regPtr is also being used as an array, so to access individual Registration objects,
you can use regPtr[index_number].
**/