I am trying to combine these two loops together per my instructions, but I cant figure out how I should do something like this. since my counter i in the for loop can't go inside while loop condition. My teacher wants the program to do one loop only. Thanks in advance. the code basically runs through the a txt file and is supposed to first assign values to an array and in the for loop I assign it to a pointer.
This part I have problem with:
void fillArr(vector <student> &arr, vector <student *> &ptrels){
student temp;
ifstream inFile ("index.txt");
while (!inFile.eof()) {
inFile >> temp.fName >> temp.Lname >> temp.gpa >> temp.id >> temp.email;
arr.push_back(temp);
}
for (unsigned i = 0; i < arr.size(); i++ ){
ptrels.push_back(&arr[i]);
}
// combine the two above loops
}
Here is the whole code:
> #include <string>
> #include <cstdio>
> #include <iostream>
> #include <vector>
> #include <fstream>
> #include <sstream> using namespace std;
>
> struct student {
> string fName, Lname, email, id;
> double gpa; } ;
>
> void input(); void fillArr(vector <student> &arr, vector <student *>
> &ptrels); void printFile(vector <student *> pointarray); void
> sortFile(vector <student> &arr, vector <student *> &pointarray);
>
> int main() {
> vector <student> elements;
> vector <student *> ptrels;
>
> ifstream inFile;
> inFile.open("index.txt");
>
> int answer;
> fillArr(elements, ptrels);
> cout << "Please select:" << endl
> << "1 = Select All" << endl
> << "2 = Order A-Z by Name"<<endl
> << "3 = To exit"<< endl;
> cin >> answer ;
> if (answer == 1){
> printFile(ptrels);
> main();
> }
> if (answer ==2){
> sortFile(elements, ptrels);
> printFile(ptrels);
> main();
> }
> if (answer ==3){
> inFile.close();
> exit(0);
> }
> else {
> cout << "Invalid Try Again:"<< endl;
> main();
> }
> return 0; }
>
> void fillArr(vector <student> &arr, vector <student *> &ptrels){
> student temp;
>
> ifstream inFile ("index.txt");
> while (!inFile.eof()) {
> inFile >> temp.fName >> temp.Lname >> temp.gpa >> temp.id >> temp.email;
> arr.push_back(temp);
> }
> for (unsigned i = 0; i < arr.size(); i++ ){
> ptrels.push_back(&arr[i]);
> }
>
> // combine the two above loops }
>
>
>
> void printFile(vector <student *> pointarray){
> for (unsigned j = 0; j < pointarray.size(); j++){
> cout << pointarray[j] -> fName << " ";
> cout << pointarray[j] -> Lname<< " ";
> cout << pointarray[j] -> gpa << " ";
> cout << pointarray[j] -> id << " ";
> cout << pointarray[j] -> email << " ";
> cout << endl;
> } }
>
> //swap the elements by pointer. you are swaping the record not the
> pointers. // only sorting by firstname, sort by all 5 void
> sortFile(vector <student> &arr, vector <student *> &pointarray){
> for(unsigned i = 0; i < arr.size(); ++i){
> for(unsigned j = i + 1; j < arr.size(); ++j) {
> if(arr[i].fName > pointarray[j] -> fName) {
> swap(arr[i].fName,pointarray[j] ->fName);
> swap(arr[i].Lname,pointarray[j] ->Lname);
> swap(arr[i].gpa,pointarray[j] ->gpa);
> swap(arr[i].id,pointarray[j] ->id);
> swap(arr[i].email,pointarray[j] ->email);
> }
> }
> } }
Also I know I am supposed to ask this in a different question but she also want me to do the sorting which is the last function sortFile to sort by all values not only firstName. Plus somehow she hates the swap function, looking for alternatives. Any hints would be appreciated.