need some help with C string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #11
Jun 29th, 2009
A standard ASCIIz C string buffer requires a space to store the terminator.

['A'][0] [ ] [ ] [ ] [ ]
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #12
Jun 29th, 2009
['A']['P']['E']['\0']
3 = strlen( "APE" );
But actually occupies 4 bytes!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 282
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #13
Jun 29th, 2009
nvm. It gives me some conversion error.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #14
Jun 29th, 2009
I just reglanced at your first post and you have the same (no room for terminator) problem as well.
  1. char mycstring[number + 1];
  2.  
  3. mycstring[number]='\0';

You need to add 1 to the buffer length to store the terminator!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 282
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #15
Jun 29th, 2009
that's what i have the +1 for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #16
Jun 29th, 2009
Then you've revised your code from the first post because it doesn't show the +1. I added the +1 in my previous code snippet posting to indicate how to do it.
You should probably repost your current revised code then so everyone that is helping has a cleaner base to view!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 282
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #17
Jun 29th, 2009
most recent code

//main
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. #include "strextra.h"
  5.  
  6. int main()
  7. {
  8. Search a;
  9. cout<<"enter max number of characters in the sentence";
  10. int number;
  11. cin>>number;
  12. cout<<"enter a sentence";
  13. char let;
  14. char mycstring[number+1];
  15. for(int i=0; i<number; i++)
  16. {
  17. cin>>let;
  18. mycstring[i]=let;
  19. }
  20. mycstring[number+1]='\0';
  21. cout<<"my string is\n"<<mycstring<<"\n";
  22. cout<<"enter the character you are trying to find in the string above\n";
  23. char alpha;
  24.  
  25. char lett[2];
  26. for(int a=0; a<1; a++)
  27. {
  28. cin>>alpha;
  29. lett[a]=alpha;
  30. }
  31. lett[1]='\0';
  32. cout<<lett[0]<<lett[1];
  33. //a.find(mycstring, lett);
  34.  
  35. }

//interface
  1. #ifndef STREXTRA_H_INCLUDED
  2. #define STREXTRA_H_INCLUDED
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Search
  8. {
  9. public:
  10. int find(char mystring,char letter);
  11. int find(char mystring,string word);
  12.  
  13. };

//implementation
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5. #include "strextra.h"
  6.  
  7. int search::find(char mystring,char letter)
  8. {
  9. return strchr(mystring,letter);
  10. }

#endif // STREXTRA_H_INCLUDED
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #18
Jun 29th, 2009
You've almost have your +1 covered but I believe you have a problem.

char mycstring[number+1];
Firstly Your buffer is {0...number-1, number }
Where as number index is the terminator.
Incorrect:
mycstring[ number+1]='\0';
Correct:
mycstring[ number ]='\0';

Secondly the array index requires a constant. Not a dynamic value. That buffer is declared in the scope of the function call. that is it exists on the stack as
FUNCTION RETURN
Local Stack Arguments
Other FUNCTIONS RETURNS when called within the braces {}.
So you either have to dynamically allocte memory, or use an local buffer large enough to contain your largest string. And keep a count of how many characters you've stuffed into it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #19
Jun 29th, 2009
Also you're still using a second loop for the single character entry.
You don't need the loop. Or is this a setup for testing your other function which finds substrings within strings?

Also you have two prototypes for find.
One is ASCIIz string and character, but you're passing ASCIIz string, ASCIIz string.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need some help with C string

 
0
  #20
Jun 29th, 2009
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number+1];

This is actually compiling for me and I'm a little surprised, since I was taught to never do this. Aren't you either supposed to make number a constant or allocate the array with the new command? Why isn't this code giving me an error?

Looks like wildgoose has the same idea regarding dynamic allocation, but again, shouldn't this not compile at all?


[EDIT]
This is one fast moving thread. 20 posts in less than an hour!
[/EDIT]
Last edited by VernonDozier; Jun 29th, 2009 at 3:55 pm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC