OOP char [8] to char

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

OOP char [8] to char

 
0
  #1
Mar 24th, 2005
[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

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.
Can anyone point me in the direction for fixing it?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,036
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: OOP char [8] to char

 
0
  #2
Mar 24th, 2005
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()
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: OOP char [8] to char

 
0
  #3
Mar 24th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: OOP char [8] to char

 
0
  #4
Mar 24th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: OOP char [8] to char

 
0
  #5
Mar 24th, 2005
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??
Last edited by Acidburn; Mar 24th, 2005 at 12:25 pm. Reason: php error
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 8
Reputation: radioman28 is an unknown quantity at this point 
Solved Threads: 0
radioman28 radioman28 is offline Offline
Newbie Poster

Re: OOP char [8] to char

 
0
  #6
Mar 24th, 2005
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);
Then in your setName function, you were passing a string literal ("student"), that was probably converted to a char{] by the compiler, and was trying to assign it to a single char (char N) in the function definition. strcpy takes char[] (char arrays) if I'm not mistaken, there would be no reason to use it for a char, just copy one char to another.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 8
Reputation: radioman28 is an unknown quantity at this point 
Solved Threads: 0
radioman28 radioman28 is offline Offline
Newbie Poster

Re: OOP char [8] to char

 
0
  #7
Mar 24th, 2005
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()
Hi Dani,

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 8
Reputation: radioman28 is an unknown quantity at this point 
Solved Threads: 0
radioman28 radioman28 is offline Offline
Newbie Poster

Its called composition "has a"

 
0
  #8
Mar 24th, 2005
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??
What you are trying to do is composition. Copmposition is a "has a" relationship. Class Student "has an" object of class date. But you are declaring it wrong. Try this:
  1. date newDate;
That creates a date object called newDate which is a member of class Student.

In order to use it, you call it like this in main:
  1. St1.newDate.Printdate();
Now this is NOT a good way of using classes. date newDate is a public member of class Student (you have it listed as date Printdate()), with public accesible functions. It would be better that newDate become a private field and write GET/SET's for it. Although, it works as I showed.

Cheers
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: OOP char [8] to char

 
0
  #9
Mar 24th, 2005
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??
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: OOP char [8] to char

 
0
  #10
Mar 24th, 2005
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

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.
I tried what you said:
[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.
anyone care to explain this? I'm out of ideas
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC