Hi guys,

I have been given a task in my C++ course to develop an application that will ask the names of the students and store the entered values into a string type array. Considering each student has participated in four exams, the program should ask the points received and store them into an int type array. Then the application should display the names of the students and the average value of the exams from each.

Key Points:

a) Use always pointer notations when writing/reading information to/from arrays.
b) You can’t ask “How many names will be inputted”.

I know how to develop the program without using pointers, but that is the whole point of the exercise. I guess pointers are the most challenging part of C++, any help would be much appreciated.

Thank you in advance.

Kind Regards,

croussou

I guess pointers are the most challenging part of C++, any help would be much appreciated.

Pointers are fantastically simple and easy; they're just often badly taught using clumsy metaphors that do more harm than good.

Anyway, here's some code that takes in as many names as you care to enter, managing the array size as it goes. Read, digest, understand. Then you can write your own, and develop the code to hold each student's score as well. Any questions, ask them here, and we'll answer.

#include <iostream>
#include <string>

using namespace std;

// start by allocating space for ten strings

int maxNumAllowed = 10;
int numStudents = 0;
string* pStudentArray = new std::string[10];

int main()
{

char moreStudents = 'Y';
while(moreStudents == 'Y')
  {
    if (numStudents == maxNumAllowed)
      {
	// need bigger array
	maxNumAllowed += 10;
        cout << "Making new array of size" << maxNumAllowed << endl;
        string* newPStudentArray =  new std::string[maxNumAllowed];
        for (int i =0; i < numStudents; ++i)
	  {
	    newPStudentArray[i] = pStudentArray[i];
	  }
	// have now copied all old data to new array
	delete[] pStudentArray; // Tidy away old array
	pStudentArray = newPStudentArray; // now pStudentArray points to more space
      }
    cout << "Next student name?" << endl;
    cin >> pStudentArray[numStudents];
    numStudents++;
    cout << "Another? (Y/N)" << endl;
    cin >> moreStudents;
  }

cout << "You entered " << numStudents << "students. They are: ";
for (int i =0; i < numStudents; ++i)
  { cout << pStudentArray[i] << " ";}

return 0;
}

Edited 5 Years Ago by Moschops: n/a

Moschops

Thank you for your prompt reply.

I will have a look at the code you provided and then try develop my own.

I will let you know in case something else comes up.

Once again, thank you.

Kind Regards,

croussou

This article has been dead for over six months. Start a new discussion instead.