file to print single words per line

I'm trying to print each letter from a data file (will show data).
but my program just ends up printing the while data file without the spaces and not going into the else function.

data.txt:
Write a program that reads several lines from a data file and prints each word of the
file on a separate line of an output file followed by the number of letters in that word.
Also print a count of words in the file on the screen when done. Assume that words are
separated by one or more blanks.
:

so my output is just essentially what is above but with out spaces, when i want it to display each word then go into the else statement where it enters a new line.
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
  #include <iostream>
#include <fstream>
#define in_file "data.txt"
#define out_file "result.txt"


using namespace std;
int main(){
    ifstream ins;
    ofstream outs;
    ins.open(in_file);
    outs.open(out_file);
//Call function that
    string hello
    char letter;
    ins >> letter;
    while (!ins.eof()){
        if(!(letter == ' ' && letter == '.' && letter == '/n')){
            cout << letter;
        }
        else
            cout << endl;
        ins.get(letter);
    }

    ins.close();
    outs.close();
    return 0;
}


.. to print single words per line
trying to print each letter from a data file


So which is it? Words or letters?
Last edited on
Hello awesomesause.

As gunnerfunner pointed out you are trying to read each individual character when you need to be working with words.

As the instructions say:

that reads several lines from a data file

That says to me read an entire line until ā€™\nā€™ is reached then break down the line into individual words. When you break up the line into individual words you can figure out the total number of letters in the word.
Hint variable _name.size() will work. I used a nested while loops and a stringstream to make the program work. I also used a for loop, but that seemed to easy.

You also need a total count of the words in the file and you have no code to output to your output file.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.