Hello everybody, I am new here in this community and I hope to learn a lot from you guys and then maybe later become the one to give out advices. I am also new to programing and I recently started learning C++.

I am doing a program that is supposed to ask a user for a name of a file and count the lines from that file and print it out in another file and also to the screen.

I am supposed to use a function but I can´t use pointers, vectors, arrays because we have not covered that yet in the course.

My problem is that I can´t figure out how to actually count the lines and print it out to an output file and also to the screen.

Here is what I got so far.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>


using namespace std;

void countLines(ifstream& in_stream, ofstream& out_stream);

int main( )
{

    using namespace std;
    char in_file_name[16], out_file_name[16];
    ifstream in_stream;
    ofstream out_stream;


    cout << "Enter the input file name (maximum of 15 characters):\n";
    cin >> in_file_name;
    cout << "Enter the output file name (maximum of 15 characters):\n";
    cin >> out_file_name;

    in_stream.open(in_file_name);
    if (in_stream.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    out_stream.open(out_file_name);
    if (out_stream.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
}

void countLines(ifstream& in_stream, ofstream& out_stream)

{
    int count;
    char next;
    in_stream.get(next);
    while (! in_stream.eof( ))
    {
        if (next == '\n')
            out_stream << count++;
        else
            out_stream << next;

        in_stream.get(next);
    }
}

Any help would be much appreciated.

Recommended Answers

All 2 Replies

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.