Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment uses functions, files, and strings. Enough flexibility is provid

ID: 3712682 • Letter: T

Question

This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution.
Develop a functional flowchart and then write a C++ program to solve the following problem.


1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening the file, and testing the open success is done in a userdefined function. The function communicates the status of file open to the calling function).

2. Create a text file named c2.txt and write your computer model in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the model of your computer from the keyboard. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)

3. Create a text file named myComputer.txt and write the brand of your computer from the file c1.txt, and the model from file c2.txt. You will be reading the name of the file from the keyboard as a string, using the string class. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)

4. At this time, each file will have one line of text in it.

5. Prepare the files for reading.

6. Display the results of the three files in the order shown below:


c1.txt   your brand of computer as written in the c1


c2.txt   your model of the computer as written in the c2


myComputer.txt your computer brand and the model as written in myComputer


Note that there is a single space between the Computer brand and the computer model when you display the contents of the file myComputer.txt.

You will use a user-defined function to display all the files at once or one at a time.

7. Close all files. You will use a user-defined function that closes each file separately or all at once.

8. Run the program again and use your choice of the filenames, and different names, different than what you used above and repeat the process.

6. NO global declarations of VARIABLES allowed in any program that you develop in this course.

11. Do not use <stdio.h> and <conio.h> in this assignment and all other assignments.

12. Do not use any #define in your program until the time that is required for class declaration header files.

13. No goto statements allowed in any program that you develop in this course.

Explanation / Answer

SOurceCode:

#include <iostream>

#include <fstream>

#include <sstream>

#include <cstdlib>

#include <string>

#include <cmath>

#include <limits>

using namespace std;

int file_open(string const &filename, fstream &m_file );

int main() {

//brand file creation

cout << "Please give filename for computer brand"<<endl;

string computer_brand_file;

cin >> computer_brand_file;

cout << "Please Enter computer brand"<<endl;

string computer_brand;

cin >> computer_brand;

fstream brand_fs;

if( file_open(computer_brand_file, brand_fs ) ){

brand_fs << computer_brand;

brand_fs.close();   

}

else{

cout << "computer brand file is NOT opened" <<endl;

return(1);

}

//model file creation

cout << "Please give filename for computer model"<<endl;

string computer_model_file;

cin >> computer_model_file;

cout << "Please Enter computer model"<<endl;

string computer_model;

cin >> computer_model;

fstream model_fs;

if( file_open(computer_model_file, model_fs ) ){

model_fs << computer_model;

model_fs.close();   

}

else{

cout << "computer model file is NOT opened" <<endl;

return(1);

}

//wite computer Info file

cout << "Please give filename for computer Information"<<endl;

string computer_info_file;

cin >> computer_info_file;

fstream computer_info_fs;

if( file_open(computer_info_file, computer_info_fs ) ){

fstream brand_fs_read;

fstream model_fs_read;

file_open(computer_brand_file, brand_fs_read );

file_open(computer_model_file, model_fs_read );

string line;

getline(brand_fs_read,line);

computer_info_fs << line << " ";

getline(model_fs_read,line);

computer_info_fs << line ;

computer_info_fs.close();

model_fs_read.close();

brand_fs_read.close();

}

else{

cout << "computer Info file is NOT opened" <<endl;

return(1);

}

//diplay content of file

cout << "display file " <<computer_model_file <<endl;

std::ifstream f(computer_model_file.c_str());

if (f.is_open())

cout << f.rdbuf();

f.close();

cout << "display file " <<computer_brand_file <<endl;

std::ifstream f1(computer_brand_file.c_str());

if (f1.is_open())

cout << f1.rdbuf();

f1.close();

cout << "display file " <<computer_info_file <<endl;

std::ifstream f2(computer_info_file.c_str());

if (f2.is_open())

cout << f2.rdbuf();

f2.close();

return(0);

}

int file_open(string const &filename, fstream &fs ){   

fs.open (filename.c_str(), fstream::in | fstream::out | fstream::app);

return fs.is_open();  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote