64 491
Assignments Done
99,1%
Successfully Done
In September 2018

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.
Expert's answer
.........................................................................................
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions