Help with displaying an Object Char Array

Pages: 12
closed account (NU9GNwbp)
Hi there, my original lab assignment was to work with strings and pointers. Our assignment was to ask for a first name, middle name and last name. Create and enter them into a dynamic array, then display the array. Well I accomplished this, but wanted to take it one step further.

With the inserted code, I have created a struct and implemented my code to create an array to store multiple names- up to 100 as defined in main. So I can input the name, count the chars- but for some reason cannot pass the fullName to the displayName function for output.

What am I doing wrong or missing?

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
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
using namespace std;

struct Names
{
public:
    void GetNames();
    void displayNames();
   

private:
    
   int userInput;
   char fName;
   char mName;
   char lName;
   char fullName;
   
};


void Names::GetNames()
{
   userInput = 0;
   
   char *fName = new char[userInput];
   char *mName = new char[userInput];
   char *lName = new char[userInput];
   
   cout << "Enter First Name ";
   cin >> fName;

   cout << "Enter Middle Name ";
   cin >> mName;

   cout << "Enter Last Name ";
   cin >> lName;
      
   int fnameLength = strlen(fName);
   int mnameLength = strlen(mName);
   int lnameLength = strlen(lName);   
   
   int totalLength = fnameLength + mnameLength + lnameLength + 3;
   cout << totalLength <<"\n"<<endl;
   
   char *fullName = new char[totalLength];

   strcpy(fullName, fName); //copies fName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, mName); //copies mName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, lName); //copies lName into fullName Array
   
    
      
}

void Names::displayNames()
{

   cout << fullName <<endl;

}


int main()
{
    int inputNames = 0;
    Names objects[100]; //up to 100 can be stored

    cout << "How many names would you like to store? ";
    cin >> inputNames;

    for(int i = 0; i < inputNames; ++i)
        objects[i].GetNames();

    cout << "\n\nHere are the names you entered\n";
    
   for(int j = 0; j < inputNames; ++j)
    {
        cout << "Name. " << j << " " <<endl;
      objects[j].displayNames();
        
    }

   system("pause");

   return 0;
}


25
26
27
28
29
30
31
32
33
34
35
36
37
38
   userInput = 0;
   
   char *fName = new char[userInput];
   char *mName = new char[userInput];
   char *lName = new char[userInput];
   
   cout << "Enter First Name ";
   cin >> fName;

   cout << "Enter Middle Name ";
   cin >> mName;

   cout << "Enter Last Name ";
   cin >> lName;
PLEASE tell me this is a joke...do you expect the user to have names with lengths of -1? You don't even have room for the null character...
Last edited on
Inside GetNames you don't use any of the member variables so all the strings created in that function are lost when the function returns.
closed account (NU9GNwbp)
Can you explain a little further please. Thank you.

L B why even bother typing if it seems like a joke- you just wasted your time thanks.
I bothered typing to point out the serious flaw in your code that could cause a segmentation fault...is pointing out mkistakes not helpful? I though it was a joke because you wrote 0...
closed account (NU9GNwbp)
Then how about offering some help instead of wasting my time and yours.
closed account (NU9GNwbp)
Also the null char is accounted for in: int totalLength = fnameLength + mnameLength + lnameLength + 3;
Clearly I dont understand, can you explain what you mean by serious flaw. Obviously I am a beginner so the only way to learn is to dangle my code out there to be ripped apart. However I will get better if I know what I am doing wrong- ie someone explains it.
What L B tries to say is that you set userInput to zero and then you create 3 strings of length userInput. So you have 3 zero sized strings which don't even have room for the null char.
closed account (NU9GNwbp)
This is actually correct. The userInput is set to 0 to initialize the dynamic array, which is populated by the three cin requests. This part works fine, its passing that information on where I am having problems.
Dynamic arrays do not automatically resize! You have to do that yourself, and unfortunately you cannot know how many characters the user will enter.

I suggest using std::string instead.
closed account (NU9GNwbp)
Thanks that worked but probably not the way you were suggesting. I simply added std::string str; to my private variables, then pointed str += fullName array.


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
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
using namespace std;

struct Names
{
public:
    
	void GetNames();
    void displayNames();
   

private:
   
   std::string str;  
   char fName;
   char mName;
   char lName;
   char fullName;
      
};


void Names::GetNames()
{
	 
   int userInput = 0;
   
   char *fName = new char[userInput];
   char *mName = new char[userInput];
   char *lName = new char[userInput];
   
   cout << "Enter First Name ";
   cin >> fName;

   cout << "Enter Middle Name ";
   cin >> mName;

   cout << "Enter Last Name ";
   cin >> lName;
      
   int fnameLength = strlen(fName);
   int mnameLength = strlen(mName);
   int lnameLength = strlen(lName);   
   
   int totalLength = fnameLength + mnameLength + lnameLength + 3;
   cout << totalLength <<"\n"<<endl;
   
   char *fullName = new char[totalLength];

   strcpy(fullName, fName); //copies fName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, mName); //copies mName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, lName); //copies lName into fullName Array
   
   str += fullName;
       
}

void Names::displayNames()
{

   cout << str <<endl;

}


int main()
{
    int inputNames = 0;
    Names objects[100]; //up to 100 can be stored

    cout << "How many names would you like to store? ";
    cin >> inputNames;

	for(int i = 0; i < inputNames; ++i)
        objects[i].GetNames();

    cout << "\n\nHere are the names you entered\n";
    
   for(int j = 0; j < inputNames; ++j)
    {
      cout << "Name. " << j << " " <<endl;
      objects[j].displayNames();
        
    }

   

   system("pause");

   return 0;
}
Last edited on
It works, it's not correct. Try this:

1
2
3
4
5
6
7
8
char * s1 = new char[0];
char * s2 = new char[0];
s2[0] = 'b';
s2[1] = '\0';

cout << "Enter s1: ";
cin >> s1;
cout << "s1: " << s1 << "\n" << "s2: " << s2 << "\n";

and enter a big string. For example:


Enter s1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
s1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
s2: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Since you didn't give s1 the proper size, line 7 wrote over s2. That's a really, really bad thing to happen, and one of the most difficult bugs to find. So, either give your strings a proper size or use std::string.
closed account (NU9GNwbp)
I understand, thank you for this, I appreciate you taking the time to show me too. I will implement the new changes.
closed account (NU9GNwbp)
Ok here are the changes- you were absolutely right too. I added a fixed size of 50- making sure to cover any part of the name, assuming it is equal to or under 50 chars. I also forgot to delete the new char array when I was done. Thanks bbgst!

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
using namespace std;

struct Names
{
public:
    
	void GetNames();
    void displayNames();
   

private:
   
   std::string str;  
   char fName;
   char mName;
   char lName;
   char fullName;
   int charLength;

};


void Names::GetNames()
{
	 
   int userInput = 50;
   
   char *fName = new char[userInput];
   char *mName = new char[userInput];
   char *lName = new char[userInput];
   
   cout << "Enter First Name ";
   cin >> fName;

   cout << "Enter Middle Name ";
   cin >> mName;

   cout << "Enter Last Name ";
   cin >> lName;
      
   int fnameLength = strlen(fName);
   int mnameLength = strlen(mName);
   int lnameLength = strlen(lName);   
   
   int totalLength = fnameLength + mnameLength + lnameLength + 3;
   //cout << totalLength <<"\n"<<endl;
   cout << "\n";
   
   char *fullName = new char[totalLength];

   strcpy(fullName, fName); //copies fName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, mName); //copies mName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, lName); //copies lName into fullName Array
   
   str += fullName;
   charLength = totalLength;
  
   //check memory allocation
	if (charLength <= 0)
	{
		cout << "Memory did not allocate correctly" <<endl;
		system("pause");
		exit(1);
		
	}


   delete [] fullName;
}

void Names::displayNames()
{

   cout << str <<endl;
   cout << "Characters: "<< charLength <<endl;
  	   

}


int main()
{
    int inputNames = 0;
    Names objects[100]; //up to 100 can be stored

    cout << "How many names would you like to store? ";
    cin >> inputNames;

	for(int i = 0; i < inputNames; ++i)
        objects[i].GetNames();

    cout << "\nHere are the names you entered:\n";
    
   for(int j = 0; j < inputNames; ++j)
    {
      cout << "\nName. " << j + 1 << " :  "; objects[j].displayNames();
      cout << "\n" << endl;
        
    }
      
   system("pause");

   return 0;
}
Lines 16 to 19: these are signle characters, and on top of that you never even use them in the GetNames function.

Line 59: If you're going to do this at all, why not have std::strings for first, middle, and last and input to them directly? There is no need to do all the above code if you're going to use std::string...

Line 72: What about fName, mName, and lName?

Line 98: Just a not here, you didn't have to change to j instead of i. The i from line 93 does not exist at this point.

There are other less serious problems, but I didn't want to overwhelm things by having too many things change at once.
closed account (NU9GNwbp)
Thanks L B, for this assignment we had to use chars. We were asked to create a dynamic array, take in chars for first, middle and last names, store those in one array, count them, then display them together as one name. I took it one step further by wanting to do this multiple times( or as many as the user wants).

Thanks for the suggestions- I have ammended the code: Let me know if you see anything else.

I created pointers in the private area for the member variables for fName, mName and lName. Is this correct usage?

Also I deleted the arrays for these as well.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

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

struct Names
{
public:
    
	void GetNames();
    void displayNames();
   

private:
   
	char *fName;
	char *mName;
	char *lName;
	char *fullName;
	int charLength;
    std::string str;  
   
};


void Names::GetNames()
{
	 
   int userInput = 50;
   
   fName = new char[userInput];
   mName = new char[userInput];
   lName = new char[userInput];
   
   cout << "Enter First Name ";
   cin >> fName;

   cout << "Enter Middle Name ";
   cin >> mName;

   cout << "Enter Last Name ";
   cin >> lName;
      
   
   int fnameLength = strlen(fName);
   int mnameLength = strlen(mName);
   int lnameLength = strlen(lName);   
  
   int totalLength = fnameLength + mnameLength + lnameLength + 3;
   
   //cout << totalLength <<"\n"<<endl;
   cout << "\n";
       
   fullName = new char[totalLength];


   strcpy(fullName, fName); //copies fName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, mName); //copies mName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, lName); //copies lName into fullName Array
   
   str += fullName;
   charLength = totalLength;
  

   //check memory allocation
	if (charLength == 0)
	{
		cout << "Memory did not allocate correctly" <<endl;
		system("pause");
		exit(1);
		
	}


   delete [] fullName;
   delete [] fName;
   delete [] mName;
   delete [] lName;
}

void Names::displayNames()
{

   cout << str <<endl;
   cout << "Characters: "<< charLength <<endl;
  	   

}


int main()
{
    int inputNames = 0;
    Names objects[100]; //up to 100 can be stored

    cout << "How many names would you like to store? ";
    cin >> inputNames;

	for(int i = 0; i < inputNames; ++i)
        objects[i].GetNames();

    cout << "\nHere are the names you entered:\n";
    
   for(int j = 0; j < inputNames; ++j)
    {
      cout << "\nName. " << j + 1 << " :  "; objects[j].displayNames();
      cout << "\n" << endl;
        
    }
      
   system("pause");

   return 0;
}
My suggestions were somewhat independent from each other; you seem to have combined incompatible suggestions. Also, I was unsure if you had wanted to learn the hard way (character arrays) or the easy way (std::string) to do this, because you said you had already completed and turned in your assignment. I suggest you pick one or the other but not both.

Lines 77-80: Now that you are correctly storing the arrdesses of the names in your class, you should have this code instead at the beginning of the function and only delete them if they already exist. This way, you avoid memory leaks and thus can call GetNames as many times as you want. Currently, though, you delete them making them useless, leaving str the only way to get the name.

Now, to the stuff I couldn't get to before.

On line 96 you make 100 names, but then you actually ask the user how many they want on line 98. If you switched the order, you could dynamically allocate the array with as many elements as they wanted, be it 1 or 100000.

On line 108, you have to add 1 to j, but you could easily start the loop at j = 1 and have the condition be j <= inputNames, thus you wouldn't have to add one to j. Just a minor issue of preference in loop style; I try to make my code 'lazy'.

If there's anything else I'll point it out next time. ;)
closed account (NU9GNwbp)
Actually I dont have to hand it in until Sunday. I would very much appreciate learning the hard way(character arrays) and not combining the two as I have done. How would you accomplish the task of inputing chars...etc then passing them for display? The later is where I think I am really struggling..

Thanks for your help. I will adjust the code based on your suggestions.

Also I am really learning alot and appreciate your time.
You're actually there now, just get rid of any traces of the str variable or std::string and make the changes I mentioned, and you should be good to go. ;)
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
void Names::GetNames()
{
	string str ; 

         userInput = 0;
   
   
   cout << "Enter First Name ";
	cin>>str; 
	userInput  =  str.length(); 
	char *fName = new char[userInput + 1];
	strcpy( fName  ,  str.c_str() ) ; 

	str.empty() ; 

   cout << "Enter Middle Name ";
   cin>>str; 
	userInput  =  str.length(); 
	char *mName = new char[userInput + 1];
	strcpy( mName  ,  str.c_str() ) ; 
	str.empty();


   cout << "Enter Last Name ";
   cin>>str; 
	userInput  =  str.length(); 
	char *lName= new char[userInput + 1];
	strcpy( lName  ,  str.c_str() ) ; 
	str.empty();

      
   int fnameLength = strlen(fName);
   int mnameLength = strlen(mName);
   int lnameLength = strlen(lName);   
   
   int totalLength = fnameLength + mnameLength + lnameLength -2  ;
   cout << totalLength <<"\n"<<endl;
   
   char *fullName = new char[totalLength];

   strcpy(fullName, fName); //copies fName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, mName); //copies mName into fullName Array
   strcat(fullName, " "); //adds a space
   strcat(fullName, lName); //copies lName into fullName Array
   
    
      
}
Last edited on
Pages: 12