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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}