I am working on a sorting program for hockey stats, (school assignment) and for some reason when I attempt to read from a text file I get weird output. It doesn't read the entire file (output is "Chris", file reads: CHRIS NEIL ,"6' 1""",215,18-Jun-79,33,"FLESHERTON, ON, CAN") and while I only have one system pause written into the file it gives me two, but the second one is only non alphabetic characters. I believe the problem is in these lines of code:
cout << "Enter the name of the file you wish to choose from." << endl;
getline(cin, fname); // Getting name of file to open
infile.open(fname.c_str());
getline(infile, oops);
cout << "" << oops << endl;
but I included the entire program.
/* Jeremy Bredeson
Hockey Statistics Sept 13 2012 */
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
struct date {
int day;
int month;
int year;
} ;
struct place {
string city;
string province;
string country;
} ;
struct players {
string name;
int height;
int weight;
date birth;
int age;
place home;
} ;
string pname (string passed) { // Function to search through string read from file and return only
string retname = ""; // the name of the player devoid of all punctuation
int namelen = passed.find(",");
for (int i=0; i<namelen; i++) {
if (passed[i] >= 'A' && passed[i] <= 'Z')
retname += passed[i];
else if (passed[i] >= 'a' && passed[i] <= 'z')
retname += passed[i];
else if (passed[i] = ' ' && passed[i+1] != ' ')
retname += passed[i];
}
return retname;
}
cout << "Enter the name of the file you wish to choose from." << endl;
getline(cin, fname); // Getting name of file to open
infile.open(fname.c_str());
getline(infile, oops);
cout << "" << oops << endl;
/*cout << "To just print the name of the player hit 'ENTER' now." << endl;
player[0].name = pname(temp);
cout << "" << player[0].name << endl; */
system("pause");
return 0;
}
string pname (string passed) { // Function to search through string read from file and return only
string retname = ""; // the name of the player devoid of all punctuation
int namelen = passed.find(",");
for (int i=0; i<namelen; i++) {
if (passed[i] >= 'A' && passed[i] <= 'Z')
retname += passed[i];
else if (passed[i] >= 'a' && passed[i] <= 'z')
retname += passed[i];
else if (passed[i] = ' ' && passed[i+1] != ' ')
retname += passed[i];
}
return retname;
}
Im not sure i understand. You have a function called pname(string) which returns the players first name, and this is the only function that gets called within the main function. Its doing what you had asked it too.
1 2 3 4 5
/*cout << "To just print the name of the player hit 'ENTER' now." << endl;
player[0].name = pname(temp);
cout << "" << player[0].name << endl; */