Printing text file line by line pressing enter

Hi,

I'm writing a code for an assignment, and I can't figure out how to get the code to print one line at a time after the user presses enter. Right now it's print the whole text file at once. I'm assuming it's got something to do with the getline() function but I don't know how to use it in the code, or turning each line into a string and then printing it out.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
	ifstream in("read.txt");

	if(!in){

		cout<< "Cannot open input file!" << endl;
		return 1;
	}

	cout<< "This program reads and prints a text file" << endl;
	cout<< "Press Enter for every line of text to be displayed" << endl;


	if(cin.get() == '\n'){
	char str[50];
	int i = 50;

	while(in) {
		in.getline(str, i);
		if(in) cout << str << endl;


	  	  }

	  	  in.close();
	}
  return 0;
  }


Thanks for the help!
Well yeah. Your only cin.get() is before you enter your read loop. Move the cin.get() inside the read loop.
This should work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
  ifstream in("read.txt");

  if(!in)
  {
    cout<< "Cannot open input file!" << endl;
    return 1;
  }

  cout<< "This program reads and prints a text file" << endl;
  cout<< "Press Enter for every line of text to be displayed" << endl;
  string line;
  while(getline(in, line)) 
  {
    cout << line;
    char ch;
    do
    {
      ch = cin.get();
    }while(ch != '\n');
  }
  system("pause");
  return 0;
}
Topic archived. No new replies allowed.