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.
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.
Write a C++ program to read a text file "input.txt" and store the data in form...
Write a C++ program to read a text file "input.txt" and store the data in form of 2d array (or any other data structure you prefer). Input file: a0 400 1 0 20 a1 300 2 0 30 a2 200 3 0 40 a3 100 6 0 40 a4 0 7 0 50
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.
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;   ...