I am trying to write a code that is giving me a great deal of problems.

The program challenge is as follows:
Write a program that asks the user for the name of a file. The program shold display the contents of the file on the screen. If the file's contents won't fit on a single screen, the program should display 24 lines of output at a time, and then pause. Each time the program pauses, it should wait for the user to strike a key before the next 24 lines are displayed.
NOTE: Using an editor, you should create a simple text file that can be used to test this program.

I have written some code and it sort of works, but my .txt file is not showing in the command window. Please help!

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

int main() 
{

    cout << "Please enter the name of the file: ";
    string fileName;
    getline(cin, fileName);

    ifstream file(fileName.c_str(), ios::in);

    string line;
    for (int count = 1; !file.eof(); ++count) 
    {
        getline(file, line);

        cout << line << endl;
        if (count % 24 == 0) system("Pause");
    }

       system("Pause");


 }

Line 18 : getline is to be called like this file.getline(line, 200,'\n');

I tried but all I get are errors.
The errors are:

Error   1   error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::string' to 'char *'   c:\users\boopookins\documents\visual studio 2010\projects\file display program\file display program\testing.cpp 18


    2   IntelliSense: no instance of overloaded function "std::basic_ifstream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list   c:\users\boopookins\documents\visual studio 2010\projects\file display program\file display program\testing.cpp 18

I have not seen a for loop used like for readin a file before. When I read from a file I use a while loop. Try doing something like this and see if it helps.

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

using namespace std;

int main()
{
    string input;
    ifstream fin("myfile.txt");
    int count = 0;

    while (getline(fin, input))  // use getline to control the loop.  once the read
    {                            // fails it will end the loop.
        cout << line << endl;
        count++;
        if (count % 24 == 0)
            cin.get();  // used to pause
    }

    cin.get();
    return 0;
}

If I were to implement this into the code I already have where would I put it? I am new to this so I am still getting confused. Will your code work for asking the user to input the .txt file of their choosing?

If I were to implement this into the code I already have where would I put it?

It's a replacement for your code.

Will your code work for asking the user to input the .txt file of their choosing?

No, you'd need to add that part. Though it shouldn't be a big deal as you clearly already know how to do it (that part of your code is correct).

I replaced the code to what you see know. However when ever I run the code the .txt file that I have will not display on the screen.

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

int main()
{
    cout << "Please enter the name of the file: ";
    string fileName;
    getline(cin, fileName);

    ifstream file(fileName.c_str(), ios::in);

    string input;
    ifstream fin("Show.txt");
    int count = 0;

    while (getline(fin, input))
    {
        cout << input << endl;
        count++;
        if (count % 24 == 0)
            cin.get();
    }

    cin.get();
    system("pause");
    return 0;
}

ifstream file(fileName.c_str(), ios::in);
ifstream fin("Show.txt");

Which of those objects are you actually using? That's the problem.

Which of those objects are you actually using? That's the problem.

If I comment out one or the other I still do not get my .txt file to show. I am really confused as to what is supposed to be happening.

Then your file is not where the program can find it.

You are not testing for errors. If the program can't find the file, you ignore the error and read it anyway. Look up the file methods again and add error conditions with worthwhile messages.

If I comment out one or the other I still do not get my .txt file to show.

The file you typed is never the file that gets read. Ever. Period. Why? Because you only use fin to read lines, and fin always uses "Show.txt". So is Show.txt the file that you actually have and want to read? If so, then it's not located in the correct place (as Walt said), and you need to do some error checking. Otherwise you need to stop opening Show.txt and use file to read lines instead.

perror() may work for you to get some additional error information:

#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string filename;

    cout << "Enter a file name: ";

    if (getline(cin, filename)) {
        ifstream in(filename.c_str());

        if (!in) {
            perror("Error opening file");
        }
        else {
            cout << "The file opened successfully\n";
        }
    }
}

You were correct. It is stating that the file cannot be found. It has been a few years since I learned to open a file through a program so I can't exactly remember how to save it so that the program will actually find the file. Do I save it in word pad or do I create a file through visual c++? I have done both but the program is not finding the .txt files?

The file just isn't in the right place. Move it to your project folder along with the .cpp file and try again. If that doesn't work, move it into the debug folder with the .exe file for your program.

Thank you for the help. It runs correctly now.

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.