File Input

Given an input file of sentances, how do you return the first word of every sentance?

Example input file:
1
2
3
4
This is the story of
a cat and a 
dog who were 
friends


How do I return this?
1
2
3
4
This
a
dog
friends


My code 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
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	ifstream infile;
	string line, inputfilename;

	cout << "Enter the file name ";
	getline (cin, inputfilename);
	
	infile.open(xyz.c_str());

// some code needed here to input from the file
	
	while (infile)
	{
		
// some code here to output the desired word

// some code needed here to input from the file

	}
	infile.close();
	return 0;
}
You're really just returning the first word of every line.
right..thats what i meant..i need to return the first word of every line..but not sure how
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    ifstream in;
    in.open("file");
    string s;
    while(getline(in, s)) // fetch each line
    {
        stringstream line(s); 
        string firstword;
        line >> firstword; //fetch first word of current line
        cout << firstword << endl; // you could put in a list here
    }
}
Last edited on
felip21: Please don't post answers to homework, only helpful hints.
thank you felipe21..very helpful but i haven't learned about sstream yet so i won't be able to use it. any other helpful hints?
std::getline

felip21: Please don't post answers to homework, only helpful hints.


You are right. I got a bit carried away : )

i haven't learned about sstream yet so i won't be able to use


You mean you can't as your teacher won't let you or something or you don't know how?

If the the latter, it is basically identical to fstream, you just treat a string as a file.

[edit]
What I did in the code is to open a file(with the text) and then create a "file" for each line

getting the first word as you would get one word from a normal stream
Last edited on
teacher won't let us use methods we haven't covered in class yet..
Once you read the line from the file, starting at the beginning of the line read, continue to output the characters until you find a space or a newline.

teacher won't let us use methods we haven't covered in class yet..


Personally, I would file a serious complaint with the teacher, and then the dean of the school immediately. You can't possibly cover every single topic in the class. If you aren't able to read a book or a website on your own and improve your skills without a teacher holding the hand every step of the way then the class itself seems pointless. This is a college class? The stream classes all inherit from the same base. If you can use a file stream it shouldn't be too difficult to use a stringstream. When I was in college we were expected to read further and learn as much as possible on our own. We were always responsible for material regardless of whether it was covered in class specifically. Perhaps you are misunderstanding the teacher and if not I wouldn't be very happy with those teaching methods. Different people learn at different rates and should not be held back just because something was not yet discussed in class.
Last edited on
Topic archived. No new replies allowed.