In: Computer Science
Write the non modular version of this solution. Start with the implementation provided in proj6-sequentialMain2.cpp and implement the functionality found in proj6-modularMain2.cpp. Name your source code proj6-sequentialMain3.cpp
proj6-sequentialMain2.cpp:
int main() { string name; int age; string choice = "yes"; while( choice != "no" ) { cout << "What is your name? " << endl; getline( cin, name ); cout << “How old are you? " << endl; cin >> age; cout << "Your name is: " << name << endl; cout << "You are " << age << “ years old” << endl; cout << "Do you want to read in another person (yes/no)? " << endl; cin >> choice; cin.ignore(3, '\n'); } return 0; }
proj6-modularMain2.cpp:
void readPerson( istream &in, string &name, int &age ) { getline( in, name ); in >> age; in.ignore(3, '\n'); } void requestInfo( ostream &out ) { out << "Please enter your name followed by your age." << endl; } void writePerson( ostream &out, string name, int age ) { out << name << endl; out << age << endl; } string readAgain( ostream &out, istream &in ) { string choice; out << "Do you want to read in another person (yes/no)? " << endl; in >> choice; in.ignore(3, '\n'); return choice; } int main() { string name, choice; int age; bool fileRead = false; ifstream pfile; ofstream opfile; cout << "Would you like to read the data from a file? " << endl; cin >> choice; cin.ignore(3, '\n'); if( choice == "yes" ) { fileRead = true; pfile.open("iperson.txt"); if( !pfile ) { cerr << "Can't open person.txt for read." << endl; return 1; } } opfile.open("operson.txt"); choice = "yes"; if( fileRead ) { readPerson( pfile, name, age ); if( !pfile ) { choice = "no"; } } while( choice != "no" ) { if( !fileRead ) { requestInfo( cout ); readPerson( cin, name, age ); } writePerson( opfile, name, age ); if( !fileRead ) { choice = readAgain( cout, cin ); } else { readPerson( pfile, name, age ); if( !pfile ) { choice = "no"; } } } if( fileRead ) { pfile.close(); } opfile.close(); return 0; }
C++ PROGRAM
#include <iostream>
#include <fstream>
using namespace std;
int main() {//main
string name, choice,c;
int age;
bool fileRead = false;
ifstream pfile;
ofstream opfile;
cout << "Would you like to read the data from a file? "
<< endl;
cin >> choice;
cin.ignore(3, '\n');
if( choice == "yes" ) {
fileRead = true;
pfile.open("iperson.txt");
if( !pfile ) {
cerr << "Can't open person.txt for read." <<
endl;
return 1;
}
}
opfile.open("operson.txt");
choice = "yes";
if( fileRead ) {
pfile>>name;
pfile>>age;
if( !pfile ) {
choice = "no";
}
}
while( choice != "no" ) {//while loop
if( !fileRead ) {
cout<< "Please enter your name followed by your age."
<< endl;
cin>>name;
cin>>age;
}
opfile<< name << endl;
opfile<< age << endl;
if( !fileRead ) {
cout<< "Do you want to read in another person (yes/no)? "
<< endl;
cin>>choice;
} else {
pfile>>name;
pfile>>age;
if( !pfile ) {
choice = "no";
}
}
}
if( fileRead ) {
pfile.close();
}
opfile.close(); //close file
return 0;
}
iperson.txt
Harry 15
Smith 25
Rohan 39
operson.txt
jeet
29
Robert
24
OUTPUT