Ok I am writing a program that will read in 5 words using string list[5] and a for loop. Now I need to get it to output the 1st and 3rd letters in the words that were entered. I am having a bit of trouble and not sure what I am doing wrong. Am I supposed to use a do..while to accomplish this and if so can I please get an example are maybe some suggestions on what I might need. Thank you

Here is what I got so far...(reads 5 words but cant get it to display the certain letters wanted)

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{

	string list[5];
	int counter;

		for (counter = 0; counter < 5; counter++)
		{
			cout << "Enter five words: ";
			cin >> list[counter];
		}

		cout << "The first and third letters in the entered words are: "
		 	 << static_cast<unsigned char> (list[5].find(list[5], 1, 3))
			 << endl;

		cout << endl;


}

Recommended Answers

All 4 Replies

O

    cout << "The first and third letters in the entered words are: "
         << static_cast<unsigned char> (list[5].find(list[5], 1, 3))
         << endl;

    cout << endl;


}

end quote.

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].

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?

Member Avatar for iamthwee

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.

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.