O
cout << "The first and third letters in the entered words are: "
<< static_cast (list[5].find(list[5], 1, 3))
<< endl;
cout << endl;
}
[/code]
I'm drunk at the moment, but I can see a few problems with this code. First, you are out of range on the array (0 thru 4 only) and you are only asking for a SPECIFIC out of range element. Also, find is not a member function of list[5].
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
So why not use
cout << "The first and third letters in the entered words are: "
<< static_cast<unsigned char> list[1][1] << list[1][3];
to display the 1st and 3rd letters of word #2?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
1) This don't compile it without errors, so look at your errors!
2) Use spaces instead of tabs to indent your code in future.
3) @ Mr Disney, arrays start and 0.
4) Please see a more contrived example below (untested):)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector <string> crap;
for ( int i = 0; i < 5; i++ )
{
string word;
cout << "Enter a word ugly:";
cin >> word;
crap.push_back( word );
}
for ( int j = 0; j < 5; j++ )
{
string tmp = crap[j];
//output first and third character
cout << tmp.substr ( 0,1 ) << " " << tmp.substr( 2,1 ) <<endl;
}
return 0;
}
You might also wanna do a check to ensure the word entered is more than three letters long.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
for ( int j = 0; j < 5; j++ )
{
string tmp = crap[j];
//output first and third character
cout << tmp.substr ( 0,1 ) << " " << tmp.substr( 2,1 ) <<endl;
}
Wouldn't it make more sense to to:
for ( ... ) {
std::cout<< crap.at(j).at(1) << " " << crap.at(j).at(3);
// std::cout<< crap[j][1] << " " << crap[j][3];
}
or use the [] operator, of course (though at throws ... not so certain about []) instead of constructing a string for every iteration of the loop? Obviously use substr if you want more than one character.
Tut tut tut. Giving away all the code...
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57