rfind function with files help
Feb 6, 2015 at 11:28pm UTC
I'm having trouble taking a students name from a file putting it into a string and cutting the string into first and last name. For this assignment I have to use the rfind function and a struct. Any help would be appreciated
Heres my code so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include <cstddef>
using namespace std;
//Struct to show student types
struct studentType
{
string str;
string key = (" " );int found = str.rfind(key);
string studentFName = str.substr(0, found); //students first name
string studentLName = str.substr(found + 1);// students last name
double testScore; //students test score
char grade; //students grade
};
int main()
{
int c = 0;
double highestTestScore = 0;
string fname = " " ;
string lname = " " ;
studentType students[20];
ifstream inFile;
inFile.open("student.dat" , ios::in);
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, students[c].str);
inFile >> students[c].testScore;
inFile.ignore(100, '\n' );
c++;
}
//if else statement for letter grade
for (int c = 0; c < 6; c++)
{
if (students[c].testScore>=90 && students[c].testScore <= 100)
students[c].grade = 'A' ;
else if (students[c].testScore >= 80 && students[c].testScore <= 89)
students[c].grade = 'B' ;
else if (students[c].testScore >= 70 && students[c].testScore <= 79)
students[c].grade = 'C' ;
else if (students[c].testScore >= 60 && students[c].testScore <= 69)
students[c].grade = 'D' ;
else
students[c].grade = 'F' ;
//if statement to get the students highest grade
if (students[c].testScore > highestTestScore)
{
highestTestScore = students[c].testScore;
fname = students[c].studentFName;
lname = students[c].studentLName;
}
}
cout << left << fixed;
cout << setw(15) << "Last Name" ;
cout << setw(15) << "First Name" ;
cout << setw(15) << "Test Score" ;
cout << setw(15) << "Grade" << endl;
//for loop for students
for (int i = 0; i < 6; i++)
{
cout << setw(15) << students[i].studentLName; //supposed to display last name
cout << setw(15) << students[i].studentFName; //supposed to display first name
cout << setw(15) << setprecision(1)<<students[i].testScore;
cout << setw(15) << students[i].grade << endl;
}
inFile.close();
}
else
{
cout << "Error Openning file" << endl;
}
cout << endl << "Highest test score: " << highestTestScore;
cout << " Student name: " << fname << " " <<lname<< endl;
system("pause" );
return 0;
}
Last edited on Feb 6, 2015 at 11:38pm UTC
Topic archived. No new replies allowed.