Using local variables

I am still currently working on my program. But I am wondering how do I use the string of arrays inside the constructor with int parameter?
What I did was to intake the amount of classes a Student haves, and then use the amount to create string of arrays that'll store Student's course names.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

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

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

    void setCourses(string courses);
    void setName(string name);
    void setYear(int year);
    void setSemester(string semester);

    string getName();
    int getYear();
    string getSemester();
    int getAmtClass();

  //  ~Student();
};

string Student::cohort[4] = { "Freshman", "Sophomore", "Junior", "senior" };


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


Student::Student(int amount)
{
    AmtClass = amount;

    string *pName;
    pName = new string[AmtClass];
}

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

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

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

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;
}

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();
    cout << amount << endl;
    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 0 for Freshman, 1 for Sophomore, 2 for Junior or 3 for Senior.";
    cout << "\nWhat year are you? ";
    cin >> year;
    while(cin)
    {   if(year >= 0 && year < 4)
        {
            kid.setYear(year);
            break;
        }
        else
        {
            year = 0;
            cin >> year;
        }
    }

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

    cout << "\nPlease enter the name of all your courses." << endl;
    for(int i = 0; i < amount; i++)
    {
        cout << "Course #" << i+1 << " : ";
        getline(cin,pName[i]);
    //How do i set the course names into the constructor with int parameter     
    //here?
    }

}
When you say "string of arrays", do you actually mean "array of strings"?

What are you trying to do on lines 46-52?
Well on lines 46-52 I am trying to create an allocated array of strings so I can enter my course names into there.
I have to create a specific amount of space through the request of the user's and then enter the class names into there.
Last edited on
What do you think happens to pName on line 52? What do you think happens to *pName on line 52?
I'm not sure, I know i dynamically created a new array of strings, i just don't know how to access the array of strings inside of the constructor
You should pass it to the constructor, as a parameter.
Topic archived. No new replies allowed.