Jan 19, 2013 at 8:16am UTC
I want to make a program that can read from a text file, then output what is in the text file in vertical lines.
Example:
The text in the file is horizontal:
My dog is red.
I want it to be displayed vertically:
m
y
d
o
g
i
s
r
e
d
I already have already created part of the program to display from the text file.
I have about 5 lines that i want to be displayed vertically.
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 <cmath>
#include <cstdlib>
using namespace std;
int main ()
{
cout << "Enter filename:" << endl;
char filename[500];
ifstream bookshelf;
cin.getline (filename, 500);
bookshelf.open (filename);
if (!bookshelf.is_open()){
exit(EXIT_FAILURE);
}
char word[500];
bookshelf >> word;
while (bookshelf.good()){
cout << "\n" << word << " " ;
bookshelf >> word;
}
system("pause" );
return 0;
}
I need help on how to get the desired output. I don't know where to start. Give me some direction as to what I need to do next. Thank you.
Last edited on Jan 19, 2013 at 8:17am UTC
Jan 19, 2013 at 9:54am UTC
Sadly, I'm still a newbie on the C++ but as far as i think you need to use "Tokens". I hope somebody will explain it as I haven't got to them ,yet.
Jan 19, 2013 at 7:43pm UTC
Thank You JLBorges. Your solution works great. I am a beginner so I still do not understand most of what is going on in the code. Can you please explain how it works?
Jan 19, 2013 at 7:51pm UTC
Last edited on Jan 19, 2013 at 7:53pm UTC