-1
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
    string name;
    int year;
    string semester;
    int AmtClass;
    string *CName;
    static string cohort[4];

public:
    Student();
    Student(int AmtClass);
    Student(Student &);

    void setCourses(string courses[], int n);
    void setName(string name);
    void setYear(int year);
    void setSemester(string semester);

    string getName();
    int getYear();
    string getSemester();
    int getAmtClass();
    string getCohort();
    string getCNames();
  //  ~Student();
};

string Student::cohort[4] = "";


Student::Student()
{
    name = "";
    year = 0;
    semester = "";
    AmtClass = 0;
}

//I am trying to create a dynamically allocated array of strings
Student::Student(int amount)
{
    AmtClass = amount;

    CName = new string[AmtClass];
}

void Student::setCourses(string courses[], int n)
{
    CName[n] = courses[n];
}

Student::Student(Student &obj)
{
    *this = obj;
}

void Student::setName(string Name)
{
    name = Name;
}

void Student::setYear(int Year)
{
    year = Year;

    switch(year)
    {
    case 1:
        cohort[0] = "Freshman";
        break;
    case 2:
        cohort[1] = "Sophomore";
        break;
    case 3:
        cohort[2] = "Junior";
        break;
    case 4:
        cohort[3] = "Senior";
        break;
    }
}

void Student::setSemester(string Semester)
{
    semester = Semester;
}

string Student::getName()
{
    return name;
}

int Student::getYear()
{
    return year;
}

string Student::getSemester()
{
    return semester;
}

int Student::getAmtClass()
{
    return AmtClass;
}

string Student::getCohort()
{
    year -= 1;
    return cohort[year];
}

//Returns only the first course name i entered, i want to return all
string Student::getCNames()
{
    return *CName;
}

void readStudentData(Student &);
void printStudentData(Student);

int main()
{
    int amount;
    cout << "How many courses are you currently taking? ";
    cin >> amount;

    Student kid(amount);

    readStudentData(kid);
    printStudentData(kid);
}

void readStudentData(Student &kid)
{
    cin.ignore(10000, '\n');

    int amount = kid.getAmtClass();
    string name = "";
    string semester = "";
    int year = 0;
    string *pName = new string[amount];

    cout << "What is your full name? ";
    getline(cin,name);
    kid.setName(name);

    cout << "Type in 1 for Freshman, 2 for Sophomore, 3 for Junior or 4 for Senior.";
    cout << "\nWhat year are you? ";
    cin >> year;
    while(cin)
    {   if(year >=1  && year <= 4)
        {
            kid.setYear(year);
            break;
        }
        else
        {
            cout << "Number must be between 1 and 4 Please try again." << endl;
            year = 0;
            cin >> year;
        }
    }

    cin.ignore();
    cout << "What is your current semester? ";
    getline(cin,semester);
    kid.setSemester(semester);

    cout << "Please enter the name of all your courses." << endl;
    for(int i = 0; i < amount; i++)
    {
        cout << "Course #" << i+1 << " : ";
        getline(cin,pName[i]);
        kid.setCourses(pName, i);
    }
}

void printStudentData(Student kid)
{
    Student kid2 = kid;
    cout << "\nHere is your information:" << endl;
    cout << "Name : " << kid2.getName() << endl;
    cout << "Semester : " << kid2.getSemester() << endl;
    cout << "Year : " << kid2.getCohort() << endl;
    for(int i = 0; i < kid2.getAmtClass(); i++)
    {
        cout << "Course #" << i+1 << " : " << kid2.getCNames() << endl;
    }
}

So in this function I am trying to create a program that'll store information about a student. When I am taking the student's courses name and then printing them out later, I only end up with only the first course name that was entered in. I am trying to print all of the courses name the user has entered. I believe the problem is within the accessor function.

Here are my results

How many courses are you currently taking? 2
What is your full name? Testing Name
Type in 1 for Freshman, 2 for Sophomore, 3 for Junior or 4 for Senior.
What year are you? 4
What is your current semester? Spring 2014
Please enter the name of all your courses.
Course #1 : C++
Course #2 : Adv C++

Here is your information:
Name : Testing Name
Semester : Spring 2014
Year : Senior
Course #1 : C++
Course #2 : C++
  • 3
    That's a lot of code. You should develop new functions in isolation, and your question will get more attention if you prepare a [minimal complete example](http://www.sscce.org). – Beta Mar 27 '14 at 00:06
  • As already mentioned for your other question: **Use** `std::vector<>` instead of plain arrays and raw pointers! You cannot return arrays, just pointers of them, and that's inherently unsafe and error prone! Also I don't get your `AmtClass` member in relation to your [other question](http://stackoverflow.com/questions/22674443/access-information-inside-constructors), what's the intended semantics behind that?? – πάντα ῥεῖ Mar 27 '14 at 00:07
  • I would have used vectors if I am supposed to use vectors. AmtClass is to gather information about the number of courses I need to create space for the array of strings – user3347541 Mar 27 '14 at 00:09
  • @user3347541 You are supposed to for this use case, believe me. Anything else would need some much more thorough overworking of the code you have! – πάντα ῥεῖ Mar 27 '14 at 00:12
  • In your setcourse function change array to a pointer. – aghoribaba Mar 27 '14 at 00:14

1 Answers1

1

You can use an index of an element of the array as a parameter of function getCohort

For example

string getCohort( int i ) const;

and the function has to return the element that corresponds to the given index or an empty string if the index is not valid.

The other way is to use standard class std::array<std::string, 4> and use it as the return value. For example

class Student
{
private:
    string name;
    int year;
    string semester;
    int AmtClass;
    string *CName;
    std::array<std::string, 4> cohort;

   //...

I removed keyword static because I do not understand why this data member is static.

And the function can be declared as

std::array<std::string, 4> getCohort() const;

If data member string *CName; corresponds to courses and you have such function

void setCourses(string courses[], int n);

then either CName has to point to dynamically allocated array or it should be an object of type std::vector<std::string>

For example std::vector CName; //...

void setCourses( const std::string courses[], int n )
{ 
    CName.assign( courses, courses + n );
}

Or

#include <algorithm>
//...

std::string *CName;
//...

void setCourses( const std::string courses[], int n )
{ 
    CName = new std::string[n];

    std::copy( courses, courses + n, CName );
}

In the last case do not forget to delete the pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335