Answer to Question #51090 in C++ for Zexy

Question #51090
6. a. Write a program that lets a user enter a five-line limerick and save it in a file. Save the
file as SaveLimerick.cpp.
b. Write another program that reads the file and displays the limerick on the screen. Save
the file as DisplayLimerick.cpp.
1
Expert's answer
2015-03-04T02:41:05-0500
.........................................................................................
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <fstream> // for file
using namespace std;
const string FILE_NAME = "output.txt";
int main(){
string s[5];
for (int i = 0; i < 5; i++){
cout<<"Enter "<<i+1<<" limerick : ";
cin.clear();
cin.sync();
getline(cin,s[i]);
}
ofstream fout(FILE_NAME);
for (int i = 0; i < 5; i++)
fout<<s[i]<<"\n";
cout<<"Limericks were written into file..."<<endl;
system&#40;"pause"&#41;;
return 0;
}
........................................................................................
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <fstream> // for file
using namespace std;
const string FILE_NAME = "output.txt";
int main(){
//These variables are used to read from file
char str_term[256];
//Reading file:
ifstream fin(FILE_NAME); // open file for reading
// if the file 'input' is missing then display error message
if (!fin.is_open())
{
cout<<"Error!!Input file cannot be found !!"<<endl;
}
//if the file 'input' is found
else
{
cout<<"Limericks from file : "<<endl;
while(!fin.eof())
{
fin.getline(str_term, 256); // read line from file
cout<<&str;_term[0]<<endl;
}
}

system&#40;"pause"&#41;;
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