| | |
Strings this time
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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
Here are the functions
Here is the function I'm trying to write that's giving me the hassles
Okay, what am I screwing up? Thank you
Header
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; Mar 7th, 2008 at 8:46 pm. Reason: correct code tags
>>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
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
C++ Syntax (Toggle Plain Text)
size_t totallength() { return strlen(firstname) + strlen(midname) + strlen(lastname) + 1; }
Last edited by Ancient Dragon; Mar 7th, 2008 at 8:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 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
but that gave me a list of errors, so I tried this
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:
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
C++ Syntax (Toggle Plain Text)
char *fullName = new char fullName [];
but that gave me a list of errors, so I tried this
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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);
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
Last edited by henpecked1; Mar 7th, 2008 at 11:25 pm.
>>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
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
C++ Syntax (Toggle Plain Text)
strcpy( fullName, lastName); strcat( fullName, " "); strcat( fullName, firstName); // etc etc like above
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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?
So it now looks like this?
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?
C++ Syntax (Toggle Plain Text)
char *fullname = new char[totallength]
So it now looks like this?
C++ Syntax (Toggle Plain Text)
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?
Last edited by henpecked1; Mar 7th, 2008 at 11:38 pm.
>>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
>>char *fullName = new char[]; // with the box filled in
That is STILL wrong!
Define a variable to receive the value returned by that function
C++ Syntax (Toggle Plain Text)
size_t len = totallength(); char* fullName = new char[len];
>>char *fullName = new char[]; // with the box filled in
That is STILL wrong!
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Time Difference with precision (C)
- Strings (Java)
- Beginner with C++ and goofy run time problem with DBL Linked List (C++)
- Can't compare strings after PostBack (ASP.NET)
- Error comparing strings from arrays (PHP)
- parsing date strings mm/dd/yy?? (C++)
- !NEW!:::::!!Time Display Program!! (C++)
- Time display program (C++)
Other Threads in the C++ Forum
- Previous Thread: Multithreading C++
- Next Thread: Array's Size Problem
Views: 1322 | Replies: 30
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






