Array vs Vector

I'm really confused on how to manipulate my code and change it from array into vector.

This is the code:

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>


using namespace std;


double ScoreToGrade(double);
void displayOne (struct students *studinfo , int , int );
void displayAll (struct students *studinfo, int );
void IDsort (struct students *studinfo, int );

struct students
{
int id;
string name;
string course;
int credit;
int score;
double grade;
};


int main()
{
ifstream inputFile;

const int SIZE = 999;
students studinfo[SIZE];
int counter = 0;
int data = 0;
string data2 = "";
string data3 = "";
int data4 = 0;
int data5 = 0;

inputFile.open("StudentRecords.txt");
/*
12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64

12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71

12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69
14388 Henry CS3 3 80
15667 Peter CS3 3 45
*/


if (inputFile.is_open())
{
while (inputFile >> studinfo[counter].id >> studinfo[counter].name >> studinfo[counter].course >> studinfo[counter].credit >> studinfo[counter].score)
{
studinfo[counter].grade = ScoreToGrade(studinfo[counter].score);
counter++;
}
inputFile.close();
}
else cout << "Unable to open file";



displayOne(studinfo, counter, 12546);

displayOne(studinfo, counter, 14328);

displayOne(studinfo, counter, 15667);

displayAll(studinfo, counter);

IDsort(studinfo, counter);

displayAll(studinfo, counter);


return 0;
}

double ScoreToGrade(double x)
{
/*
Score Range

Grade score
100 - 90 4.0
89 - 80 3.0
79 - 70 2.0
69 - 60 1.0
0 -59 0.0
*/

double score = 0.0;

if (x >= 90 && x <= 100)
score = 4.0;
else if (x >= 80 && x <= 89)
score = 3.0;
else if (x >= 70 && x <= 79)
score = 2.0;
else if (x >= 60 && x <= 69)
score = 1.0;
else if (x >= 0 && x <= 59)
score = 0.0;

return score;
}

void displayOne (students *studinfo, int counter, int id)
{
int prevID = 0;
int total = 0;
int credit = 0;

cout << setprecision(2) << fixed;

for (int i = 0; i < counter; i++)
{
if (id == studinfo[i].id)
if (prevID == studinfo[i].id)
{
cout << studinfo[i].course << setw(10) << studinfo[i].credit << setw(10) << studinfo[i].grade << "\n";
credit = credit + studinfo[i].credit;
total = total + studinfo[i].credit * studinfo[i].grade;
}
else
{
cout << "id" << setw(11) << "name" << "\n";
cout << "-----------------" << "\n";

cout << studinfo[i].id << setw(9) << studinfo[i].name << "\n\n";

cout << "Course" << setw(9) << "Credit" << setw(8) << "Grade" << endl;
cout << "-------" << setw(9) << "-------" << setw(8) << "------" << endl;

cout << studinfo[i].course << setw(10) << studinfo[i].credit << setw(10) << studinfo[i].grade << "\n";
prevID = studinfo[i].id;
credit = credit + studinfo[i].credit;
total = total + studinfo[i].credit * studinfo[i].grade;
}



}
cout << setw(15) << "GPA: " << (double)total / credit << "\n\n";
}

void displayAll (students *studinfo, int counter)
{

cout << "Name" << setw(11) << "Course" << setw(11) << "Credit" << setw(11) << "Score" << setw(11) << "Grade" << "\n\n";

for (int i = 0; i < counter; i++)
{
cout << setprecision(2) << fixed;
cout << setw(3) << studinfo[i].name << "\t" << setw(7) << studinfo[i].course << setw(11) << studinfo[i].credit << setw(11) << studinfo[i].score << setw(11) << studinfo[i].grade << "\n";
}
}

void IDsort (students *studinfo, int counter)
{
bool swap = false;
students temp;
while (!swap)
{
swap = true;
for (int i = 0; i < (counter - 1); i++)
{
if (studinfo[i].id > studinfo[i + 1].id)
{
temp = studinfo[i];
studinfo[i] = studinfo[i + 1];
studinfo[i+1] = temp;
swap = false;
}
}
}
}
It's actually not that hard once you read up on <vector>. Start by adding #include <vector> to your include list. Then, replace arrays like student* studentInfo with vector<student> studentInfo. The good thing about <vector> is that it overloads operator [] so you don't have to change your array resolution brackets to function calls.
Also, I got this when running your code through IDEOne using g++ 6.3:

Unable to open file          GPA: -nan

          GPA: -nan

          GPA: -nan

Name     Course     Credit      Score      Grade

Name     Course     Credit      Score      Grade
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post to add the tags (and indentation).

1
2
3
4
void foo( Bar* ); // #1
void foo( std::vector<Bar> ); // #2
void foo( std::vector<Bar>& ); // #3
void foo( const std::vector<Bar>& ); // #4 

#1 (presumably) passes a pointer to array. The function accesses the elements of the caller's array via the pointer. Function does not know how many elements the array has (needs additional argument).

#3 passes a reference to an "array". The vector has member function size() that has count of items in the array. The function accesses the elements of the caller's array via the reference.

#2 creates a copy of the array that the function uses. Caller's array will not be changed.

#4 a reference, like #3, but function cannot modify the array and its elements.


You do create SIZE students at the start of main(), but fill and use only 'counter' of them. If the input would have more than SIZE students, then you would have a problem.

The vector approach would be to create empty vector, optionally preallocate memory, but create only as many students as necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<students> studs; // empty
studs.reserve( SIZE ); // optional, studs is still empty


students record;
while ( inputfile >> record ... ) {
  studs.emplace_back( record );
}


for ( size_t i=0; i < studs.size(); ++i ) {
  std::cout << studs[i].name ...
}
Registered users can post here. Sign in or register to post.