get and set char *

Dec 16, 2020 at 1:35pm
That's part of my HW as an introduction to "set and get" to classes.

We had to write a code which will create a class with different properties data types as INT,DOUBLE,BOOL,CHAR and CHAR array.

I am having trouble with the char array.

Any advice or reference will be much appreciated!

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

class datatype
{
    int x=0;
    double d=0;
    char c;
    char* t=new char[256];
    char* t1=new char[256];
    bool y;
    
public:
	
 void setXmanual(){ cout<<"Enter INT value "; cin>>x; }
 void setX(int value){ x=value; }
const int getX()const{ return x; }

 void setDmanual(){ cout<<"Enter DOUBLE value "; cin>>d; }
 void setD(double value){ d=value; }
const double getD()const{ return d; }

 void setCmanual(){ cout<<"Enter CHAR value "; cin>>c; }
 void setC(char value){ c=value; }
const char getC()const{ return c; }

 void setYmanual(){ cout<<"Enter BOOL value "; cin>>y; }
 void setY(bool value){ y=value; }
const bool getY()const{ return y; }

 void setTmanual(){ cout<<"Enter CHAR array value "; cin>>t1; strcpy(t, t1) ;}
void setT(char *value){ strcpy(t, value); }
const char getT()const{ return t; } //error



}; 


int main()
{
 srand(time(0));
 
   datatype x;
   
   x.setXmanual();
   cout<<"setXmanual  INT x="<<x.getX()<<endl;
   x.setX(777);
   cout<<"setXautomat INT x="<<x.getX()<<endl;
   cout<<endl;
   
   x.setDmanual();
   cout<<"setDmanual DOUBLE d="<<x.getD()<<endl;
   x.setD(7.777);
   cout<<"setXautomat DOUBLE x="<<x.getD()<<endl;
   cout<<endl;
   
   x.setCmanual();
   cout<<"setDmanual CHAR c="<<x.getC()<<endl;
   x.setC('a');
   cout<<"setXautomat CHAR c="<<x.getC()<<endl;
   cout<<endl;
   
   x.setCmanual();
   cout<<"setYmanual BOOL y="<<x.getC()<<endl;
   x.setC('a');
   cout<<"setXautomat BOOL y="<<x.getC()<<endl;
   cout<<endl;
   
   x.setTmanual();
   cout<<"setYmanual CHAR array T="<<x.getT()<<endl;
   x.setT("You are awesome");
   cout<<"setXautomat CHAR array T="<<x.getT()<<endl;
   
return 0;
}
Dec 16, 2020 at 1:40pm
> const char getT()const{ return t; } //error
Make it a const char *
const char *getT()const{ return t; }

> cin>>t1; strcpy(t, t1)
You may as well just do
cin>>t;
and be done with t1

Oh, and use std::string instead of all this manual memory allocation and unsafe C-style str... functions.

Dec 16, 2020 at 1:56pm
salem, many thanks!

please take a look at
void setTmanual(){ cout<<"Enter CHAR array value "; cin>>t1; strcpy(t, t1) ;}

it can't display a normal sentence, simply just the first word of it.( until the first space)

what am I doing wrong?
Dec 16, 2020 at 2:16pm
stream extraction (>>) only extracts to a white-space char (space, tab or newline). To obtain the whole line, use .getline() eg
 
cin.getline(t1, 256);


Note there is an issue mixing >> and getline(). >> doesn't consume the terminating white-space char, while getline() returns when a terminating white-space is found. If you mix >> and getline() then after a >> is used and before the .getline() use cin.ignore(1000, '\n') which will remove up to 1000 chars before the newline and remove the newline.
Dec 16, 2020 at 4:11pm
seeplus thanks for in depth explanation
Topic archived. No new replies allowed.