This what I'm trying to do:

Type two or more lines of text into a file. Write a program to
    open the file, reading each word into a vector<string> object. Iterate
    over the vector, displaying it to cout. That done, sort the words using
    the sort() generic algorithm

But I'm stuck unfortunately.

#include <iostream>
#include<fstream>
#include<vector>
#include<cstdio>

using namespace std;

int main()
{
    ifstream infile("about.txt");

    if(!infile){
        cout<<"Sorry, file could not be open"<<endl;
    }else{
        vector<string> strVec;
       // string eachWord[];

        while(!infile.eof()){
            strVec.push_back(getline(about));
        }

    }

    return 0;
}

Error: 'about' was not declared in this scope

strVec.push_back(getline(about));

In that line of code, what is the variable about? It does not exist.

is the name of the data file. the data is called "about.txt". How can i make it that about contain what's in the file?

The object you created to be the source of the data is not named about. It is named infile. You created this object on line 10.

You should first get a line from your textfile and split it in separate strings and then feed those strings to your strVec variable.

Replace strVec.push_back(getline(about)); with strVec.push_back(getline(infile));
Then show the vector strVec
Then sort the vector strVec
Finally show again the vector strVec

I did this:

 while(!infile.eof()){
            strVec.push_back(getline(infile));
        }
Heading Here

BUT GOT THOSE ERRORS:

Invalid arguments '
Candidates are:
std::basic_istream<#0,#1> & getline(std::basic_istream<#0,#1> &, std::basic_string<#0,#1,#2> &)
std::basic_istream<#0,#1> & getline(std::basic_istream<#0,#1> &, std::basic_string<#0,#1,#2> &, #0)
'   readingSentences.cpp    /fStream    line 36 Semantic Error
Invalid arguments '
Candidates are:
void push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)
void push_back(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)
'   readingSentences.cpp    /fStream    line 36 Semantic Error
no matching function for call to 'getline(std::ifstream&, int)' readingSentences.cpp    /fStream    line 36 C/C++ Problem

You are using this getline function:

http://www.cplusplus.com/reference/string/string/getline/

As you can see, it takes more than one parameter, and it does not return a string. If you want to use that function, you will need to create string to store the data in. Something like:

string inputdata;
getline(infile, inputdata);
strVec.push_back(inputdata);
string words;
    while(!infile.eof()){
                strVec.push_back(getline(infile, words));
            }

Errors:

Invalid arguments '
Candidates are:
void push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)
void push_back(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)
'   readingSentences.cpp    /fStream    line 37 Semantic Error
no matching function for call to 'std::vector<std::basic_string<char> >::push_back(std::basic_istream<char>&)' readingSentences.cpp    /fStream    line 37 C/C++ Problem

strVec.push_back(getline(infile, words));

What are you passing to the function push_back? You're passing whatever the function getline returns. What does the function getline return? Check here:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

Looks like it returns an istream. What kind of object should you be passing to push_back (on a vector of strings)? A string. Is an istream object the same thing as string? No. It is not.

A bigger question here is why didn't you work this out for yourself? The error message said "invalid arguments" and told you the function in question; push_back. So you should have looked at your use of the fuction push_back to see what arguments you were passing it. The point of error messages is for YOU to read them and work out what the problem is. Sometimes they're very difficult to understand, but this one is very simple.

commented: Fine explanation! +15

Also: ask yourself---
1) how does getline() know when to stop extracting information from the input stream?

2) What can you do to avoid default behavior, if you want to? All of that information should be in your documentation as well.

3) what is the difference between a line and a string and a word and how can I manipulate from one to the other?

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.