Question #180612

Write a C++ program to create a new file named “college.txt” to store list of colleges under Punjab 

university. Read names of 3 colleges (consider input given below) from console and store it in 

“college.txt”. Further copy contents of “college.txt” in another file “university.txt”. Now open the 

“university.txt” file and display the following (underlined) characters on console using file 

manipulation functions.

Input: 

Lovely Professional University

Thapar Institute of Engineering and Technology

 Dr B R Ambedkar National Institute of Technology


Expert's answer

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const std::string collegeFile{ "college.txt" };
    const std::string universeFile{ "universe.txt" };
    std::string line;
    std::ofstream collegeWrite(collegeFile);
    for (int i = 1; i < 4; i++)
    {
        std::cout << "Enter name" << i << "college: ";
        std::getline(std::cin, line);
        collegeWrite << line << std::endl;
    }
    collegeWrite.close();
    std::ifstream collegeRead(collegeFile);
    std::ofstream universeWrite(universeFile);
    while (std::getline(collegeRead, line))
    {
        universeWrite << line << std::endl;
    }
    collegeRead.close();
    universeWrite.close();
    std::ifstream universeRead(universeFile);
    while (std::getline(universeRead, line)) 
    {
        std::cout << line << std::endl;
        //simulate underline
        for (int i = 0; i < line.length(); i++) 
        {
            std::cout << "-";
        }
        std::cout << std::endl;
    }
    universeRead.close();
    return 0;
}

LATEST TUTORIALS
APPROVED BY CLIENTS