Output to command line format
Jan 16, 2012 at 8:20pm UTC
Hi I am trying to read in from a file based on the users input of the file name. I am running into one problem. The text file that i have to test has a space after the first line which looks like this:
Here
Is
A
SAMPLE
TEXT
File that i will
use to show input.
the output is this:
Here
Is
A
SAMPLE
TEXT
File that i will
use to show input.
why does it get rid of one line after Here.
Here is my code:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Text
{
public :
string name;
string object;
Text();
void contents();
};
int main()
{
Text txt1;
txt1.contents();
cin.ignore();
return 0;
}
Text::Text()
{
name = " " ;
object = " " ;
};
void Text::contents()
{
cout << "Please enter the name of the text file to be opened (Please include file extention): " << "\n" ;
cin >> name;
ifstream in(name); // Opens test4.txt for reading
if (in == NULL)
{
cout << "Error opening file" ;
cin.ignore();
}
else
{
while (getline(in,object))
{
cout << object;
cin.ignore();
}
}
};
Jan 16, 2012 at 9:23pm UTC
Because
getline
ignores new line character (or other delimiter).
You may modify lines 45-46 with
1 2
cout << object << endl;
//cin.ignore();
Jan 17, 2012 at 12:13am UTC
Isn't there a way to change the default delimiter value?
Jan 17, 2012 at 7:03pm UTC
Topic archived. No new replies allowed.