I tried earlier to use a for loop to put data in but became too problematic. So I tried to use a while loop, it works but when I tried to debug it it continued to put -858993460 into every slot. The .dat file is in the right spot and opens.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct record
{
int item_id;
string item_type;
float item_price;
int num_stock;
string item_title;
string item_author;
int year_published;
};
void read_all_records(record records[], int &valid_entries);
int num_inventory_of_type(record records[], string type, int &valid_entries);
const int max_array = 100;
int main()
{
int valid_entries = 0;
record records[max_array];
read_all_records(records, valid_entries);
cout << "Stock Report" << endl;
cout << "------------" << endl;
int book = num_inventory_of_type(records, "book", valid_entries);
cout << "Book's In Stock: " << book << endl;
int cd = num_inventory_of_type(records, "cd", valid_entries);
cout << "CD's In Stock: " << cd << endl;
int dvd = num_inventory_of_type(records, "dvd", valid_entries);
cout << "DVD's In Stock: " << dvd << endl;
return 0;
}
void read_all_records(record records[], int &valid_entries)
{
ifstream invfile;
invfile.open("inventory.dat");
if (!invfile.is_open())
{
cout<<"file open failed";
exit(1);
}
while(invfile.good() && valid_entries < max_array)
{
invfile >> records[valid_entries].item_id >> records[valid_entries].item_type
>> records[valid_entries].item_price >> records[valid_entries].num_stock
>> records[valid_entries].item_title >> records[valid_entries].item_author
>> records[valid_entries].year_published;
if(!invfile.good())
break;
valid_entries++;
}
invfile.close();
}
int num_inventory_of_type(record records[], string type, int &valid_entries)
{
int count = 0;
int holder = 0;
for (int count = 0; count<valid_entries; count++);
{
if (records[count].item_type == type)
{
holder+=records[count].num_stock;
}
}
return holder;
}
the .dat file is
123456
book
69.99
16
Problem_Solving_With_C++
Walter_Savitch
2011
123457
cd
9.99
32
Sigh_No_More
Mumford_and_Sons
2010
123458
dvd
17.99
15
Red_State
Kevin_Smith
2011
123459
cd
9.99
16
The_Church_Of_Rock_And_Roll
Foxy_Shazam
2012
123460
dvd
59.99
10
The_Walking_Dead_Season_1
Robert_Kirkman
2011
all are on new lines, no spaces.
Basically it should start, run the read_all_records function and put the .dat data into the array. However, I put the cout << records[count].item_id; in the while loop just to see if the data was actually going in, and I get -858993460 each time. After that it should run the next function 3 times and return how many of each books there are.
You used the integer type int on item_price. invfile >> records[count].item_price will then only extract 69 instead of 69.99, thus resulting in a error when you try to extract the year_published.
Use a float or double instead.
struct record
{
int item_id;
string item_type;
float item_price;
int num_stock;
string item_title;
string item_author;
int year_published;
};
/* skipped identical lines */
while(invfile.good() && count < max_array)
{
invfile >> records[count].item_id >> records[count].item_type
>> records[count].item_price >> records[count].num_stock
>> records[count].item_title >> records[count].item_author
>> records[count].year_published;
cout << records[count].item_price << endl;
if(!invfile.good())
break;
cout << records[count].item_id << endl;
count++;
}
invfile.close();
Note that you have an extra semicolon in for (int count = 0; count<max_array; count++);. I guess you didn't intend this, so remove it.
This isn't a direct answer to the problem, but perhaps it will go away after a refactoring:
std::istream& operator>>(std::istream& is, record& r) {
return is >> r.item_id >> r.item_type >> … >> r.year_published;
}
int main () {
if (std::ifstream invfile("inventory.dat")) {
std::vector<record> records((std::istream_iterator<record>(invfile)),
std::istream_iterator<record>());
num_inventory_of_type(records, "dvd");
num_inventory_of_type(records, "cd");
num_inventory_of_type(records, "book");
}
}
If you still want to print out each record as you read it, the code can be rearranged as follows:
std::vector<record> records;
for (std::istream_iterator<record> i(invfile);
i != std::istream_iterator<record>(); ++i)
{
records.push_back(*i);
std::cout << i->item_id << "\n";
}
You need to change int item_price; to a float so -> float item_price;
and as mentioned above you need to swap the count++; and cout << records[count].item_id line.
After these two changes it will work properly.
struct record
{
int item_id;
string item_type;
float item_price; // <--- Needs to be a float
int num_stock;
string item_title;
string item_author;
int year_published;
};
// This order is required because you are storing in the current 'count' record and then you need to print it. Then increment the count to store the next record
cout << records[count].item_id;
count++;
Related
Bulbasaur|Grass| |2|16|45|65|65|45|0.059
Ivysaur|Grass|Poison|3|32|60|80|80|60|0.059
Venusaur|Grass|Poison| | |80|100|100|80|0.059
Torchic|Fire| |23|16|45|70|50|45|0.045
Combusken|Fire|Fighting|24|36|60|85|60|55|0.045
Blaziken|Fire|Fighting| | |80|110|70|80|0.045
These are some data in my text file, which stores Pokemon's name, type 1, type 2, index number of evolved Pokemon from the list, and stats, with character "|" as dividing lines, how do I read all hundreds of similar data into a 2D array? Or any other form of array which gives better result?
These are my C++ codes, and the result are totally failure.
ifstream inFile;
inFile.open("PokemonBaseStats.txt");
if (inFile.fail())
{
cerr << "Could not find file" << endl;
}
vector<string> code;
string S;
while (inFile >> S) {
code.push_back(S);
inFile >> name >> type1 >> type2 >> evolveTo >> evolveLevel >> hp >> atk >> def >> spd >> catchRate;
cout << name << type1 << type2 << evolveTo << evolveLevel << hp << atk << def << spd << catchRate;
}
system("PAUSE");
return 0;
The output is:-
I wrote a piece of code to do that but I'm not sure if it is exactly what you wanted:
struct Pokemon
{
public:
std::string Name;
std::string Type1;
std::string Type2;
int IdxVector[6];
float Stat;
};
int main(int argc, char *argv[])
{
std::string TxtLine;
int count;
std::ifstream F("./TextFile.txt");
std::list<Pokemon> PList;
while(std::getline(F,TxtLine))
{
Pokemon P;
int idx=TxtLine.find_first_of('|');
std::string Element=TxtLine.substr(0,idx);
P.Name=Element;
TxtLine.erase(0,idx+1);
idx=TxtLine.find_first_of('|');
Element=TxtLine.substr(0,idx);
P.Type1=Element;
TxtLine.erase(0,idx+1);
idx=TxtLine.find_first_of('|');
Element=TxtLine.substr(0,idx);
P.Type2=Element;
TxtLine.erase(0,idx+1);
for(int i=0;i<6;i++)
{
idx=TxtLine.find_first_of('|');
Element=TxtLine.substr(0,idx);
P.IdxVector[i]=atoi(Element.c_str());
if(Element.compare(" ")==0)
P.IdxVector[i]=-1;
TxtLine.erase(0,idx+1);
}
P.Stat=atof(TxtLine.c_str());
PList.push_back(P);
}
return 0;
}
I get this error " 0xC0000005: Access violation writing location 0xfeeefeee" in c++ program.
My code is
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee
{
public:
string name;
int age;
int phone;
int salary;
};
int main()
{
Employee emp1;
ofstream f1;
f1.open("qwe.txt",ios::binary|ios::app);
for(int i=0;i<2;i++)
{
cout<<"enter name:\t";
cin>>emp1.name;
cout<<"enter age:\t";
cin>>emp1.age;
cout<<"enter phone:\t";
cin>>emp1.phone;
cout<<"enter salary:\t";
cin>>emp1.salary;
cout<<"\n";
f1.write((char *)(&emp1),sizeof(Employee));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt",ios::binary|ios::in);
while(f2)
{
f2.read((char *)(&emp2),sizeof(Employee));
if(f2.eof())
{
break;
}
else
{
cout<<"\n"<<emp2.name;
cout<<"\n"<<emp2.age;
cout<<"\n"<<emp2.phone;
cout<<"\n"<<emp2.salary<<"\n";
}
}
f2.close();
cin.get();
return 0;
}
I think the problem is in while(f2). But I am not sure. This line f2.read((char *)(&emp2),sizeof(Employee)) may create problem. But I need this line.
You could not read/write structures with a complex types, like std::string that way. They have an internal structure which is implementation specific. Directly overriding its memory is a surest way to shoot yourself in the foot. Use >>/<< operators instead:
f1 << emp1.name;
f1 << emp1.age;
//...
f2 >> emp2.name;
f2 >> emp2.age;
The internal representation of a class is implementation defined. Additionally the string member can hold the data in the member directly or in an additional heap object depending of the number of charaters.
That's why you need a serialization of instances of your class. The serialize function will take serialization target like ofstream an writes a represenation of the data. The deserialization function will take a serialization source like ifstream and reads the reprensentation to the members.
If you need a low level API as used in your code(read/write) check out:
Be careful, the following line can break your data, I was changed this to avoid the previous valued writed by you.
f1.open("qwe.txt", ios::binary | ios::trunc);
Complete code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
class Employee {
public:
string name;
int age;
int phone;
int salary;
};
int main() {
Employee emp1;
ofstream f1;
f1.open("qwe.txt", ios::binary | ios::trunc);
for (int i = 0; i < 2; i++)
{
cout << "enter name:\t";
cin >> emp1.name;
cout << "enter age:\t";
cin >> emp1.age;
cout << "enter phone:\t";
cin >> emp1.phone;
cout << "enter salary:\t";
cin >> emp1.salary;
cout << "\n";
size_t lenght = emp1.name.size();
char lenghtval[sizeof(lenght)];
std::memcpy(&lenghtval, &lenght, sizeof(lenght));
f1.write(lenghtval, sizeof(lenght));
const char *name = emp1.name.c_str();
f1.write(name, static_cast<int>(lenght));
int val = emp1.age;
char towrite[sizeof(val)];
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.phone;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.salary;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt", ios::binary | ios::in);
while (f2) {
size_t lenght = 0;
char lenghtval[sizeof(lenght)];
f2.read(lenghtval, sizeof(lenght));
std::memcpy(&lenght, lenghtval, sizeof(lenght));
char name[lenght + 1];
f2.read(name, static_cast<int>(lenght));
name[lenght] = '\0';
emp2.name = name;
int val = 0;
char toread[sizeof(val)];
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.age = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.phone = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.salary = val;
if (f2.eof()) {
break;
}
cout << "\n" << emp2.name << std::endl;
cout << "\n" << emp2.age;
cout << "\n" << emp2.phone;
cout << "\n" << emp2.salary << "\n";
}
f2.close();
cin.get();
return 0;
}
Hi so I started to create a program to calculate a person's GPA and save that info for future reference. My problem is that I can't figure out how to read the numbers saved in the .txt file and then store them for GPA calculating purposes. Here is the unfinished program's code and any help would be great
EDIT: The .txt file is laid out like this: 5.0 3.7 5.0 3.7 5.0 4.0 ... Below is my progress in the program but when I run it I receive a GPA of 0 (incorrect). Not sure if the lexical cast is my problem, the getline() method or something else. Any help (the calculateGPA() method is the trouble area)?
#include <iostream>
#include <fstream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
string newCC, newCG;
double curGPA;
char command;
bool running = 1;
void calculateGPA();
void writeGrades();
void mainMenu();
void addClass();
int main()
{
while(running) {
mainMenu();
}
return 0;
}
void calculateGPA()
{
double credit_sum = 0.0, grade_sum = 0.0;
double credit, grade;
ifstream gReader("grades.txt");
for( int i = 0; ! gReader.eof() ; i++ )
{
string number;
getline( gReader , number ) ;
double dblNumber;
try
{
dblNumber = boost::lexical_cast<double>(number);
}
catch (boost::bad_lexical_cast const&)
{
dblNumber = 0;
}
credit_sum = credit_sum + dblNumber;
}
ifstream cReader("credits.txt");
for( int i = 0; ! cReader.eof() ; i++ )
{
string number;
getline( cReader , number ) ;
double dblNumber;
try
{
dblNumber = boost::lexical_cast<double>(number);
}
catch (boost::bad_lexical_cast const&)
{
dblNumber = 0;
}
credit_sum = credit_sum + dblNumber;
}
if(credit_sum == 0.0) {
curGPA = 0.0;
}
curGPA = (grade_sum / credit_sum);
cReader.close() ;
gReader.close() ;
}//End calculateGPA
void writeGrades()
{
string cToWrite = newCC + "\n";
string gToWrite = newCG + "\n";
ofstream cWriter("credits.txt", ios::app);
cWriter << cToWrite;
ofstream gWriter("grades.txt", ios::app);
gWriter << gToWrite;
cWriter.close();
gWriter.close();
}//End writeGrades
void addClass()
{
cout << "New class' credits?"; cin >> newCC;
cout << endl << "New class' grade? (GP)"; cin >> newCG;
writeGrades();
cout << "Add another class? (y/n)" << endl; cin >> command;
if(command == 'y')
addClass();
else mainMenu();
}//End addClass
void mainMenu()
{
string command;
cout << "What would you like to do?" << endl;
cout << "(V)iew GPA" << endl;
cout << "(A)dd grades" << endl;
cout << "(E)xit" << endl;
cin >> command;
if(command == "v")
{
calculateGPA();
cout << "Your current GPA is " << curGPA << endl;
}
else if(command == "a")
{
addClass();
}
else running = 0;
}//End mainMenu
Using streams:
double calculateGPA() {
double credit_sum = 0.0, grade_sum = 0.0;
double credit, grade;
ifstream reader("grades.txt");
while (!reader.eof()) { /* while the file still has numbers to be read */
reader >> credit >> grade;
if (reader.fail()) {
/* something unexpected happened (read error/formatting error) */
break;
}
credit_sum += credit;
grade_sum += grade;
}
if(credit_sum == 0.0) {
/* avoid divide by zero */
return 0.0;
}
/* divide the total grade by the total credits */
return grade_sum / credit_sum;
}
Notes:
Assumes .txt file just has numbers (credits, grade, credits, grade, ...) delimited by whitespace (spaces or
line breaks): For more complicated formatting you want scanf or
regex.
Returns double instead of float, you usually want double's precision
over float's relatively small speed and memory advantage.
File will be closed at the end of the function, when reader goes out
of scope. reader.close() is not necessary, but wouldn't be a bad
thing to put in (I'm just lazy.)
Look at this function: http://www.cplusplus.com/reference/cstdlib/atof/
You could use it to convert the string (or char) being read in into a double. I assume you want double because grades are 4.0, 3.7, 3.3, 3.0, etc.
Or, you can read into a double directly.
calculateGPA()
{
double credit;
...
reader >> credit;
...
}
Also, in your WriteGrades function, why are you writing out new lines (use endl instead of '\n') before your line instead of at the end?
I'm working on this project and I'm fairly new to C++. Its kind of hard to explain what I'm trying to do but I shall try. So I'm working with a file called flix.txt and in it looks like the following:
1 A 5
1 B 4
1 D 3
1 F 5
2 A 1
3 E 3
3 F 1
4 A 2
The first column are people(my objects), second columns are movies, and the third are the ratings given by the objects.
I'm trying to first extract the first int from every line and create an object using an 'operator new'. Then I'm taking a movie and turning it into an int so I can plug the rating into an array. Sorry if it sounds confusing. Heres the code I have now:
//flix program
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#define NUMBER_OF_MOVIES 6
using namespace std;
int tokenize(string line);
int getMovieNum(char movie);
void resetPos(istream& flix);
class Matrix{
public:
int movieRate[NUMBER_OF_MOVIES];
};
int main(){
int distinctCount = 0;
int checker = -1;
int check = 0;
string line;
int personNum;
char movie;
int rating;
int movieNum;
ifstream flix("flix.txt");
ofstream flick("flix1.txt");
//identify distinct account numbers in file
while(getline(flix, line)){
check = tokenize(line);
if(check != checker)
distinctCount++;
checker = check;
check = 0;
}
//reset position in file
resetPos(flix);
//create objects in accordance with distinct numbers
Matrix* person = new Matrix[distinctCount];
for(int i = 0; i < distinctCount; i++){
for(int j = 0; j < NUMBER_OF_MOVIES; j++){
person[i].movieRate[j] = 0;
cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
}
cout << "\n";
}
//reset position in file
resetPos(flix);
//get data from file and put into respective variables
while(getline(flix, line)){
flix >> personNum >> movie >> rating;
cout << personNum << ' ' << movie << ' ' << rating << endl;
//changes the char into an int
movieNum = getMovieNum(movie);
person[personNum].movieRate[movieNum] = rating;
}
//reset position in file
resetPos(flix);
//input ratings into movie array
for(int i = 0; i < distinctCount; i++){
for(int j = 0; j < NUMBER_OF_MOVIES; j++){
cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
flick << i + 1 << ' ' << person[i].movieRate[j] << endl;
}
}
//write data to text file
//??
flick.close();
//free memory
delete[] person;
system("pause");
return 0;
}
int tokenize(string line){
string myText(line);
istringstream iss(myText);
string token;
getline(iss, token, ' ');
int strInt = atoi(token.c_str());
return strInt;
}
int getMovieNum(char movie){
int movieNum = 0;
switch(movie){
case 'A':
movieNum = 1;
break;
case 'B':
movieNum = 2;
break;
case 'C':
movieNum = 3;
break;
case 'D':
movieNum = 4;
break;
case 'E':
movieNum = 5;
break;
case 'F':
movieNum = 6;
break;
default:
movieNum = 0;
break;
}
return movieNum;
}
void resetPos(istream& flix){
flix.clear();
flix.seekg(0);
}
I also apologize in advance if there are noobish mistakes here.
I think the problem is somewhere in the while loop, that's where it keeps locking up. I spent hours on this and I can't figure out why it doesn't work. In the while loop, I'm trying to access every line of the file, snag the data from the line, take the movie char and turn it into an int, and then plug the data into the array within the object. When I did have it working, all the data was wrong too. Any input is highly appreciated. Thanks in advance.
You need to change a little in your program and i'l paste only the changed part.
Somewhere after you reset the person[].movieRate[] to zero,you have written this while loop
resetPos(flix);
int k = NUMBER_OF_MOVIES + 2; //this is the variable that i have declared
//get data from file and put into respective variables
while(k){ //do you see that instead of getline() i have used the variable k. i'l tell you why later
flix >> personNum >> movie >> rating;
//personNum = tokenize(line,1);
cout << personNum << ' ' << movie << ' ' << rating << endl;
//changes the char into an int
movieNum = getMovieNum(movie);
person[personNum - 1].movieRate[movieNum] = rating; //this is personNum-1 and NOT personNum the most common mistake while indexing array.
k--;
}
this code seems to work as your criteria.
the reason that i removed getline() is, if u call getline then the get pointer position will be incremented. so after this you call flix >> something... , this reads the data from the second line. your first line 1 A 5 is lost. this was the cause of the trouble. change it n let me know.
Okay, let me try to give at least some idea of a simple starting point:
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>
struct rater {
std::vector<int> ratings;
rater() : ratings(6) {}
friend std::istream &operator>>(std::istream &is, rater &r) {
char movie;
is >> movie;
return is >> r.ratings[movie-'A'];
}
friend std::ostream &operator<<(std::ostream &os, rater const &r) {
for (int i=0; i<r.ratings.size(); i++) {
os << char(i + 'A') << ":" << r.ratings[i] << "\t";
}
return os;
}
};
int main() {
std::ifstream in("flix.txt");
std::vector<rater> ratings(5);
int i;
while (in >> i)
in >> ratings[i-1];
i=1;
for (auto r : ratings)
std::cout << i++ << "-> " << r << "\n";
}
Here's a bit of a clean up. It uses std::map to keep track of the Person and Movie keys, which is more flexible as textual strings of any kind (sans whitespace) can be used. You've added a comment saying you specifically want to list movies people didn't rate in their outputs - that can be done by using a std::set and ensuring each movie name encountered is inserted, then using an iteration over the set to guide lookups in each person's ratings: left as an exercise.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
using namespace std;
typedef std::map<std::string, int> Movie_Ratings;
typedef std::map<std::string, Movie_Ratings> Persons_Movie_ratings;
int main()
{
if (!ifstream flix("flix.txt"))
{
std::cerr << "error opening input\n";
exit(1);
}
if (!ofstream flick("flix1.txt"))
{
std::cerr << "error opening output\n";
exit(1);
}
Persons_Movie_Ratings ratings;
std::string line;
while (getline(flix, line))
{
istringstream iss(line);
string person, movie;
int rating;
if (line >> person >> movie >> rating)
ratings[person][movie] = rating;
}
// input ratings into movie array
for (Persons_Movie_Ratings::const_iterator i = ratings.begin();
i != ratings.end(); ++i)
{
for (Movie_Ratings::const_iterator j = i->second.begin();
j != i->second.end(); ++j)
{
cout << i->first << ' ' << j->second << endl;
flick << i->first << ' ' << j->second << endl;
}
}
system("pause");
}
have a project where I'm supposed to ask the user to input a file name and then take that file and make an array of structs. I'm completely lost! Here is what i have so far
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string filename;
ifstream inFile;
struct ftballPlayer
{
int NO;
string first;
string last;
char POS;
char clas;
int height;
char ds;
int iheight;
string hometown;
};
int counter=0;
const int MAX_PLAYER=50;
void printList(const ftballPlayer list[], int listSize);
void printOne ( ftballPlayer two);
void getData(ifstream& inFile, ftballPlayer list[], int& listSize);
int main ()
{
ftballPlayer list[MAX_PLAYER] ;
cout << "Enter the name of the input file: " ;
cin >> filename;
inFile.open(filename.c_str () );
if (!inFile)
{
cout<<"Cannot Open Input File."<<endl;
cout<< "Program Terminates!"<<endl;
return 1;
}
inFile.ignore (200,'\n');
getData (inFile, list, counter);
for ( counter = 0;counter < 50;counter++)
{
printOne (list[ counter] ) ;
cout <<endl;
}
return 0;
}
void getData(ifstream& inFile, ftballPlayer list[], int& listSize)
{
ftballPlayer item ;
listSize = 0;
inFile >> item.NO >> item.first >> item.last
>> item.POS >> item.clas >> item.height
>> item.ds >> item.iheight >> item.hometown;
while (inFile )
{
list [listSize ] = item ;
listSize++;
inFile >> item.NO >> item.first >> item.last
>> item.POS >> item.clas >> item.height
>> item.ds >> item.iheight >> item.hometown;
}
inFile.close () ;
}
void printList(const ftballPlayer list[], int listSize)
{
int looper;
for ( looper = 0; looper < listSize ; looper++)
{
printOne ( list [looper] );
cout << endl ;
}
}
void printOne ( ftballPlayer one)
{
cout << fixed << showpoint << setprecision (2);
cout << "NO " << one.NO;
cout << setw(5) << left << " Name: " << one.first << " " << one.last;
cout << " POS " << one.POS << setw(5);
cout << "Class "<<one.clas<<setw (5);
cout << "Height "<<one.height<<" "<<one.ds<<" "<<one.iheight<<setw(5);
cout << "Hometown " << one.hometown << endl;
}
Can someone tell me if I'm on the right track? The print out I get is not even close to the text file which is this.
NO NAME POS CLASS HEIGHT WEIGHT Hometown/High School/Last College
60 Josh Mann OL SO 6-4 300 Virginia Beach, Va./Ocean Lakes
64 Ricky Segers K/P FR 5-11 185 Glen Allen, Va./Henrico
70 Brandon Carr OL RS_SR 6-2 305 Chesapeake, Va./Western Branch/Fork Union Military Academy
53 Calvert Cook LB FR 6-0 250 Norfolk, Va./Booker T. Washington
51 Michael Colbert DE RS_SR 6-1 230 Fayetteville, N.C./E.E. Smith
22 T.J. Cowart CB RS_JR 5-9 190 Virginia Beach, Va./Ocean Lakes
1 Jakwail Bailey WR SO 5-11 185 Haddonfield, N.J./Paul VI
25 Andre Simmons S JR 6-0 205 Lorton, Va./South County/Vanderbilt
34 Johnel Anderson RB FR 5-8 180 Sicklerville, N.J./Paul VI
This is one of three that the user can input but they all have the same type of information. I've looked in my text book and have been hunting online for hours and I can't find anything about doing this when the user inputs the file instead of just starting with a file. Any help or direction would be greatly appreciated.
A few things that I see wrong right away.
struct ftballPlayer
{
int NO;
string first;
string last;
char POS; // Should be multiple characters
char clas; // Should be multiple characters
int height;
char ds;
int iheight;
string hometown;
};
I suspect that if you get the right sizes of your POS/ clas, it will work better. Why not make them strings?
Also, there is some issue with reading the last bit. It looks like you have to look for the / to tell the various pieces apart, and I don't see you doing that.
Overall, it would be much cleaner to read this if you could change the format of the input, but I suspect you aren't able to. Comma separated values are very easy to read, as are any other divider.