Char type hit me

Jul 8, 2010 at 7:32pm
Hi everyone,

I am learning C++, but I have very much dificult with type char.
I write the simple program to learn, and I have use the type string because I not know how to use char[256].
Can anyone help me? It is necessary rewrite the program using char instead of string.

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
75
76
77
78
79
80
81
82
// Friends - Demonstrate the use of friend class.
//           Functions also can be friend
// 

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class People
{
      friend class Age;
      public:
             void setName(string name)
             {
                  sName = name;
             }
             void setAge(int age)
             {
                  nAge = age;
             }
             string getName(void)
             {
                  return sName;
             }
             int getAge(void)
             {
                  return nAge;
             }
      private:
              string sName;
              int nAge;
};

class Age
{
      public:
             void changeAge(People* pS)
             {
                  int newAge;
                  cout << "Type the new age: ";
                  cin >> newAge;
                  pS->nAge = newAge;
             }
};

int main(int NumberofArgs, char* pszArgs[])
{
    cout << "Program to demonstrate the use\n"
         << "of the friend classes.\n\n";
         
    string name;
    cout << "Name = ";
    cin >> name;
    
    int age;
    cout << "Age = ";
    cin >> age;
    
    cout << "Call the class People with the values typed.\n";
    People p;
    p.setName(name);
    p.setAge(age);
    
    cout << "Verify the values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
         
    cout << "Use a friend class Age to modify the value.\n";
    Age a;
    a.changeAge(&p);
    
    cout << "Verify again he values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}


Jul 8, 2010 at 7:57pm
Replace string in main() with char name[256]. Replace strings in People class with char*. That should work. It would probably be better to declare sName as char[256] and use strcpy in setName() though. That way any changes made to name won't affect sName.
Jul 9, 2010 at 11:31am
Hello,

I aplly the changes recommended for you. It works, but still have errors.
The code is list below, and my doubt is, why the program only accept a single argument in Name field? If I type "Yuri" it works fine, but instead off, I type "Yuri Rodrigues", it no longer works!

Helpe me please, and thank you for all.


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
75
76
77
78
79
80
81
82
83
// Friends - Demonstrate the use of friend class.
//           Functions also can be friend
// 

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;

class People
{
      friend class Age;
      public:
             void setName(char* name)
             {
                  strcpy(sName,name);
             }
             void setAge(int age)
             {
                  nAge = age;
             }
             string getName(void)
             {
                  return sName;
             }
             int getAge(void)
             {
                  return nAge;
             }
      private:
              char sName[256];
              int nAge;
};

class Age
{
      public:
             void changeAge(People* pS)
             {
                  int newAge;
                  cout << "Type the new age: ";
                  cin >> newAge;
                  pS->nAge = newAge;
             }
};

int main(int NumberofArgs, char* pszArgs[])
{
    cout << "Program to demonstrate the use\n"
         << "of the friend classes.\n\n";
         
    char name[256];
    cout << "Name = ";
    cin >> name;
    
    int age;
    cout << "Age = ";
    cin >> age;
    
    cout << "Call the class People with the values typed.\n";
    People p;
    p.setName(name);
    p.setAge(age);
    
    cout << "Verify the values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
         
    cout << "Use a friend class Age to modify the value.\n";
    Age a;
    a.changeAge(&p);
    
    cout << "Verify again he values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}

Jul 9, 2010 at 11:35am
Try using getline (cin, variable) instead of cin >> (variable).
Last edited on Jul 9, 2010 at 11:38am
Jul 9, 2010 at 11:42am
I try it! But the compiler (DevC++) accused an error for me (no matching function for call to 'getline(std::istream&, char[256]').

This command, getline, should not be used with type string?
Jul 9, 2010 at 1:34pm
It should be cin.getline(name, 256);
Jul 9, 2010 at 2:04pm
This command, getline, should not be used with type string?

No. If you had looked at the function definitions properly you would have seen that there is no overload available that takes a std::istream& and a maximum amount of characters to extract:

1
2
istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );


It should be getline(std::cin, name);
Last edited on Jul 9, 2010 at 2:06pm
Jul 9, 2010 at 3:10pm
My friend,

It works very well!! Thank you!
The final code is list below.


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
75
76
77
78
79
80
81
82
83
// Friends - Demonstrate the use of friend class.
//           Functions also can be friend
// 

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;

class People
{
      friend class Age;
      public:
             void setName(char* name)
             {
                  strcpy(sName,name);
             }
             void setAge(int age)
             {
                  nAge = age;
             }
             string getName(void)
             {
                  return sName;
             }
             int getAge(void)
             {
                  return nAge;
             }
      private:
              char sName[256];
              int nAge;
};

class Age
{
      public:
             void changeAge(People* pS)
             {
                  int newAge;
                  cout << "Type the new age: ";
                  cin >> newAge;
                  pS->nAge = newAge;
             }
};

int main(int NumberofArgs, char* pszArgs[])
{
    cout << "Program to demonstrate the use\n"
         << "of the friend classes.\n\n";
         
    char name[256];
    cout << "Name = ";
    cin.getline(name, 256);
    
    int age;
    cout << "Age = ";
    cin >> age;
    
    cout << "Call the class People with the values typed.\n";
    People p;
    p.setName(name);
    p.setAge(age);
    
    cout << "Verify the values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
         
    cout << "Use a friend class Age to modify the value.\n";
    Age a;
    a.changeAge(&p);
    
    cout << "Verify again he values in the object People.\n"
         << "Name - " << p.getName() << "\n"
         << "Age - " << p.getAge() << "\n";
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}


Topic archived. No new replies allowed.