View Single Post
Old 10-23-2004, 08:42 PM   #14 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
by the way michael, he used my code and changed it,
What are you talking about? Who is "he". I hope you don't mean me. I don't need "your" code. Conceptual my code is completely different from what you showed us here.

Quote:
Valmont... However, why did you have to go and write out the whole program??
Because I received some input form "somewhere else". So I decided to post the whole code. Only a few minutes of work.
Besides, I was in the mood to show various approaches. I posted 3. You can make a 4th (do-loop) and a 5th (while loop) and a 6th (using a char* with STL, very tricky)...
Anyway, if you understand the concept then you know enough.

Quote:
don't understand what you mean by allocating space. are you saying that i have to make the compiler think that the sReversedWord string is an array...
A std::string IS a container (sort of a luxury "array").
However, if you do this:
Code:
string sMyString;
Then sMyString is only a name for us humans. In the computer, there is no sMyString yet.
But each time you add something to that string, std::string will allocate memory for it, and then it does exist.

Example:
Suppose you do this:
Code:
string sMyString;
sMyString[0] = 'X';
Then the std::string will think:
"Hey, Androto is telling me that sMyString[0] truly exists. Therefore he did something that created sMyString[0]. Now he is trying to replace sMyString[0] with 'X'."
But that is not true yet. You didn't initialize the string with anything yet, so it doesn't exist. You're trying to fool td::string. You can't replace sMyString[0]. There is no [0] yet.

But if you do this:
Code:
string sMyString = "belly";
Then sMyString[0] does exist. And [1] too. And [2] and [3], [4] too! Because you assigned sMyString letters (a word in this case), std::string will create the spots where all the elements fit in.
And now you can replace the EXISTING elements indeed like:
Code:
string sMyString; //ONLY a name for humans: [0]...[etc] does NOT exist yet!
sMyString = "hello"; //NOW std::string makes sure [0]...[4] exist!
for(unsigned i=0; i < sMyString.size(); ++i)
{
   sMyString[i] = 'i';
}
cout<<sMyString<<endl;
//output: 01234
And that's why your approach won't work.

Quote:
one more thing. the error that i got before (parse error before ',' )
- You need to add: using namespace std;
- You need to change the call to verify like this: verify( reversed, word);
__________________
Valmont is offline   Reply With Quote