Extend the structure definition in cpp file to use a structure rather than just
ID: 3732651 • Letter: E
Question
Extend the structure definition in cpp file to use a structure rather than just a string. Update the program to use this new structure and move all the structure definitions to a separate .h file
Create a .h file with the current structure definitions. Include that file in the cpp file and make sure that the program still compiles/runs.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct date
{
int month;
int day;
int year;
};
enum state {COMPLETED = 1, WORKING = 0};
struct project_Detailes
{
string projectName, description;
struct date startDate, endDate;
int expectedTime , timeExpended;
int pState;
};
void add()
{
ofstream outfile;
outfile.open("F:project.txt", ios::app);
struct project_Detailes p;
cout<<"Enter all the detailes"<<endl;
cout<<"Enter project name : ";
cin.ignore();
getline (std::cin,p.projectName);
cout<<" Enter Description : ";
getline (std::cin,p.description);
cout<<" Enter startDate -- ";
cout<<" Enter month : ";
cin>>(p.startDate).month;
cout<<" Enter day : ";
cin>>(p.startDate).day;
cout<<" Enter year : ";
cin>>(p.startDate).year;
cout<<" Enter end date -- ";
cout<<" Enter month : ";
cin>>(p.endDate).month;
cout<<" Enter day : ";
cin>>(p.endDate).day;
cout<<" Enter year : ";
cin>>(p.endDate).year;
cout<<" Enter expected time in minutes : ";
cin>>p.expectedTime;
cout<<" Enter time expended in minutes : ";
cin>>p.timeExpended;
cout<<" Enter 1-Completed 0-working";
cin>>p.pState;
outfile<<p.projectName<<","<<p.startDate.day<<"/"<<p.startDate.month<<"/"<<p.startDate.year<<","<<p.endDate.day<<"/"<<p.endDate.month<<"/"<<p.endDate.year;
outfile<<","<<p.expectedTime <<","<<p.timeExpended<<",";
switch(p.pState)
{
case COMPLETED:
outfile<<"Completed"<<endl;
break;
case WORKING:
outfile<<"Working"<<endl;
break;
}
outfile<<p.description<<endl;
outfile.close();
}
void display()
{
ifstream infile;
string str;
int i,j=1;
string contents[]={"Project Name","Start date","End date","Expected Date","Time Expended","Status"};
infile.open("F:project.txt");
while(!infile.eof()){
infile>>str;
if(str=="") return;
cout<<" ********* Project "<<j<<" Detailes ***********"<<endl;
int index=0;
for(i=0;i<str.length();i++)
{
if(i==0 || str[i]==','){
cout<<" "<<contents[index]<<" : ";
index++;
}
if(str[i]!=','){
cout<<str[i];
}
}
infile>>str;
j++;
cout<<" Description : "<<str;
cout<<" *******"<<endl;
}
infile.close();
}
void searchf()
{
ifstream infile;
int i;
infile.open("F:project.txt");
string str,out;
string contents[]={"Project Name","Start date","End date","Expected Date","Time Expended","Status"};
cout<<"Enter project Name to search for";
cin>>str;
while(!infile.eof()){
infile>>out;
if(out=="") return;
std::size_t found = out.find(str);
if (found!=std::string::npos){
cout<<"Search for project name - "<<str<<" is a success :"<<endl;
int index=0;
for(i=0;i<out.length();i++)
{
if(i==0 || str[i]==','){
cout<<" "<<contents[index]<<" : ";
index++;
}
if(out[i]!=','){
cout<<out[i];
}
}
infile>>out;
cout<<" Description : "<<out;
return;
}
infile>>out;
}
infile.close();
}
int main()
{
char ch;
while(true){
cout<<"Menu"<<endl;
cout<<"d - Display all current projects"<<endl;
cout<<"s - Look up a project based on name"<<endl;
cout<<"a - Add new project"<<endl;
cout<<"e - Exit"<<endl;
cout<<" Your choice ? "<<endl;
cin>>ch;
switch(ch)
{
case 'd':
display();
break;
case 's':
searchf();
break;
case 'a':
add();
cout<<"record added successfully"<<endl;
break;
case 'e':
exit(EXIT_SUCCESS);
break;
default:
cout<<"Please enter correct choice"<<endl;
break;
}
}
}
Explanation / Answer
1. Contents of header file structure.h
// Structure definitions
struct date
{
int month;
int day;
int year;
};
enum state { COMPLETED = 1, WORKING = 0 };
struct project
{
char projectName[20];
char description[20];
};
struct project_Detailes
{
struct project name;
struct date startDate, endDate;
int expectedTime, timeExpended;
int pState;
};
2. Contents of cpp file
// prog2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<bits/stdc++.h>
#include<fstream>
#include<string>
#include "structure.h"
using namespace std;
void add()
{
ofstream outfile;
outfile.open("F:project.txt", ios::app);
struct project_Detailes p;
cout << "Enter all the detailes" << endl;
cout << "Enter project name : ";
cin.ignore();
cin>> p.name.projectName;
cout << " Enter Description : ";
cin>>p.name.description;
cout << " Enter startDate -- ";
cout << " Enter month : ";
cin >> (p.startDate).month;
cout << " Enter day : ";
cin >> (p.startDate).day;
cout << " Enter year : ";
cin >> (p.startDate).year;
cout << " Enter end date -- ";
cout << " Enter month : ";
cin >> (p.endDate).month;
cout << " Enter day : ";
cin >> (p.endDate).day;
cout << " Enter year : ";
cin >> (p.endDate).year;
cout << " Enter expected time in minutes : ";
cin >> p.expectedTime;
cout << " Enter time expended in minutes : ";
cin >> p.timeExpended;
cout << " Enter 1-Completed 0-working";
cin >> p.pState;
outfile << p.name.projectName << "," << p.startDate.day << "/" << p.startDate.month << "/" << p.startDate.year << "," << p.endDate.day << "/" << p.endDate.month << "/" << p.endDate.year;
outfile << "," << p.expectedTime << "," << p.timeExpended << ",";
switch (p.pState)
{
case COMPLETED:
outfile << "Completed" << endl;
break;
case WORKING:
outfile << "Working" << endl;
break;
}
outfile << p.name.description << endl;
outfile.close();
}
void display()
{
ifstream infile;
string str;
int i, j = 1;
string contents[] = { "Project Name","Start date","End date","Expected Date","Time Expended","Status" };
infile.open("F:project.txt");
while (!infile.eof()) {
infile >> str;
if (str == "") return;
cout << " ********* Project " << j << " Detailes ***********" << endl;
int index = 0;
for (i = 0; i<str.length(); i++)
{
if (i == 0 || str[i] == ',') {
cout << " " << contents[index] << " : ";
index++;
}
if (str[i] != ',') {
cout << str[i];
}
}
infile >> str;
j++;
cout << " Description : " << str;
cout << " *******" << endl;
}
infile.close();
}
void searchf()
{
ifstream infile;
int i;
infile.open("F:project.txt");
string str, out;
string contents[] = { "Project Name","Start date","End date","Expected Date","Time Expended","Status" };
cout << "Enter project Name to search for";
cin >> str;
while (!infile.eof()) {
infile >> out;
if (out == "") return;
std::size_t found = out.find(str);
if (found != std::string::npos) {
cout << "Search for project name - " << str << " is a success :" << endl;
int index = 0;
for (i = 0; i<out.length(); i++)
{
if (i == 0 || str[i] == ',') {
cout << " " << contents[index] << " : ";
index++;
}
if (out[i] != ',') {
cout << out[i];
}
}
infile >> out;
cout << " Description : " << out;
return;
}
infile >> out;
}
infile.close();
}
int main()
{
char ch;
while (true) {
cout << "Menu" << endl;
cout << "d - Display all current projects" << endl;
cout << "s - Look up a project based on name" << endl;
cout << "a - Add new project" << endl;
cout << "e - Exit" << endl;
cout << " Your choice ? " << endl;
cin >> ch;
switch (ch)
{
case 'd':
display();
break;
case 's':
searchf();
break;
case 'a':
add();
cout << "record added successfully" << endl;
break;
case 'e':
exit(EXIT_SUCCESS);
break;
default:
cout << "Please enter correct choice" << endl;
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.