I have a text which consists of several paragraphs in which each paragraph consists of several lines. I have the number of paragraphs and now I want to store the number of lines in each paragraph in a vector.This is what I've written and doesn't work properly:
additionally,a paragraph is separated from the next with an empty line.

vector <int> line(){
ifstream ff("A.txt");
string ss;
int Y=0;
int s=NumerOfParagraphs();
vector<int> v; 
	while (getline (ff,ss)){
		for (int i=0;i<s;i++){
			if (ss!=""){
				Y++;
			}
			else {
			v[i]=Y;
			Y=0;
			}
		}	
	}
	return v;
}

I think you should be counting the number of paragraphs and the number of lines in each paragraph at the same time so that the program only reads the file one time. Just increment a line counter each time getline() is called.

Use a structure to hold both the pragraph and line count, then make a vector of those strutures.

Thanks,I've done what you said in the first paragraph but I don't know how to use structures to hold the numbers.could you please explain more about this.You know though I don't know the number of paragraphs,how I can use a structure?

> I have the number of paragraphs and now I want to store the number of lines in each paragraph in a vector.

You do not need to calculate these separately - once you have the number of lines in each paragraph in a vector, the size() of the vector would give you the number of paragraphs.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::ifstream file( "whatever.txt" ) ;
    std::string line ;
    std::vector<int> lines_in_paras ;
    int lines_this_para = 0 ;

    while( std::getline( file, line ) )
    {
        if( line == "" )
        {
            if( lines_this_para > 0 )
            {
                lines_in_paras.push_back( lines_this_para ) ;
                lines_this_para = 0 ;
            }
        }
        else ++lines_this_para ;
    }

    if( lines_this_para > 0 ) // for the last paragraph in file
          lines_in_paras.push_back( lines_this_para ) ;

    std::cout << "#paragraphs: " << lines_in_paras.size() << '\n' ;
}
This article has been dead for over six months. Start a new discussion instead.