Answer to Question #194061 in Other for Mpopo

Question #194061

You have to write a program to read an input file character by character to help Peter solve the following activity in his activity book. The paragraph below is given: We h2pe that 32u e5723ed the acti4it3. A6ter 32u ha4e c2mpleted the acti4it3, 0e5d 32ur re0ult t2: The Acti4it3 C2mpetiti25, Bett3 Da4i0 0treet 99, Auckla5d Park, 989, a5d 0ta5d a cha5ce t2 wi5 a hamper c250i0ti51 26 c2l2uri51 a5d acti4it3 b22k0, c2l2uri51 pe5cil0 a5d pe50. Create an input file activity.dat with the paragraph above. The numbers 0 to 7 have to be replaced as follows: 0 must be replaced by s 1 must be replaced by g 2 must be replaced by o 3 must be replaced by y 4 must be replaced by v 5 must be replaced by n 6 must be replaced by f 7 must be replaced by j Ask the user to input the names of the input and output files. Read the input file character by character, and write the character (if it stays the same) to the output file, or write the changed character to the output file. Call your output file competition.txt. 


1
Expert's answer
2021-05-21T03:44:15-0400
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

int main()
{
    std::string inputFileName, outputFileName;
    std::cout << "Enter the name of input file: ";
    std::getline(std::cin, inputFileName);
    std::cout << "Enter the name of output file: ";
    std::getline(std::cin, outputFileName);

    std::ifstream inputFile(inputFileName);

        if(!inputFile)
        {
            std::cerr << "Open input file '" << inputFileName << "' failed\n";
            return 1;
        }

    std::ofstream outputFile(outputFileName);

        if(!outputFile)
        {
            std::cerr << "Open output file '" << outputFileName << "' failed\n";
            return 1;
        }

    char c;
    while(inputFile >> std::noskipws >> c) 
    {
        switch(c)
        {
            case '0': outputFile << 's'; break;
            case '1': outputFile << 'g'; break;
            case '2': outputFile << 'o'; break;
            case '3': outputFile << 'y'; break;
            case '4': outputFile << 'v'; break;
            case '5': outputFile << 'n'; break;
            case '6': outputFile << 'f'; break;
            case '7': outputFile << 'j'; break;
            default:  outputFile << c; break;
        }
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Bhavie
05.06.21, 23:49

How do you enter the paragraph into the file through the coding and show the final paragraph where the numbers were replaced?

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS