954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to output certain letters from words??

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;


}
wicked357
Light Poster
26 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You