I am running into a couple of problems with the code below. I need to dynamically create an array of strings which contain the name of classes. Below is how I am tring to do it but it loops through i=0 and assigns the first input to className[1] instead of className[0].
for(i=0; i<classes;i++)
className = new string[classes];
for(i=0; i<classes;i++) //skips over i=[0] and
getline(cin, className[i]); //saves first input in i=[1]
// cant figure out why.
My second problem is getting the array of strings from the main function where all the cin cout's have to be done and into the class Students. Below is how I attempted to do it.
student1.setClasses(className); // this is how i am calling it in main
void Student::setClasses(string *className)
{
string *classNames; // this is actually in class but wanted to show how i initialized it
int i;
for(i=0; i<numClasses;i++)
{
classNames = new string[numClasses];
classNames[i] = className[i];
cout<<"\n\n"<<className[i]; //
cout<<"\n"<<classNames[i]; //prints out same as above so appears to
//have been copied correctly
}
for(i=0; i<numClasses;i++) //when I do cout here nothing prints except
cout<< classNames[i]; //for the final loop it prints the last string
//name
}
The entire code is below. I really appreciate any help.
Thanks,
Mike
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
private:
string name;
int numClasses;
string *classNames;
};
int main()
{
Student student1;
string studentName;
string *className;
int classes;
int i = 0;
cout <<"Please enter students first and last name";
getline(cin,studentName);
student1.setName(studentName);
cout <<"\nPlease enter the number of classes the student is in";
cin >> classes;
student1.setNumClasses(classes);
for(i=0; i<classes;i++)
className = new string[classes];
for(i=0; i<classes;i++) //skips over i=[0] and saves first input in i=[1]
getline(cin, className[i]);
void Student::setClasses(string *className)
{
int i;
for(i=0; i<numClasses;i++)
{
classNames = new string[numClasses];
classNames[i] = className[i];
cout<<"\n\n"<<className[i];
cout<<"\n"<<classNames[i]; //cout here displays full list of classes
}
for(i=0; i<numClasses;i++)
cout<< classNames[i]; //cout here displays nothing but final class in classNames[]