excuse me as i am new to programming and trying to learn
i have searched through previous forum groups on this topic and i wish to seek the help of all tthe experts out there...
i wish to input a file into a class
i found this example
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class job
{
string name;
int length, time;
string type;
char urgent;
public:
bool read(string &line)
{
istringstream iss(line);
return iss >> name >> length >> time >> type >> urgent;
}
void show()
{
cout << "name = " << name << '\n';
cout << "length = " << length << '\n';
cout << "time = " << time << '\n';
cout << "type = " << type << '\n';
cout << "urgent = " << urgent << '\n';
cout << endl;
}
};
int main()
{
ifstream iFile("input1.txt");
if ( iFile )
{
string line;
while ( getline(iFile, line) )
{
job myjob;
if ( myjob.read(line) )
{
myjob.show();
}
}
}
return 0;
} but it only works if the format looks like this
BAK 90001 200 TXT N
CRSC 90010 150 TXT N
MARY 90011 140 FP N
CARL240 90050 300 FP N
LOW236 90052 150 FP N
where the space is the next variable
how would i manipulate this code if my stucture was on a per line basis as opposed to a space basis for the variable?
e.g.
bak
90001
200
txt
n
CRSC
90010
150
TXT
N
i need it to read the WHOLE line... as the name may or may not have a first and/or last name
thanks in adavance
understand what the code is doing. In main you make an input file and read it a line at a time. You then pass that line to myjob::read. This is where the parsing occurs. first an istringstream is constructed from our line of input then several items are extracted from it. This works the same as extracting items from cin but instead of a keyboard for input it reads from a string. Now have a go at solving your problem. How would you do it if you was reading from cin. you do the same with a stringstream.
understand what the code is doing. In main you make an input file and read it a line at a time. You then pass that line to myjob::read. This is where the parsing occurs. first an istringstream is constructed from our line of input then several items are extracted from it. This works the same as extracting items from cin but instead of a keyboard for input it reads from a string. Now have a go at solving your problem. How would you do it if you was reading from cin. you do the same with a stringstream.
like i said i am very ver new to this...
but that makes logical sense
if i was reading from cin...
i would go something like
iss.cin << name << length << time << type << urgent;
is this correct....?
would i be right in therefore thinking ...
iss.istringstream << name << length << time << type << urgent;
but how would i get each line to read to say about 5 lines per class?
btw thanks for the help
Minor tweaks:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class job
{
string name;
int length, time;
string type;
char urgent;
public:
bool read(ifstream &file)
{
return file >> name >> length >> time >> type >> urgent;
}
void show()
{
cout << "name = " << name << '\n';
cout << "length = " << length << '\n';
cout << "time = " << time << '\n';
cout << "type = " << type << '\n';
cout << "urgent = " << urgent << '\n';
cout << endl;
}
};
int main()
{
ifstream iFile("file.txt");
if ( iFile )
{
job myjob;
while ( myjob.read(iFile) )
{
myjob.show();
}
}
return 0;
}Minor tweaks:
#include <iostream> #include <string> #include <sstream> #include <fstream> using namespace std; class job { string name; int length, time; string type; char urgent; public: bool read(ifstream &file) { return file >> name >> length >> time >> type >> urgent; } void show() { cout << "name = " << name << '\n'; cout << "length = " << length << '\n'; cout << "time = " << time << '\n'; cout << "type = " << type << '\n'; cout << "urgent = " << urgent << '\n'; cout << endl; } }; int main() { ifstream iFile("file.txt"); if ( iFile ) { job myjob; while ( myjob.read(iFile) ) { myjob.show(); } } return 0; }
sorry one more question...
maybe i didnt make myself clear eariler...
if i have more then one record.... say about 10 records for the entire file
and want to be able to manipluate them individually... the code above only lets me edit the final class ....
e.g. aka myjob
if wanted to say declare 10 records whats the best way to do it
and how would that implement
e.g.
]#include
#include
#include
#include
using namespace std;
class job
{
string name;
int length, time;
string type;
char urgent;
public:
bool read(ifstream &file)
{
return file >> name >> length >> time >> type >> urgent;
}
void show()
{
cout << "name = " << name << '\n';
cout << "length = " << length << '\n';
cout << "time = " << time << '\n';
cout << "type = " << type << '\n';
cout << "urgent = " << urgent << '\n';
cout << endl;
}
};
int main()
{
ifstream iFile("file.txt");
if ( iFile )
{
job myjob, myjob1, myjob2, myjob3, myjob4; //..etc;
while ( myjob.read(iFile) )
{
}
while ( myjob1.read(iFile) ) //etc...
{
}
myjob.show();
myjob1.show();
myjob2.show();
myjob3.show(); //etc...
}
return 0;
like i said eariler what is a good way to code this?
if i have more then one record.... say about 10 records for the entire file
and want to be able to manipluate them individually... the code above only lets me edit the final class .... e.g. aka myjob if wanted to say declare 10 records whats the best way to do it and how would that implement
You're pretty much hinting that you want an array of 10. First, let me make this change (so I can be really lazy later on).
class job
{
string name;
int length, time;
string type;
char urgent;
public:
bool read(ifstream &file)
{
return file >> name >> length >> time >> type >> urgent;
}
friend ostream& operator<< (ostream &o, const job &j);
};
ostream& operator<< (ostream &o, const job &j)
{
o << "name = " << j.name << '\n';
o << "length = " << j.length << '\n';
o << "time = " << j.time << '\n';
o << "type = " << j.type << '\n';
o << "urgent = " << j.urgent << '\n';
o << endl;
return o;
}
Then after you've opened the file, you might have something like this.
job myjob[10];
for ( size_t i = 0; i < sizeof myjob / sizeof *myjob; ++i )
{
if ( !myjob[i].read(iFile) )
{
break;
}
cout << myjob[i];
}
But if you really don't want to hard-code a size of 10, I'd say use a vector.
Some additional headers are necessary for this example:
#include <vector>
#include <algorithm>
#include <iterator>
Now after the file is opened, code to read and display it might be as follows.
job ajob;
vector<job> myjob;
while ( ajob.read(iFile) )
{
myjob.push_back(ajob);
}
copy(myjob.begin(), myjob.end(), ostream_iterator<job>(cout, "\n"));ok... is it okay if i you help me expand my knowledge even more?
take a different example...
say i got a file of swimmers
e.g.
M
100 Free
50.24
Ian Thorpe
10/12/1960
10/02/2005
Wagga Wagga
1
M
100 Free
51.01
Grant Hackett
20/07/69
12/03/2005
Melbourne
2
M
100 Free
52.00
John Konrads
22/10/1970
14/04/2005
Adelaide
3
where the lines read
Sex M
Event eg 100 Free
Performance 50.24
Name Ian Thorpe
Date of birth 10/12/1960
Data of competition 10/2/2005
Venue Wagga Wagga
Position 1
lets make it interesting and say... we can put those files into a struct ...
e.g.
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
int main(){
fstream fs;
int const MAX = 11;
struct swim
{
char sex[2];
char eventtype[20];
char perf[20];
char name[20];
char dob[11];
char doc[11];
char venue[20];
char position[2];
};
swim record[MAX];
int count=0;
fs.open("c:\\swimmers.txt", ios::in);
if(fs.fail()) return 2;
while (!fs.eof() && count < MAX)
{
fs.getline(record[count].sex,2,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].eventtype,20,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].perf,20,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].name,21,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].doc,11,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].dob,11,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].venue,21,'\n');
for(int i=0; i<1;i++)
fs.getline(record[count].position,2,'\n');
for(int i=0; i<1;i++)
cout << record[count].sex << "\n";
cout << record[count].eventtype << "\n";
cout << record[count].perf << "\n";
cout << record[count].name << "\n";
cout << record[count].doc << "\n";
cout << record[count].dob << "\n";
cout << record[count].venue << "\n";
cout << record[count].position << "\n";
count++;
fs.close();
cout << record[6].name;
system("pause");
return 0;
}
<< moderator edit: added code tags : [code][/code] >>
and i wanna make classes BASED on the events e.g.
Men or women denoted by M or W.The events are 100m Free, 200m Back, 400m Medley, 800m Back, 1500m Free.
therefore in total 10 classes...
and inside those classes i got a struct of each swimmer with the apprioate event
i was thinking something along the lines of
struct athlete{
name
performance
dob
doc
venue
position
}
struct record{
sex
event
name
performance
dob
doc
venue
position
}
const int MAXSIZE = 10;
class rank
{
string sex, event;
athlete data[maxsize];
int total;
public
rank (string &s, string &e);
string getsex();
string getevent();
int gettotal();
althelete 7getdata(int i );
void sort();
void add();
void replace(int, athlete&);
void remove(int position);
friend ostream &operator<<(ostream&, const rank&):
void save(ostream&);
}; so a user is prompted with an option of selecting 1) to select the event of the class.... e.g. 100 free male
when selected given the option to edit it
therefore when an event is selected in option 1 the appropriate object should be selected. Option 2 will involve searching the list of swimmers names in the object
whats a good way of approching this??
i was thinking develop a class and functions similar to the above...
with the apprioprate structs....
am i on the right track? any thoughts?
Ah, if only I had more time to spend...
First thing I'd recommend is using std::string [note to Dani: here again is where I'd prefer a fixed font] instead of a char array; perhaps numeric fields where appropriate. Put them in your class or struct -- let's call it a record. Taking input for a record could a number of lines, similar to previous posts. And again use use::vector. Something like that. And there's [post=155265]this[/post].
ive been working on the above example.. heres what i have so far...
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
void displaymenu(void); //display the menu that will be presented to the user
void getnumber(int&); //get a choice from the user
void shownumber(int); //show the choice to user
void validatenumber(int&); //validate number0
void selectcase(int&, int&, int&); //select the number and choose a case
void selectclass();
int const MAX = 100;
struct swimrec
{
char sex[2];
char eventtype[20];
};
struct athrec
{
char perf[20];
char name[20];
char dob[11];
char doc[11];
char venue[20];
char position[2];
};
const int MAXSIZE = 10;
class swimevent
{
char sexclass[2];
char eventtypeclass[20];
athrec data[MAXSIZE];
int totalrec;
public:
};
int main(){
int number;
int i=0;
int currentrec =0;
int totalrec = 0;
do {
displaymenu();
getnumber(number);
validatenumber(number);
shownumber(number);
fstream fs;
swimrec recordswim[MAX];
athrec recordath[MAX];
int count=0;
fs.open("swimmers.txt", ios::in);
if(fs.fail()) return 2;
while (!fs.eof() && count < MAX)
{
fs.getline(recordswim[count].sex,2,'\n');
fs.getline(recordswim[count].eventtype,20,'\n');
fs.getline(recordath[count].perf,20,'\n');
fs.getline(recordath[count].name,21,'\n');
fs.getline(recordath[count].doc,11,'\n');
fs.getline(recordath[count].dob,11,'\n');
fs.getline(recordath[count].venue,21,'\n');
fs.getline(recordath[count].position,2,'\n');
count++;
} //end while
selectcase(number,currentrec, totalrec);
fs.close();
} while (number != 5);
system("pause");
return 0;
//cout << count;
//cout << record[6].name;
/*
for (int showallrec = 0; showallrec < count; showallrec++)
{
cout << recordswim[showallrec].sex << "\n";
cout << recordswim[showallrec].eventtype << "\n";
cout << recordath[showallrec].perf << "\n";
cout << recordath[showallrec].name << "\n";
cout << recordath[showallrec].doc << "\n";
cout << recordath[showallrec].dob << "\n";
cout << recordath[showallrec].venue << "\n";
cout << recordath[showallrec].position << "\n";
}
*/
system("pause");
return 0;
}
void selectcase(int& option,int& currentrec, int& totalrec)
{
// making the first record in file the default
switch (option)
{
case 1:
{
system("cls");
cout << "\n\n Select Current Event Selected\n\n";
selectclass();
// system("pause");
// system("cls");
break;
}
case 2:
{
// system("cls");
cout << "\n\n Update Existing Swimmer Selected\n\n";
cout << "\n\n -----------------------------\n\n ";
// system("pause");
// system("cls");
break;
}
case 3:
{
system("cls");
cout << "\n\n Add New Swimmer Selected\n\n";
cout << "\n\n -----------------------------\n\n ";
// system("pause");
// system("cls");
break;
}
case 4:
{
//system("cls");
cout << "\n\nProduce Report for current event Selected\n\n";
cout << "\n\n -----------------------------\n\n ";
// borrowvideo(i);
break;
}
}
}
void displaymenu(void)
{
cout << "\n1. Select Current Event" "\t"
<< "\n2. Update Existing Swimmer Selected" "\t"
<< "\n3.Add New Swimmer Selected" "\t"
<< "\n4. Produce Report for current event Selected"
<< "\n5. Exit and save";
return;
} //DisplayMenu
void validatenumber(int& option)
{
do
if(option!=1 && option!=2 &&
option!=3 && option!=4 &&
option!=5
)
{
cout << "Selection is not valid. "
<< "Must be 1, 2, 3, 4, 5 to exit " << endl;
getnumber(option);
} //if
while (option!=1 && option!=2 &&
option!=3 && option!=4 &&
option!=5 );
return;
}
void shownumber (int option)
{
cout << "\n\nYour choice was " << option << endl;
return;
}
void getnumber(int& option)
{
cout << "\nEnter menu option required ";
cout << "\n\n -----------------------------\n\n ";
cin >> option;
return;
} //getnumber
void selectclass()
{
cout << "\n1. Male 100 Free" "\t"
<< "\n2. Male 200 Back" "\t"
<< "\n3. Male 400 Medley" "\t"
<< "\n4. Male 800 Back"
<< "\n5. Male 1500 Free"
<< "\n6. FEMale 100 Free" "\t"
<< "\n7. FEMale 200 Back" "\t"
<< "\n8. FEMale 400 Medley" "\t"
<< "\n9. FEMale 800 Back"
<< "\n0. FEMale 1500 Free\n\n";
cout << "enter a choice of class" << endl;
int option=0;
cin >> option;
switch (option)
{
case 1:
{
system("cls");
cout << "\n\n Male 100 Free\n\n";
// system("pause");
// system("cls");
break;
}
case 2:
{
// system("cls");
cout << "\n\n Male 200 Back\n\n";
cout << "\n\n -----------------------------\n\n ";
// system("pause");
// system("cls");
break;
}
case 3:
{
system("cls");
cout << "\n\n Male 400 Medley\n\n";
cout << "\n\n -----------------------------\n\n ";
// system("pause");
// system("cls");
break;
}
case 4:
{
//system("cls");
cout << "\n\nMale 800 Back\n\n";
cout << "\n\n -----------------------------\n\n ";
break;
}
}
} now what i want to do is when a user types 1 to select event (run it if you wanna understand easily)
they are presented with the event choices e.g. 1 =Male 100 free (up to 10)
if they select that number... a new object is created for each event... for that class...
surely there must be an easy way to do this...
if you remember the file looks like this
M
100 Free
50.24
Ian Thorpe
10/12/1960
10/02/2005
Wagga Wagga
1
W
100 Free
59.55
Susie O'Neill
19/04/1971
24/03/2005
Brisbane
2
W
200 Back
2:20.02
Samantha Riley
10/05/1972
10/04/2005
Sydney
1
W
200 Back
2:22.30
Isla Konrads
11/09/1968
12/03/2005
Albury
2
M
400 Medley
4:29.63
Ian Thorpe
10/12/1960
10/02/2005
Wagga Wagga
2
<< moderator edit: added code tags : [code][/code] >>
ive been working on the above example.. heres what i have so far...
#include <iomanip> #include <fstream> #include <iostream> using namespace std; void displaymenu(void); //display the menu that will be presented to the user void getnumber(int&); //get a choice from the user void shownumber(int); //show the choice to user void validatenumber(int&); //validate number0 void selectcase(int&, int&, int&); //select the number and choose a case void selectclass(); int const MAX = 100; struct swimrec { char sex[2]; char eventtype[20]; }; struct athrec { char perf[20]; char name[20]; char dob[11]; char doc[11]; char venue[20]; char position[2]; }; const int MAXSIZE = 10; class swimevent { char sexclass[2]; char eventtypeclass[20]; athrec data[MAXSIZE]; int totalrec; public: }; int main(){ int number; int i=0; int currentrec =0; int totalrec = 0; do { displaymenu(); getnumber(number); validatenumber(number); shownumber(number); fstream fs; swimrec recordswim[MAX]; athrec recordath[MAX]; int count=0; fs.open("swimmers.txt", ios::in); if(fs.fail()) return 2; while (!fs.eof() && count < MAX) { fs.getline(recordswim[count].sex,2,'\n'); fs.getline(recordswim[count].eventtype,20,'\n'); fs.getline(recordath[count].perf,20,'\n'); fs.getline(recordath[count].name,21,'\n'); fs.getline(recordath[count].doc,11,'\n'); fs.getline(recordath[count].dob,11,'\n'); fs.getline(recordath[count].venue,21,'\n'); fs.getline(recordath[count].position,2,'\n'); count++; } //end while selectcase(number,currentrec, totalrec); fs.close(); } while (number != 5); system("pause"); return 0; //cout << count; //cout << record[6].name; /* for (int showallrec = 0; showallrec < count; showallrec++) { cout << recordswim[showallrec].sex << "\n"; cout << recordswim[showallrec].eventtype << "\n"; cout << recordath[showallrec].perf << "\n"; cout << recordath[showallrec].name << "\n"; cout << recordath[showallrec].doc << "\n"; cout << recordath[showallrec].dob << "\n"; cout << recordath[showallrec].venue << "\n"; cout << recordath[showallrec].position << "\n"; } */ system("pause"); return 0; } void selectcase(int& option,int& currentrec, int& totalrec) { // making the first record in file the default switch (option) { case 1: { system("cls"); cout << "\n\n Select Current Event Selected\n\n"; selectclass(); // system("pause"); // system("cls"); break; } case 2: { // system("cls"); cout << "\n\n Update Existing Swimmer Selected\n\n"; cout << "\n\n -----------------------------\n\n "; // system("pause"); // system("cls"); break; } case 3: { system("cls"); cout << "\n\n Add New Swimmer Selected\n\n"; cout << "\n\n -----------------------------\n\n "; // system("pause"); // system("cls"); break; } case 4: { //system("cls"); cout << "\n\nProduce Report for current event Selected\n\n"; cout << "\n\n -----------------------------\n\n "; // borrowvideo(i); break; } } } void displaymenu(void) { cout << "\n1. Select Current Event" "\t" << "\n2. Update Existing Swimmer Selected" "\t" << "\n3.Add New Swimmer Selected" "\t" << "\n4. Produce Report for current event Selected" << "\n5. Exit and save"; return; } //DisplayMenu void validatenumber(int& option) { do if(option!=1 && option!=2 && option!=3 && option!=4 && option!=5 ) { cout << "Selection is not valid. " << "Must be 1, 2, 3, 4, 5 to exit " << endl; getnumber(option); } //if while (option!=1 && option!=2 && option!=3 && option!=4 && option!=5 ); return; } void shownumber (int option) { cout << "\n\nYour choice was " << option << endl; return; } void getnumber(int& option) { cout << "\nEnter menu option required "; cout << "\n\n -----------------------------\n\n "; cin >> option; return; } //getnumber void selectclass() { cout << "\n1. Male 100 Free" "\t" << "\n2. Male 200 Back" "\t" << "\n3. Male 400 Medley" "\t" << "\n4. Male 800 Back" << "\n5. Male 1500 Free" << "\n6. FEMale 100 Free" "\t" << "\n7. FEMale 200 Back" "\t" << "\n8. FEMale 400 Medley" "\t" << "\n9. FEMale 800 Back" << "\n0. FEMale 1500 Free\n\n"; cout << "enter a choice of class" << endl; int option=0; cin >> option; switch (option) { case 1: { system("cls"); cout << "\n\n Male 100 Free\n\n"; // system("pause"); // system("cls"); break; } case 2: { // system("cls"); cout << "\n\n Male 200 Back\n\n"; cout << "\n\n -----------------------------\n\n "; // system("pause"); // system("cls"); break; } case 3: { system("cls"); cout << "\n\n Male 400 Medley\n\n"; cout << "\n\n -----------------------------\n\n "; // system("pause"); // system("cls"); break; } case 4: { //system("cls"); cout << "\n\nMale 800 Back\n\n"; cout << "\n\n -----------------------------\n\n "; break; } } }now what i want to do is when a user types 1 to select event (run it if you wanna understand easily) they are presented with the event choices e.g. 1 =Male 100 free (up to 10) if they select that number... a new object is created for each event... for that class... surely there must be an easy way to do this... if you remember the file looks like this
<< moderator edit: added code tags : [code][/code] >>M 100 Free 50.24 Ian Thorpe 10/12/1960 10/02/2005 Wagga Wagga 1 W 100 Free 59.55 Susie O'Neill 19/04/1971 24/03/2005 Brisbane 2 W 200 Back 2:20.02 Samantha Riley 10/05/1972 10/04/2005 Sydney 1 W 200 Back 2:22.30 Isla Konrads 11/09/1968 12/03/2005 Albury 2 M 400 Medley 4:29.63 Ian Thorpe 10/12/1960 10/02/2005 Wagga Wagga 2
And how would you put this data in objects, i mean when u create objects. how that data for that perticular event is going to flow in that object.