Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

14) (10 points) A text file words. txt contains a paragraph of text. Write a C++

ID: 3903238 • Letter: 1

Question

14) (10 points) A text file words. txt contains a paragraph of text. Write a C++ program that opens the file, reads all the words in the file and prints on standard output a list of all the words used in the paragraph, with one word per line. Each word must appear only once in the list. The list of words must be ordered alphabetically. It is assumed that the text contains no punctuation and that all words only include lower-case characters for simplicity. Write your program on the back of this page.

Explanation / Answer

Hi,

hereby , i have written the code for reading the content from the text file and displaying each words in new line.

Program:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main()

{

    string checkline;

    string words[16];

   ifstream Test ( "words.txt" );

    if (!Test)

    {

        cout << "There was an error while attempting to open the file ";

        return 0;

    }

     int a=0;

    while( Test>>checkline )               //stores the wordss list in array

    {

        words[a]=checkline;

        a++;

    }

    cout<<"Array contents: ";          //outputs the number of words from the txt file           

     for (int a=0;a<16;a++)

     cout<<words[a]<<endl;

    return 0;

}

Sample Output:

words.txt

Adam got a sugar cup
in his right hand while he was
listening to music

Adam
got
a
sugar
cup
in
his
right
hand
while
he
was
listening
to
music

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote