Question

In: Computer Science

Write the non modular version of this solution. Start with the implementation provided in proj6-sequentialMain2.cpp and...

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;
}

Solutions

Expert Solution

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


Related Solutions

c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
write an essay. compare a traditional version of a fairy tale with a contemporary written version...
write an essay. compare a traditional version of a fairy tale with a contemporary written version and a recent film. please also mention references from where you take the data
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
Write a modular program to simulate the Game of Life and investigate the patterns produced by...
Write a modular program to simulate the Game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array; and still others may produce ‘gliders’ that detach themselves from the society and sail off into space! Since the game requires an array of cells that continually expands/shrinks, you would want to use multi-dimensional arrays and...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
how to write a cpp program that takes a number from a user, and print the...
how to write a cpp program that takes a number from a user, and print the sum of all numbers from one to that number on screen? using loop interation as basic as possible.
Write a DTD for this XML document such that the corrected version of the example is...
Write a DTD for this XML document such that the corrected version of the example is a valid XML document <?xml version="1.0"?> <computers> <item type = "Laptop" code = "Dell437">    <make>    <brand>Dell Inspiration</Brand>    <supplier id = "Dell"/>    </make>    <spec>    <cpu speed = "900"/>    <harddisk size = "12" units = "GB"/>    </Spec>    <price n = "950" units = "USD"/> </item> <item type = "Desktop" code = "Apple1679">    <make>    <brand>Apple iMac</brand>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT