using char array, new and delete to get name and surname from external function

Hello. my attempt is to write a program that requests and displays information as shown below:
What is your first name? Carl Frisk
What is your last name? Ollo Bollow
Name: Carl Frisk, Ollo Bollow

and I wanted to do that by using a function (other than the main()) to get the name in a char array, and then call the same function again to get the surname. My goal is also to use the new and delete operator to free the memory used by the 'name-grabbing' function.

I have come out with the following code which seems to work, but being a total beginner in this I would like to be sure I am not messing up with the proper use of new and delete or picking up any bad habit with the way I am write the code.
So basically I am asking if someone could review my code and point out any oddity.
Here is the code:

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

#include <iostream>

char* getname();


int main () {
	using namespace std;
	cout << "What is your first name? ";
	char* name = getname();
  
	cout << "What is your last name? " ;
	char* surname = getname();
	delete[] surname;
		
	cout << "Name: " << name << ", " << surname <<"\n" ;
	}
char* getname() {
	char temp[50];
	char* name = new char[strlen(temp + 1)];
	std::cin.get(temp, 50).get();
	strcpy(name, temp);
	
	return name;

}



ps: how can I set the length of the newly created array called name (inside the getname() function) to fit the input coming from the user?
In the code above I have used
std::cin.get(temp, 50).get();
but I would like not to use 50, but rather let the array adjust its length according to the length of the input.
Last edited on
You're along the right lines, but there are problems.

In your getname function, you declare an array temp, without initialising it. Then, before you put any data into that array, you attempt to find the length of a string stored in that array - but you haven't stored any string there! The number you get back from strlen will be the number of non-zero values it finds in that array before it reaches the first zero - but this number has no meaning.

Also, in main, you remember to delete surname, but you've forgotten to delete name.

(There's nothing wrong with doing this as an exercise in learning about arrays, dynamic memory, and functions. In "real" code, you wouldn't do it this way, though - you would be much better off using the C++ std::string class, which manages its own memory).



Last edited on
Thanks MikeyBoy,
true, I see I was asking strlen to read the size of temp before temp could get any data. I suppose swapping the lines would fix this logical problem.?
1
2
3
4
    char temp[50];
    std::cin.get(temp, 50).get();
    char* name = new char[strlen(temp + 1)];
    strcpy(name, temp);

---
thanks for pointing out also the missing
delete[] name;
:)

After the fix this is how it looks like:
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
#include <iostream>

char* getname();


int main () {
    using namespace std;
    cout << "What is your first name? ";
    char* name = getname();
    delete[] name;
    cout << "What is your last name? " ;
    char* surname = getname();
    
    delete[] surname;
    
    cout << "Name: " << name << ", " << surname <<"\n" ;
}
char* getname() {
    char temp[50];
    std::cin.get(temp, 50).get();
    char* name = new char[strlen(temp + 1)];
    strcpy(name, temp);
    
    return name;
    
}


And yes, I thought about strings and its ability to manage its own length. As you said, in this case I forced myself to stick to array just to get it right..
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
#include <string>
#include <cstring>

char* get_name()
{
    try
    {
        // get the name
        std::string name ;
        std::getline( std::cin, name ) ; // note: may throw std::bad_alloc
        
        // allocate memory, copy the name into it and return the pointer note: may throw
        char* cstr_name = new char[ name.size() + 1 ] ; // +1 for he null char at the end
        std::strcpy( cstr_name, name.c_str() ) ; 
        return cstr_name ;
    }
    
    catch( const std::bad_alloc& ) { return nullptr ; } // allocation failure
}

int main()
{
    std::cout << "name? " ;
    const char* name = get_name() ;
    if( name != nullptr ) std::cout << name << '\n' ;
    delete[] name ;
}
Topic archived. No new replies allowed.