#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace 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
}
}
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.