c++ - I don't get the right data. I just got random number -
my assignment arrays , structures. first part need read data input file. however, i'm stuck on that. know got random numbers. however, can't find it. took me 1 hour find mistakes. can guys please show me got wrong? i'll appreciate it. thank you!!!
#include <iostream> #include <fstream> #include <string> using namespace std; const int size=15; // here structure declaration. struct bestcolleges { int rnk; // rank of college. string name; //the college name. int years; // years did college establish? string email; // address of college. double tranferrate; // % of students graduate , transfer double retentionrate; // % of students first year retention. int low_cost; // lower cost of attendance int high_cost; // higher cose of attendance double numstudents; // how many students in college. }; void getdata (bestcolleges c[], bestcolleges *plast); void printcollege1 (bestcolleges college[],bestcolleges *plast); int main() { ifstream inputfile; int num_college; cout << " welcome program!!! " << endl; bestcolleges *college; college = new bestcolleges[size]; bestcolleges *ptr; bestcolleges *plast = college+ size-1; ptr= college; // here function definition getdata( college, plast); printcollege1( college, plast); // i'm testing if got data input file //cout << num_college << endl; cout << " thank reading program! see again!" << endl; return 0; } // here fuction definition void getdata (bestcolleges c[], bestcolleges *plast) { ifstream inputfile; int num_college; inputfile.open("ca_bestcolleges.txt"); inputfile >> num_college; ( bestcolleges *p =c; p < plast; p++) { inputfile >> p->rnk; getline(inputfile,p->name); inputfile >> p->years; getline(inputfile,p->email); inputfile >> p->tranferrate; inputfile >> p->low_cost; inputfile >> p->high_cost; inputfile >> p->numstudents; } inputfile.close(); } void printcollege1 (bestcolleges c[],bestcolleges *plast) { ( bestcolleges *ptr = c; ptr < plast; ptr++) // here printing entirely information. { cout << " rank: " << ptr->rnk << endl; cout << " name: " << ptr->name << endl; cout << " email: " << ptr->email << endl; cout << " graduation , transfer rate: " << ptr->tranferrate << endl; cout << " first year retention rate: " << ptr->retentionrate << endl; cout << " cost of attendance: " << "$" << ptr->low_cost << " - " << "$" << ptr->high_cost << endl; cout << " number of students: " << " approx. " << ptr->numstudents << endl; cout << endl; } }
here input file:
15 palo verde college 1947 http://www.paloverde.edu 31.2 82.7 14266 18266 3898 4 diablo valley college 1949 http://www.dvc.edu 50.2 90.5 14839 20579 24781 6 foothill college 1957 http://www.foothill.edu 68.8 87.5 12300 19302 18362 12 college of siskiyous 1957 http://www.siskiyous.edu 59.8 82.0 15306 21936 2473 10 cuesta college 1963 http://www.cuesta.edu 50.7 86.2 12052 19135 9571 8 ohlone college 1965 http://www.ohlone.edu 52.1 91.1 10898 15878 18000
you bad results because you're not parsing correctly:
for ( bestcolleges *p =c; p < plast; p++) { inputfile >> p->rnk; getline(inputfile,p->name); inputfile >> p->years; getline(inputfile,p->email); inputfile >> p->tranferrate; inputfile >> p->low_cost; inputfile >> p->high_cost; inputfile >> p->numstudents; }
you're missing inputfile >> p->retentionrate;
. because of next items read unexpected data , cause bad results.
Comments
Post a Comment