traversing vectors in c++?

Question by sgtjoebear: traversing vectors in c++?
This is my problem, I am supposed to print out the average score, student with highest score and student that is below the average score, and the student with the highest score. All using what is included here and I may add int i; or similar. This must be don using vectors and iterators

Code so far
#include
#include
#include
#include
#include
#include

using namespace std;
#define ARRAY_SIZE 50 //maximum number of students in array
#define NAME_SIZE 40 //maximum number of characters in studentName

#define cinGetValue(value,status) \
{ \
string inputLine; /* raw user input text*/ \
istringstream convert; /* string converstion variable.*/ \
\
getline(cin,inputLine); /* get a string of data from user input (could be empty) */ \
convert.str(inputLine); /* load ‘convert’ with user input */ \
convert >> value; /* load ‘convert’ with user input */ \
status = inputLine.empty(); /* boolean indicator of empty or !empty */ \
}

// define the “payload” to be maintained in our
// array data structure (if you’re fuzzy on
// “struct”, see the teacher’s note “Data
// Structures Intro.” in Doc Sharing).
// —————————————-…
struct payload
{
string studentName;
int testScore;
};
void displayArray (struct payload *start,struct payload *end);
void dispLowScore (struct payload *start,struct payload *end);
void dispHighScore(struct payload *start,struct payload *end);
void dispStudent (struct payload *student);

int main()
{
int count; // counter to make prompting for input a little prettier

struct payload masterArray[ARRAY_SIZE]; //master array for display forward & reverse
struct payload *arrayPtr = &masterArray[0]; //load pointer with address of first array element

// display the header
// ——————
cout << "Week-2: Student Statistics" << endl; cout << "--------------------------" << endl; // load array // ---------- count = 0; while (arrayPtr <= &masterArray[ARRAY_SIZE-1]) { bool status; char name[NAME_SIZE]; cout << setw(2) << count++ << setw(2) << ": Student Name? "; gets_s(name,sizeof(name)); if (name[0] == NULL) break; arrayPtr->studentName = name;
cout << " Score? "; cinGetValue(arrayPtr->testScore,status);
arrayPtr++;
}
cout << endl; if (arrayPtr == &masterArray[0]) cout << endl << "” << endl; else { cout << endl; displayArray(&masterArray[0],arrayPtr-1)… dispLowScore (&masterArray[0],arrayPtr-1); cout << endl; dispHighScore(&masterArray[0],arrayPtr-1… } // pause // ----- getchar(); exit (1); } // displayArray / void displayArray(struct payload *arrayStart, //first element of array struct payload *arrayEnd ) //last element of array { // output label // ------------ cout << "Complete student array:" << endl; // display the array elements from the start to the end // (two lines of code required). // // Remember that the parameters are local COPIES of // the caller's values (pushed to the stack) and // changing them won't affect the caller values at all. // ----------------------------------------… if (studentCount == 0) averageScore = 0; else averageScore = (float)totalScore / (float)studentCount; cout << averageScore << endl; // Traverse array & output every student & score below the average // Make sure you handle your traversal pointer in both cases: // student below average & student >= average.
//
// Again, very similar to previous loop, except now you only
// display students below the averageScore (2 lines of code + the
// commented-out one)
// —————————————-…
//CODE NEEDED HERE
// dispStudent(tempPtr);
//CODE NEEDED HERE
}

// dispHighScore()
// —————
void dispHighScore(struct payload *arrayStart, //first element of array
struct payload *arrayEnd ) //last element of array
{
int highScore; //found by traversing list
struct payload *tempPtr ; //used t

Best answer:

Answer by Cubbi
Your program did not fit in the yahoo answers post and was truncated and some of the lines were also truncated. Try using pastebin or a similar service, and post an URL to the complete source code.

As far as the code that was posted, a few pointers:

1. don’t use #define, this is C++, not C
2. don’t use the word “struct” in front of type name, this is C++, not C
3. consider using vectors instead of arrays. In fact, I think you said your assignment was to use vectors, wasn’t it?
4. stop using pointers (this comes naturally with switching to vectors)

Give your answer to this question below!