Answer to Question #72075 in C++ for Dhvani

Question #72075
Write a C++ code which writes the resultant words to the output file, “second_file.txt”.
Example: Content of first_file – “I am going to buy an umbrella”
Output in second_file.txt – I am an umbrella
1
Expert's answer
2017-12-22T04:44:49-0500
Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool is_valid_word(const string& word) {
if (word.empty()) return false;
char ch = tolower(word[0]);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

int main(int argc, char* argv[]) {
string inputFileName, outputFileName;

// Read input file path
cout << "Enter input file path: ";
getline(cin, inputFileName);

// Read output file path
cout << "Enter output file path: ";
getline(cin, outputFileName);

// Trying to open input file
ifstream input(inputFileName);
if (!input.is_open()) {
cout << "Unable to open input file: " << inputFileName << endl;
return -1;
}

// Trying to open output file
ofstream output(outputFileName);
if (!output.is_open()) {
cout << "Unable to open output file: " << outputFileName << endl;
input.close();
return -1;
}

// Read words and write resultant into output
string word;
while (input >> word) {
if (is_valid_word(word))
output << word << " ";
}
// Close
input.close();
output.close();
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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS