Strings this time

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Strings this time

 
0
  #1
Mar 7th, 2008
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

  1. #ifndef NAMEINP_H
  2. #define NAMEINP_H
  3.  
  4. const int maxin = 16;
  5.  
  6. extern char lastName[maxin];
  7.  
  8. extern char firstName[maxin];
  9.  
  10. extern char midName[maxin];
  11.  
  12. char getlast( char lastName[] , int maxin);
  13.  
  14. char getfirst( char firstName[] , int maxin);
  15.  
  16. char getmid( char midName[] , int maxin);
  17.  
  18. //void displayName (char * name);
  19.  
  20. #endif

Here are the functions

  1. #include "nameinp.h"
  2. #include <iostream>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. //Input last name
  7. char getlast( char lastName[] , int maxin)
  8. {
  9. cout << " Please enter your last name up to 15 characters " << endl;
  10. cin.getline(lastName, maxin, '\n' );
  11. //size_t lastLen;
  12. //lastLen = strlen(lastName);
  13. //cout << lastLen << endl;
  14. return 0;
  15. }
  16.  
  17.  
  18. //Input first name
  19. char getfirst( char firstName[] , int maxin)
  20. {
  21. cout << " Please enter your first name up to 15 characters " << endl;
  22. cin.getline(firstName, maxin, '\n' );
  23. //size_t firstLen;
  24. //firstLen = strlen(firstName);
  25. //cout << firstLen << endl;
  26. return 0;
  27. }
  28.  
  29.  
  30. //Input middle name
  31. char getmid( char midName[] , int maxin)
  32. {
  33. cout << " Please enter your middle name up to 15 characters " << endl;
  34. cin.getline(midName, maxin, '\n' );
  35. //size_t midLen;
  36. //midLen = strlen(midName);
  37. //cout << midLen << endl;
  38. return 0;
  39. }

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

  1. #include "nameinp.h"
  2. #include <iostream>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. //Calculate the String Lengths
  7.  
  8. size_t totallength (size_t)
  9. {
  10. size_t fullength = 0;
  11.  
  12. size_t lastLen;
  13. lastLen = strlen(lastName);
  14. cout << lastLen << endl;
  15.  
  16. size_t firstLen;
  17. firstLen = strlen(firstName);
  18. cout << firstLen << endl;
  19.  
  20. size_t midLen;
  21. midLen = strlen(midName);
  22. cout << midLen << endl;
  23.  
  24. fullength = lastLen + firstLen + midLen;
  25.  
  26. return 0;
  27. }

Okay, what am I screwing up? Thank you
Last edited by Ancient Dragon; Mar 7th, 2008 at 8:46 pm. Reason: correct code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Strings this time

 
0
  #2
Mar 7th, 2008
Okay, not sure why that posted as text instead of code
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Strings this time

 
0
  #3
Mar 7th, 2008
Wow, no one after an hour? Is it that hard or did I just mess it up that badly?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Strings this time

 
0
  #4
Mar 7th, 2008
>>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
  1. size_t totallength()
  2. {
  3. return strlen(firstname) + strlen(midname) + strlen(lastname) + 1;
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Strings this time

 
0
  #5
Mar 7th, 2008
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

  1. char *fullName = new char fullName [];

but that gave me a list of errors, so I tried this
  1. 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:
  1. char strcopy (char fullName, const char lastName);
  2. char strcat (char fullName, ' ');
  3. char strcat (char fullName, const char firstName);
  4. char strcat (char fullName, ' ');
  5. 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
Last edited by henpecked1; Mar 7th, 2008 at 11:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Strings this time

 
0
  #6
Mar 7th, 2008
>>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
  1. strcpy( fullName, lastName);
  2. strcat( fullName, " ");
  3. strcat( fullName, firstName);
  4. // 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Strings this time

 
0
  #7
Mar 7th, 2008
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?
  1. char *fullname = new char[totallength]


So it now looks like this?

  1. char *fullName = new char[]; // with the box filled in
  2.  
  3. strcopy (fullName, lastName);
  4. strcat (fullName, " ");
  5. strcat (fullName, firstName);
  6. strcat (fullName, " ");
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Strings this time

 
0
  #8
Mar 8th, 2008
>>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
  1. size_t len = totallength();
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Strings this time

 
0
  #9
Mar 8th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Strings this time

 
0
  #10
Mar 8th, 2008
>>do all the strcpy and strcats go in {} braces?

No, except they must appear inside a function such as main().
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1322 | Replies: 30
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC