| | |
OOP char [8] to char
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
[php]
#include <iostream>
#include <cstring>
using namespace std;
class date
{
public:
date(int = 1, int = 1, int = 1990); //constructor
void Printdate();
void setday(int);
void setmonth(int);
void setyear(int);
void setdate(int, int, int);
private:
int day, month, year;
};
date::date(int d, int m, int y)
{
setdate(d,m,y);
}
void date::setdate(int dd, int mm , int yy )
{
day = dd;
month = mm;
year = yy;
}
void date:
rintdate()
{
cout << day << "/" << month << "/" << year <<endl;
}
void date::setday(int dd)
{
day = dd;
}
void date::setmonth(int mm)
{
month = mm;
}
void date::setyear(int yy)
{
year = yy;
}
class Student
{
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Student:
tudent()
{
strcpy(student, "Acidburn");
}
void Student:
rintName()
{
cout << student<<endl;
}
void Student::setName(char N)
{
strcpy(student , " N ");
}
int main(void)
{
int day = 0, month = 0, year = 0;
char student[20]; // array to hold students name.
date date1;
Student St1;
cout << "Before call to set day" << endl;
date1.Printdate(); //shows default values from consturctor
cout << " ----------------------------------------------------\n";
cout << "Please enter your student details" <<endl;
cin >> student;
St1.setName("student");
return 0;
}
[/php]
Now the problem is with the student class ... In main I've declared an array of char student[20]; . I ask the user to enter the name in main. Anyway on passing the student information to the class to write to the private array data. I get an error
Can anyone point me in the direction for fixing it?
Thanks
#include <iostream>
#include <cstring>
using namespace std;
class date
{
public:
date(int = 1, int = 1, int = 1990); //constructor
void Printdate();
void setday(int);
void setmonth(int);
void setyear(int);
void setdate(int, int, int);
private:
int day, month, year;
};
date::date(int d, int m, int y)
{
setdate(d,m,y);
}
void date::setdate(int dd, int mm , int yy )
{
day = dd;
month = mm;
year = yy;
}
void date:
rintdate(){
cout << day << "/" << month << "/" << year <<endl;
}
void date::setday(int dd)
{
day = dd;
}
void date::setmonth(int mm)
{
month = mm;
}
void date::setyear(int yy)
{
year = yy;
}
class Student
{
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Student:
tudent(){
strcpy(student, "Acidburn");
}
void Student:
rintName(){
cout << student<<endl;
}
void Student::setName(char N)
{
strcpy(student , " N ");
}
int main(void)
{
int day = 0, month = 0, year = 0;
char student[20]; // array to hold students name.
date date1;
Student St1;
cout << "Before call to set day" << endl;
date1.Printdate(); //shows default values from consturctor
cout << " ----------------------------------------------------\n";
cout << "Please enter your student details" <<endl;
cin >> student;
St1.setName("student");
return 0;
}
[/php]
Now the problem is with the student class ... In main I've declared an array of char student[20]; . I ask the user to enter the name in main. Anyway on passing the student information to the class to write to the private array data. I get an error
•
•
•
•
D:\extra programming\temp\class.cpp(103) : error C2664: 'setName' : cannot convert parameter 1 from 'char [8]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
Thanks
I am not entirely sure at all, but I don't think you can use cin to populate an array of characters. I think you might want to use cin.getline()
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
Upon further inspection of your comment I decided to delete the St1.setName("student"); and replace it with a cout << student - Prints the value of the array.
It compiles and runs... But doesnt do what I want it to do. But it does populate the array
, Basically I would like to the user to enter a name from the UI and then that name gets passed to the function setName - which then adds / edits the array in the private data class.
It compiles and runs... But doesnt do what I want it to do. But it does populate the array
, Basically I would like to the user to enter a name from the UI and then that name gets passed to the function setName - which then adds / edits the array in the private data class. void Student::setName(char *N) { strcpy(student , N); }
cin >> student;
St1.setName(student);
St1.PrintName(); "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
lol wicked... strange I was going to try pointers but not to worry. Thanks for the post, I'm left with one more question.
[php]
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
[/php]
has you can see I'm trying to call a function from another class, but it doesnt want to work. I guess I can do it using inhertiance but I'm sure you can do it this way. Or at least something similar??
[php]
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
[/php]
has you can see I'm trying to call a function from another class, but it doesnt want to work. I guess I can do it using inhertiance but I'm sure you can do it this way. Or at least something similar??
Last edited by Acidburn; Mar 24th, 2005 at 12:25 pm. Reason: php error
•
•
Join Date: Jun 2003
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Acidburn
[php]
#include <iostream>
#include <cstring>
using namespace std;
class date
{
public:
date(int = 1, int = 1, int = 1990); //constructor
void Printdate();
void setday(int);
void setmonth(int);
void setyear(int);
void setdate(int, int, int);
private:
int day, month, year;
};
date::date(int d, int m, int y)
{
setdate(d,m,y);
}
void date::setdate(int dd, int mm , int yy )
{
day = dd;
month = mm;
year = yy;
}
void date:rintdate()
{
cout << day << "/" << month << "/" << year <<endl;
}
void date::setday(int dd)
{
day = dd;
}
void date::setmonth(int mm)
{
month = mm;
}
void date::setyear(int yy)
{
year = yy;
}
class Student
{
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Student:tudent()
{
strcpy(student, "Acidburn");
}
void Student:rintName()
{
cout << student<<endl;
}
void Student::setName(char N)
{
strcpy(student , " N ");
}
int main(void)
{
int day = 0, month = 0, year = 0;
char student[20]; // array to hold students name.
date date1;
Student St1;
cout << "Before call to set day" << endl;
date1.Printdate(); //shows default values from consturctor
cout << " ----------------------------------------------------\n";
cout << "Please enter your student details" <<endl;
cin >> student;
St1.setName("student");
return 0;
}
[/php]
Now the problem is with the student class ... In main I've declared an array of char student[20]; . I ask the user to enter the name in main. Anyway on passing the student information to the class to write to the private array data. I get an error
Can anyone point me in the direction for fixing it?
Thanks
Hello,
You have a couple of design problems. Let me point them out.
in your main you are passing a string literal(or constant) to the setName
function. You got the name from user and stored it in student. Now pass
the student variable to setName.
•
•
•
•
cout << "Please enter your student details" <<endl;
cin >> student;
St1.setName("student"); ** Remove the quotes- St1.setName(student);
•
•
•
•
void Student::setName(char N) // change to setName(char N[])
{
strcpy(student , " N "); // change to strcpy(student, N);
} // "N" is NOT char N, it is a char literal
Then in your class definition, you are passing a char in. Change it to char[].
•
•
•
•
class Student
{
public:
Student(); //constuctor for student class
void setName(char ); // void setName(char[]);
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Basically you were trying to use a char instead of a char array[], big difference for what you were trying to do. If you only wanted one char, then that would be fine.
Hope this helped.
•
•
Join Date: Jun 2003
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by cscgal
I am not entirely sure at all, but I don't think you can use cin to populate an array of characters. I think you might want to use cin.getline()
Actually you can use cin to get a char[], but you can go out of bounds on the char[] if the user typed more than was allocated. You get an out of bounds debug error. But you are correct that cin.getline is a better and safer way.
Cheers
Radioman28
•
•
Join Date: Jun 2003
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Acidburn
lol wicked... strange I was going to try pointers but not to worry. Thanks for the post, I'm left with one more question.
[php]
public:
Student(); //constuctor for student class
void setName(char );
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
[/php]
has you can see I'm trying to call a function from another class, but it doesnt want to work. I guess I can do it using inhertiance but I'm sure you can do it this way. Or at least something similar??
C++ Syntax (Toggle Plain Text)
date newDate;
In order to use it, you call it like this in main:
C++ Syntax (Toggle Plain Text)
St1.newDate.Printdate();
Cheers
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
ok in the Date class I've included the set and get functions : Heres the editied code:
[php]
#include <iostream>
#include <cstring>
using namespace std;
class date
{
public:
date(int = 1, int = 1, int = 1990); //constructor
void Printdate();
void setday(int);
void setmonth(int);
void setyear(int);
void setdate();
int getday();
int getmonth();
int getyear();
private:
int day, month, year;
};
date::date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
void date::setdate ()
{
int dd = 0, mm = 0, yy = 0;
cin >> dd >> mm >> yy;
day = dd;
month = mm;
year = yy;
}
void date:
rintdate()
{
cout << day << "/" << month << "/" << year <<endl;
}
void date::setday(int dd)
{
day = dd;
}
void date::setmonth(int mm)
{
month = mm;
}
void date::setyear(int yy)
{
year = yy;
}
int date::getday()
{
return day;
}
int date::getmonth()
{
return month;
}
int date::getyear()
{
return year;
}
class Student
{
public:
Student(); //constuctor for student class
void setName(void);
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Student:
tudent()
{
cout << "default name created : " ;
strcpy(student, "Acidburn");
}
void Student:
rintName()
{
cout << student<<endl;
}
//void Student::setName(char *N)
void Student::setName()
{
char name[20];
cin >> name;
strcpy(student , name );
}
int main(void)
{
date date1;
Student St1;
cout << "Before call to set day" << endl;
date1.Printdate(); //shows default values from consturctor
St1.PrintName();
cout << " ----------------------------------------------------\n";
cout << "Please enter your student details" <<endl;
St1.setName();
St1.PrintName();
cout << "Now enter there date of birth (D.o.B)" << endl;
date1.setdate();
date1.Printdate();
return 0;
}
[/php]
could someone explain why we need to use the set and get functions ? So far using the get function makes no sence to me... only adding to the size of the program??
[php]
#include <iostream>
#include <cstring>
using namespace std;
class date
{
public:
date(int = 1, int = 1, int = 1990); //constructor
void Printdate();
void setday(int);
void setmonth(int);
void setyear(int);
void setdate();
int getday();
int getmonth();
int getyear();
private:
int day, month, year;
};
date::date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
void date::setdate ()
{
int dd = 0, mm = 0, yy = 0;
cin >> dd >> mm >> yy;
day = dd;
month = mm;
year = yy;
}
void date:
rintdate(){
cout << day << "/" << month << "/" << year <<endl;
}
void date::setday(int dd)
{
day = dd;
}
void date::setmonth(int mm)
{
month = mm;
}
void date::setyear(int yy)
{
year = yy;
}
int date::getday()
{
return day;
}
int date::getmonth()
{
return month;
}
int date::getyear()
{
return year;
}
class Student
{
public:
Student(); //constuctor for student class
void setName(void);
void PrintName();
date Printdate();
private:
char student[20]; // array to store name.
};
Student:
tudent(){
cout << "default name created : " ;
strcpy(student, "Acidburn");
}
void Student:
rintName(){
cout << student<<endl;
}
//void Student::setName(char *N)
void Student::setName()
{
char name[20];
cin >> name;
strcpy(student , name );
}
int main(void)
{
date date1;
Student St1;
cout << "Before call to set day" << endl;
date1.Printdate(); //shows default values from consturctor
St1.PrintName();
cout << " ----------------------------------------------------\n";
cout << "Please enter your student details" <<endl;
St1.setName();
St1.PrintName();
cout << "Now enter there date of birth (D.o.B)" << endl;
date1.setdate();
date1.Printdate();
return 0;
}
[/php]
could someone explain why we need to use the set and get functions ? So far using the get function makes no sence to me... only adding to the size of the program??
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
ok - No edit!!
!! Anyway I think I've managed to understand my set and get functions. I've sorted that one for now. Back to the "As a and the other relationship". I've got this so far in main():
[php]
date newDate;
St1.Printdate();
[/php]
However I get an error
I tried what you said:
[php]
date newDate;
St1.newDate.Printdate();
[/php]
and I got this error:
anyone care to explain this? I'm out of ideas
!! Anyway I think I've managed to understand my set and get functions. I've sorted that one for now. Back to the "As a and the other relationship". I've got this so far in main():[php]
date newDate;
St1.Printdate();
[/php]
However I get an error
•
•
•
•
Linking...
class.obj : error LNK2001: unresolved external symbol "public: class date __thiscall Student:rintdate(void)" (?Printdate@Student@@QAE?AVdate@@XZ)
Debug/class.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
[php]
date newDate;
St1.newDate.Printdate();
[/php]
and I got this error:
•
•
•
•
Compiling...
class.cpp
D:\extra programming\temp\class.cpp(140) : error C2039: 'newDate' : is not a member of 'Student'
D:\extra programming\temp\class.cpp(82) : see declaration of 'Student'
D:\extra programming\temp\class.cpp(140) : error C2228: left of '.Printdate' must have class/struct/union type
Error executing cl.exe.
![]() |
Similar Threads
- append char to char* (C++)
- I/O question. using vc++ read char by char (C++)
- Problems casting a const char* to char* (C++)
Other Threads in the C++ Forum
- Previous Thread: Disease Spread
- Next Thread: Help required -Porting from IBM C++ to Microsoft C++
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







