Passing string through a function...
Howdy. I have been pondering how I would go about taking, in this case, the name of a subject (for a GPA tracking program), and pass it through to an overloaded constructor. I figured pointers were the solution, but nonetheless I kept getting "int to char[11]" errors and the like. Here is a stubbed out version of what I've attempted so far:
#include <iostream.h>
class Course
{
public:
Course();
Course(int pname, float credits, unsigned short sem, unsigned short per);
~Course();
private:
char courseName[11];
float creditsAtt;
unsigned short Semesters;
unsigned short Period;
};
Course::Course()
{
}
Course::Course(int pname, float credits, unsigned short sem, unsigned short per)
{
courseName = * pname; // so this should give the string,
// and courseName is a string
// identical to the local string
// in int main(). Where have I
// fouled things up?
creditsAtt = credits;
Semesters = sem;
Period = per;
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
int main()
{
char name[11];
float credits = 0;
unsigned short sem = 0;
unsigned short per = 0;
// ( I deleted a lot of explanation, irrelevant stuff here )
//////////////////////////////////////////////////
//////////////////////////////////////////////////
Course * All = new Course[9];
Course * pCourse;
int * pname = 0; // OK, setting a pointer for each string
for(short p = 0; p < 9; p++)
{
cout << "\nPlease enter the name of period number " << p << ", 10 characters max please:\t";
cin.get(name, 10);
pname = &name; // OK. So this sets the pointer to name's
// address, right?
// ( deleted more irrelevant stuff, questions for info )
pCourse = new Course(pname, credits, sem, per); // OK, so
// this should set name's address at pname into the parameter?
All[p] = * pCourse;
delete pCourse;
delete pname; // do I have to do this in this case?
}
delete [] All;
return 0;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
END OF CODE
Hey, thanks a billion. Just wondered if anyone could give this a quick scan and detect my obvious error. :-P