Question

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code

Homework Answers

Answer #1

C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX is provided below with the output screen:

// This header file includes every header file
#include<bits/stdc++.h>
using namespace std;

// Keeping the file name as documents
const string FILE_NAME = "documents.txt";

vector<string> File_Lines(string file) 
{
    ifstream in(FILE_NAME);
    vector<string> lines;
    for (string line; getline(in, line); ) 
    {
        lines.push_back(line);
    }
    return lines;
}

string get_input() 
{
    // Initializing string variable
    string str;
    getline(cin, str);
    return str;
}

int main()
 {
    // reads the contents on line
    vector<string> lines = File_Lines(FILE_NAME);

    // open an output stream to append new lines
    ofstream fileOut(FILE_NAME, ios::app);

    long long t;

    cout<<"Enter the number of line you want with index: "<<endl;
    cin>>t;
    for (int n = 0; n < t; n++)
     {
        // get a line from the user
        cout << "> ";
        std::string t = get_input();

        // finding it in the vector of lines
        auto it = std::find(lines.begin(), lines.end(), t); 
        
        if (it == lines.end()) 
        {
            // if the line was not found then append it
            fileOut << t << endl; 
            lines.push_back(t);   
            cout << "Line \"" << t << "\" is indexed.\n";
        }
         else 
         {
            // the line was found then this is the index.
            int index = it - lines.begin();
            cout << "Line \"" << t << "\" was found at index: " << index << ".\n";

            // getting the adjacent line
            int indexx;
            if (index%2 == 0) 
            {
                // if the index is even then get the next line
                indexx = index + 1;
            } 
            else
             {
                // odd index, get the previous line
                indexx = index - 1;
            }
            if (indexx < lines.size())
             {
                string line2 = lines[indexx]; // the text of the adjacent line
                cout << "Adjacent line: \"" << line2 << "\" (index " << indexx << ")\n";
            } 
            else 
            {
                cout << "No adjacent line is found yet!\n";
            }
        } // ends if the line was found 
    } // ends for t times

    cout << endl;
    get_input();

    return 0;
}

Output screen:

Now the file named "Documents" will be created with the lines entered by the user indexed in it.

Thank you!!! Good luck! Keep Coding :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Attached is a text file full of names. Write a program that prompts the user to...
Attached is a text file full of names. Write a program that prompts the user to type in a name. If the name appears in the list of names, the program prints a message indicating the name was found. If the name entered by the user is not in the database, the program prints a different message indicating the name was not found. The program will continue prompting the user and searching for names until the user enters "quit". The...
1.      Create 100 text files automatically; in each file write a random number from 1 to 10....
1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….) 2.      Read the content of the 100 files and combine them into a 1 single file. 3.      Write java code to do the following: a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW b.      Read the file using a java program c.      Find how many D’s in the file d.      Extract the text between the @ and the # 1. Create 100 text files...
I NEED A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX, if we have an DB...
I NEED A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX, if we have an DB file has a records of students number and names like this (Student file) stNum**stName 1 jack 2 maya 3 sam 4 alex 5 jane . . . . . . this file may have a thousands records, if we want to search for a specific record(student), we need to find it by (Index file), in this index file we have this (Index file) stNum**Address...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
C++. This program can open a text file and print its contents to the screen. For...
C++. This program can open a text file and print its contents to the screen. For example, it could load the output.txt file generated in the previous program and display on the screen the text contained in the file.
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...