Good day yet again ladies and gents. Today I'm having problems with strings and such. I've been tasked with creating fixed char arrays to contain first name, last name, and middle name. I've managed that, and have the calls in my main. I'm also tasked with using strlen (specifically) to find the length of the strings to create a dynamic array to hold all three names. This is where I'm running into problems. the first cpp file I'll post has the original functions for entering the names into the strings. The commented out portion was where I was getting the link individually (which works), but I am trying to write a separate function in another cpp file which keeps giving me linking errors. I'm not sure where they are coming from since I already used the extern keyword in the header. I'm sure it's possible to write a separate function in a separate cpp file, but am I making more trouble than it's worth? Here are the files:

Header

#ifndef NAMEINP_H
#define NAMEINP_H

const int maxin = 16;

extern char lastName[maxin];

extern char firstName[maxin]; 

extern char midName[maxin];

char getlast( char lastName[] , int maxin);

char getfirst( char firstName[] , int maxin);

char getmid( char midName[] , int maxin);

//void displayName (char * name);

#endif

Here are the functions

#include "nameinp.h"
#include <iostream>
#include <string.h>
using namespace std;

//Input last name
char getlast( char lastName[] , int maxin)
{
	cout << " Please enter your last name up to 15 characters " << endl;
	cin.getline(lastName, maxin, '\n' );
	//size_t lastLen;
	//lastLen = strlen(lastName);
	//cout << lastLen << endl;
	return 0;
}


//Input first name
char getfirst( char firstName[] , int maxin)
{
	cout << " Please enter your first name up to 15 characters " << endl;
	cin.getline(firstName, maxin, '\n' );
	//size_t firstLen;
	//firstLen = strlen(firstName);
	//cout << firstLen << endl;
	return 0;
}


//Input middle name
char getmid( char midName[] , int maxin)
{
	cout << " Please enter your middle name up to 15 characters " << endl;
	cin.getline(midName, maxin, '\n' );
	//size_t midLen;
	//midLen = strlen(midName);
	//cout << midLen << endl;
	return 0;
}

Here is the function I'm trying to write that's giving me the hassles

#include "nameinp.h"
#include <iostream>
#include <string.h>
using namespace std;

//Calculate the String Lengths

size_t totallength (size_t)
{
	size_t fullength = 0;

	size_t lastLen;
	lastLen = strlen(lastName);
	cout << lastLen << endl;
	
	size_t firstLen;
	firstLen = strlen(firstName);
	cout << firstLen << endl;

	size_t midLen;
	midLen = strlen(midName);
	cout << midLen << endl;

	fullength = lastLen + firstLen + midLen;

	return 0;
}

Okay, what am I screwing up? Thank you

Okay, not sure why that posted as text instead of code

Wow, no one after an hour? Is it that hard or did I just mess it up that badly?

>>Wow, no one after an hour? Is it that hard or did I just mess it up that badly
I was naping :)

function totallength()
The parameter must have a variable name, you can't just put a data type without a variable named. Variable names can be omotted ONLY in function prototypes.

line 26: why did you bother to calculate the total length then turn around and toss the answer into the bit bucket by returning 0 ?

You also need to add 1 for the new string's null terminating character.

You could reduce that entire function to just one line

size_t totallength()
{
   return strlen(firstname) + strlen(midname) + strlen(lastname) + 1;
}

Thank you yet again AD, now for the next question. I'm having trouble getting the syntax right for the "new" string. I'm also unsure how to copy the other strings into it. I'd post what I did, but I seem to have become frustrated and dumped it..lol. What I did have for the new string was this

char  *fullName = new char fullName [];

but that gave me a list of errors, so I tried this

char  *fullName = new char[];

Now this one seems to work, (compiles) but does it do what I want it to do?

I thought of just following it up with some lines such as this:

char strcopy (char fullName, const char lastName);
char strcat (char fullName, ' ');
char strcat (char fullName, const char firstName);
char strcat (char fullName, ' ');
char strcat (char fullName, const char midName);

First of all, I don't know if that works because I'm pretty ignorant about this to begin with, and second, the ' ' are supposed to be spaces but I'm sure that's not how they'll come out. I'm not sure how to do spaces with a strcat

Once I have that much working I have to figure if there is a way to automatically allocate the correct number of elements (using the strlength you helped me with earlier) and displaying the output of the above function showing the full name

>>char *fullName = new char[];
You didn't tell new operator how many characters to allocate. put a number (or int variable name) between those square brackets.

Here's how to copy the strings

strcpy( fullName, lastName);
strcat( fullName, " ");
strcat( fullName, firstName);
// etc etc like above

Okay, I understand telling it what to expect, but how do I pull that value from the size_t totallength function? do I do this?

char *fullname = new char[totallength]

So it now looks like this?

char  *fullName = new char[]; // with the box filled in

	strcopy (fullName, lastName);
	strcat (fullName, " ");
	strcat (fullName, firstName);
	strcat (fullName, " ");
	strcat (fullName, midName);

as that stands it gives me a load of errors about too many initializers and missing type specifiers....is that because the array box is empty?

>>but how do I pull that value from the size_t totallength function? do I do this?

Define a variable to receive the value returned by that function

size_t len = totallength();
char* fullName = new char[len];

>>char *fullName = new char[]; // with the box filled in

That is STILL wrong!

I was just reading the part about setting the variable when I opened up the post. Now for the new array, do all the strcpy and strcats go in {} braces?

>>do all the strcpy and strcats go in {} braces?

No, except they must appear inside a function such as main().

so something like

function header (or main)
{
new array
strcpy
strcat
strcat
etc
}

I think what you are trying to show is a normal function such as main(), and all executable code must be inside a function.

int main()
{
    char* array = new char[255];
    strcpy(array, "Hello");
    strcat(array," ");
    strcat(array, "World\n");
    cout << array;
}

Actually, I'm trying to write separate functions to do each task (because as you can see, I'm terrible at this) and then just call the separate functions in main().

The header I posted contains my prototypes for establishing fixed arrays for last, first, and middle names, and filling them.

From there, it was my intent to construct another function to calculate the length of a "new" dynamically allocated array that would contain the three name parts.

Obviously after that, I wanted to create a function to establish the new array and fill it, again calling it from main and then displaying the full name with spaces in between the parts.

My apologies if I'm frustrating you with this, I'm not good at this to begin with, and I am tired and frustrated from trying to figure it out myself for most of the day, I know I'm not sponging this up as well as I should either.

Since all that has to be within a function, I'm trying to figure out a header for it so I can call it from main rather than putting it in main.

in the code I posted just replace main() with any function name you wish. syntaxially it doesn't matter what function name you give it.

Did that, it compiles (so far) but it's giving me unresolved external symbol "char "lastName errors and three unresolved external linking errors.....I have extern in the header files so I don't know where it is. Here is what I have
HEADER

#ifndef NAMEINP_H
#define NAMEINP_H

const int maxin = 16;

extern char lastName[maxin];

extern char firstName[maxin]; 

extern char midName[maxin];

char getlast( char lastName[] , int maxin);

char getfirst( char firstName[] , int maxin);

char getmid( char midName[] , int maxin);

#endif

first cpp--fills the fixed arrays

#include "nameinp.h"
#include <iostream>
#include <string.h>
using namespace std;

//Input last name
char getlast( char lastName[] , int maxin)
{
cout << " Please enter your last name up to 15 characters " << endl;
cin.getline(lastName, maxin, '\n' );
return 0;
}


//Input first name
char getfirst( char firstName[] , int maxin)
{
cout << " Please enter your first name up to 15 characters " << endl;
cin.getline(firstName, maxin, '\n' );
return 0;
}


//Input middle name
char getmid( char midName[] , int maxin)
{
cout << " Please enter your middle name up to 15 characters " << endl;
cin.getline(midName, maxin, '\n' );
return 0;
}

the next cpp file for the dynamic string (note the name of the function--thats how tired I am)

#include "nameinp.h"
#include <iostream>
#include <string.h>
using namespace std;

//Calculate the String Lengths

size_t totallength()
{
   return strlen(firstName) + strlen(midName) + strlen(lastName) + 1;
}


// Dynamically allocate new string

void punchmeinthehead()
{
	size_t length = totallength();
	char  *fullName = new char[length];
	strcpy (fullName, lastName);
	strcat (fullName, " ");
	strcat (fullName, firstName);
	strcat (fullName, " ");
	strcat (fullName, midName);

	cout << fullName;
}

and then main -- so far

#include "nameinp.h"
#include <iostream>
#include <string.h> 
using namespace std;

void main()
{
	const int n = 16;
	char namein[n];
    
	char lastName = getlast(namein,n);

	char firstName = getfirst(namein,n);

	char midName = getmid(namein,n);
	
}

How do you guys do it when you get so frustrated with something but you feel like you just have to get it done..lol

you have to declare the same symbols in ONE *.cpp file without the extern keyword. Such as

#include "nameinp.h"
#include <iostream>
#include <string.h> 
using namespace std;


char lastName[maxin];
char firstName[maxin]; 
char midName[maxin];

void main()
{
	const int n = 16;
	char namein[n];
    
	char lastName = getlast(namein,n);

	char firstName = getfirst(namein,n);

	char midName = getmid(namein,n);
	
}

d&^$&$
you told me that last time and I didn't remember that part

Okay, got rid of that error, compiles, links okay, but I'm still doing something wrong. It's not printing the full name....I must have to fix the call somehow

Is your program actually calling the function punchmeinthehead() ? That function looks ok to me.

I think the problem is in main(). The global variables are never getting populated because in main() you pass variable namein to all three functions and never copying the results to the global variables. You don't need namein, just pass the name of the global variable.

#include "nameinp.h"
#include <iostream>
#include <string.h> 
using namespace std;


char lastName[maxin];
char firstName[maxin]; 
char midName[maxin];

void main()
{
    
    getlast(pastName, maxin);
    getfirst(firstName, maxin);
    getmid(midName,maxin);
	
}

so you think I should just put that function in main rather than call it from main? I ask because it's not doing anything...I run the code, put in last name, first, mid, and it's done. No full name output

Please re-read my previous post because I probably changed it since you last read it.

okay, I didn't put those declarations in the testnameinp cpp, I put them in the nameinp.cpp I don't know if that matters

okay, I see that, and that part works....It was written the other way because that was how I got my temperature output to work.

Now I just have to get it to output the full name, I'm not sure why it's not doing that part

It doesn't matter where you put those declaration, just look at the changes I made to main() function.

I think I got it

Okay, it actually lets you input all three name parts, and displays all three name parts. Now come the questions again..lol

How do I check for dynamic memory allocation success?

To clear the memory for subsequent entries, is it done like this ?
delete [] fullname
and does that go within the same function (punchmeinthehead)?

in main() you also have to call punchmeinthehead()

int main()
{
    getlast(pastName, maxin);
    getfirst(firstName, maxin);
    getmid(midName,maxin);
	
    punchmeinthehead();
}

yeah, I figured that part out, I was also an idiot and had part of it commented out because I was changing it slightly. All of that works now, and about the delete, I put it within the function, but after the cout, would that be correct?

I think not, because when I run it, I get an HEAP error and it quits

dagnabbit, my loop isn't working either, 1 is supposed to exit (I figured using a letter would be bad since I am entering names)

#include "nameinp.h"
#include <iostream>
#include <string.h> 
using namespace std;

void main()
{
    int i=0;

     while (i != 1)
     {
	
	getlast(lastName, maxin);
                getfirst(firstName, maxin);
                getmid(midName, maxin);
	punchmeinthehead();
	cout << "Enter another name or 1 to quit" << endl;
                cin >> i ;

      }	
}

This isn't working, and I'm not sure why. It runs once, and then goes infinite if you don't enter 1.

I'm also not sure how to check for dynamic memory allocation

When I try to use the delete keyword to reclaim memory, I get a HEAP Corrupt error. Currently the delete [] fullname line is withing the punchmeinthehead function right after the cout statement

I think I'm packin it in for the night. Many thanks to Ancient Dragon for his help. I'll check back in the AM to see if anyone had anything to say about my questions, I think I've had it for the night. Thanks AD

cout << "Enter another name or 1 to quit" << endl;
cin >> i ;

You are reading into an integer, so you can't expect it to be able to store a name in there. for a solution:

read into a string for the name, and if the string length is 1 and it's first character is '1' then exit the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.