Pulling first letter from each word in string?

Hi! I'm trying to create a program that reads a file and pulls the first letter of each word in a string (a sentence of nine words) that will then spell a different word (nine letter word). So far, I've only been able to access the first letter of the first word. I think I need to use strcat to put the nine letters together, but I'm not sure how? I've looked online, etc., but still don't understand. Here's what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
      ifstream infile;

       infile.open("message.dat");

      string fin, word;
      while( infile.good())
       {
        getline(infile,fin);
        string text = fin.substr(0,1);
         cout << text << endl;
         }
        string text = text.substr(0,1);
        infile.close();
        return 0;
}

Thanks!
You're not collecting the first letters of the words input anywhere. You have a string called "word" which would be perfect for that.

Before your loop, clear word to make sure it's empty. Then, after your loop, concatenate each letter that you extract onto word. Finally, after your loop, write out the contents of word.
Hi,
Thank you so much for replying. For that "word" string, I was trying to collect the letters by using getline (word " "), but that wasn't working... I've looked up how to use concatenate, but I can't seem to get how to do that either. It's like??

strcat(letter1<<letter2<<letter3<<letter4<<letter5<<letter6<<letter7<<letter8<<letter9)

and then cout << letter << endl;

I apologize... I'm not sure what you mean by clear word to make sure it's empty.

Thank you again.
Concatenation with strings is pretty easy; part of the string class includes a concatenation operator "+".

You clear the variable word (which I've renamed wordOut below) just to make sure that it doesn't have any (unintended) data in it already. This may not be necessary, but in general it's a good idea to initialize your variables in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	ifstream infile;

	infile.open("message.dat");

	string wordIn, wordOut, text;

	wordOut.clear();

	while( infile.good())
	{
		getline(infile,wordIn);
		text = wordIn.substr(0,1);
		wordOut += text;
		cout << text << endl;
	}

	cout << wordOut << endl;

	infile.close();
	return 0;
}
Thank you. This looks much better than what I was going to do. When I run it, though, I still only get the first letter of the first word, twice. Do I need strcat anywhere at the end or anything? Thanks again!
Ah...I didn't look closely enough at your problem, and I didn't realize that you had all your words on the same line.

Getline() is going to return an entire line (all nine words in your case). You can either step through it yourself, or do it the easy way:

1
2
//		getline(infile,wordIn);
		infile >> wordIn;


Try that and see how it works for you.
I get the entire message now. Thanks! I'm not sure how to get rid of the extra letter on the message. When I run it, my output looks like this:
G
E
O
G
R
A
P
H
Y
Y
GEOGRAPHYY

Sorry for all the questions. Thanks for all your help!
Can you post the contents of your input file?

Also, you might get rid of the outputting of the individual letters, unless that's part of the assignment.
Okay, the file just contains this:

George Eats Old Gray Rats And Paints Houses Yellow

How would I get rid of outputting the individual letters? Thank you so much for your help!
My version of the program runs properly. Do you have an extra write statement in yours somewhere?

Here's a new copy: note the commented lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream infile;

	infile.open("message.txt");

	string wordIn, wordOut, text;

	wordOut.clear();

	while( infile.good())
	{
//		getline(infile,wordIn);
		infile >> wordIn;
//		cout << wordIn << endl;
		text = wordIn.substr(0,1);
		wordOut += text;
//		cout << text << endl;
//		cout << wordOut << endl;
	}

	cout << wordOut << endl;

	infile.close();
	return 0;
}
Thank you so much! The extra Y is still on there, but the individual letters are gone now and it just reads the word GEOGRAPHYY. Stupid question, but there's a period at the end of the sentence in the data file, is that why the extra letter on the end? Thank you! I really appreciate your help!

Here's what I ran:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
       ifstream infile;

       infile.open("message.dat");


       string wordIn, wordOut, text;

       wordOut.clear();
      while( infile.good())
       {

           // getline(infile,wordIn);
             infile >> wordIn;
            // cout << wordIn << endl;
             text = wordIn.substr(0,1);

             wordOut += text;

             //cout << text << endl;
             //cout << wordOut << endl;
        }


        cout << wordOut << endl;

       infile.close();

       return 0;
}
Nope...that shouldn't do it. You sure you don't have the word "Yellow" in there twice?

The << operator is a little trick sometimes, but...it works on my system, so I don't know. What OS and compiler are you running?
Just checked, and "Yellow" isn't in there twice. It's probably just my computer or something. I'm using this PUTTY system for the program. Not sure about how good PUTTY is. Perhaps I'll try and run the program on a different computer? Thanks again! You've been so kind and helpful!
No problem at all...keep plugging away. And yeah, try compiling your program with a different compiler, just for fun. Let me know if you want more help.
Okay, I'll give that a try. Again, thank you so much for all of your help. Much appreciated!
Topic archived. No new replies allowed.