Answer to Question #16426 in C++ for Abhishek Anand

Question #16426
Write an interactive menu driven program to create a text file and display it. Create another file by converting each line of textfile into uppercase. Also, display the size of the textfile.
1
Expert's answer
2012-10-18T08:17:45-0400
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;

void stoupper(string& s) {
string::iterator i = s.begin();
string::iterator end = s.end();

while (i != end) {
& *i = toupper((unsigned char) *i);
& ++i;
}
}

int main() {
fstream File;
string fileName;
int selection = 0;
while (selection != 4) {
& system("cls");
& cout << "-------------------------------" << endl;
& cout << " M& E& N& U" << endl;
& cout << "-------------------------------" << endl;
& cout << " 1. Create text file" << endl;
& cout << " 2. Display text file" << endl;
& cout << " 3. Convert text file " << endl;
& cout << " 4. Exit" << endl;
& cout << "-------------------------------" << endl;
& cout << " Choose: ";
& cin >> selection;
& switch (selection) {
& case 1: {
& cout << "Enter file name: ";
& cin >> fileName;
& File.open(fileName.c_str(), ios::out);
& system("cls");
& cout
& lt;< "Enter some text to file (to finish please input empty line)"
& lt;< endl;
& string line;
& cin.ignore();
& getline(cin, line);
& while (line.length() != 0) {
& File << line << endl;
& getline(cin, line);
& }
& File.close();
& break;
& }
& case 2: {
& cout << "Enter file name: ";
& cin >> fileName;
& File.open(fileName.c_str(), ios::in);
& system("cls");
& if (!File) {
& cerr << "File error!" << endl;
& return 0;
& } else {
& string line;
& while (!File.eof()) {
& getline(File, line);
& cout << line << endl;
& }
& File.close();
& }
& getchar();
& getchar();

& break;
& }
& case 3: {
& cout << "Enter input file name: ";
& cin >> fileName;
& cout << "Enter output file name: ";
& string out;
& cin >> out;
& File.open(fileName.c_str(), ios::in);
& fstream outFile(out.c_str(), ios::out);
& if (!File) {
& cerr << "File error!" << endl;
& return 0;
& } else {
& string line;
& while (!File.eof()) {
& getline(File, line);
& stoupper(line);
& outFile << line << endl;
& }
& File.close();
& }
& 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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS