1. Printout of your C++ program with a heading comment (Do not forget to add com
ID: 3695309 • Letter: 1
Question
1. Printout of your C++ program with a heading comment (Do not forget to add comments
within your program). You must use both input and output files in order to receive any credit.
(6 points)
2. Printout of an output file, and screenshot of your program. (4 points)
/*
ELEN 1301-01 Programming Assignment #13
Name : Your name
Student ID : Your student ID#
Due date : April 28, 2016
Objective of this assignment :
Read a set of words from the input file. Generate a sentence using a
random number generator. Store your conversation with the program to the
output file.
Step 1.
Open an input and an output file.
Step 2.
Read contents (subject, verb, possessive, object) from the input file
(PA13.txt). You are free to modify and create your own word set.
Step 3.
Your program accepts a string from a keyboard.
Step 4.
Randomly generate a sentence using the words read from the input file. You
may include ctime library, and use srand(time(NULL)) for your random
number seed.
Step 5.
Repeat step 3 and 4 a few times while recording the conversation to the
output file.
Step 6.
Close both input and output files.
Explanation / Answer
//c++ :
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream File;
File.open("word.txt");
string Sub[20];
string verb[20];
string obj[20];
string arr[100];
ofstream myfile;
myfile.open ("output.txt");
int i=0,j=0,k=0,l=0;
while(!File.eof())
{
File>>arr[i];
if(arr[i]=="Subject")
File>>Sub[j++];
else if(arr[i]=="verb")
File>>verb[k++];
else if(arr[i]=="object")
File>>obj[l++];
i++;
}
//printing List of Object
/*for(int i=0;i<20;i++)
if(obj[i]!="")
cout<<obj[i]<<endl;
*/
srand(time(NULL));
myfile<<Sub[rand()%j]<<" "<<verb[rand()%k]<<" "<<obj[rand()%l]<<endl;
return 0;
}
//word.txt :input file
Subject Ram
Subject Shayam
Subject Mohan
Subject Rames
Subject Shiva
Subject Shambhu
verb play
verb eat
verb read
verb cook
object cricket
object football
object food
object vegetable
//output.txt
Mohan eat football
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.