Hey Everyone,

I am including my current code. Can anyone give me advice on my middle function declaration? I rewrote this a few times and am now going on 12 hours spent on this one function alone! I am trying to just get it to display the last 10 lines of a 31 line file. You can see my weak attempt thus far to do that...haha.

Can anyone point me in the right direction?

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void readfile(string f, ifstream &infil);
void readLast10(string f, ifstream &infil);
void withLine(string f, ifstream &infil);


int main()
{

    ifstream infile;
    string clsname, filen;
    int response;
    cout << "Enter file name : ";
    cin >> filen;
    cout << "1. readfile\n2.read last 10\n3. with line#\n99 exit\n"
        <<"\nEnter 1, 2, 3, 99 : ";
    cin >> response;

    while(response != 99)
    {
        if(response == 1)
        readfile(filen, infile);



        else if(response == 2)
            readLast10(filen, infile);



        else if(response==3)
            withLine(filen, infile);


        cout << "1. Display All Records in file\n2. Display last 10 records 10\n3. Display all records with line numbers\n99 exit\n"
            <<"\nEnter 1, 2, 3, 99 : ";
        cin >> response;
    }

    system("pause");
    return 0;
}




void readfile(string f, ifstream &infil)
{

        {
        infil.open(f, ios::in);
        getline(infil, f, '\n');

        while(infil)
        {
            cout << f << endl;
            getline(infil, f, '\n');
        }
         infil.close();
        }
        cout<< endl;
        cout<< endl;
}

void readLast10(string f, ifstream &infil)
{
    char hold[80];
    int startline;
    int linenumber = 0;
    infil.open(f.c_str(), ios::in);
    getline(infil, f, '\n');

    while(infil)
    {
    ++ linenumber;
    getline(infil, f, '\n');
    startline = linenumber -10; 
    }

       //help!!!!!!

    infil.close();


}




void withLine(string f, ifstream &infil)
{

    int linenumber = 0;
    infil.open(f, ios::in);
    getline(infil, f, '\n');

    while(infil)
    {
    cout << ++ linenumber << ": " << f << endl;
    getline(infil, f, '\n');
    }

}

Nevermind on this, I decided to just return a variable to make it work in two decs. Thank you

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.