2nd part of c++ assaignment

This is my code

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int ReadProjectTasks(int EventList[],int TaskList[],int DaysList[],int size);
int DisplayMenu(int EventList[],int TaskList[],int DaysList[],int size);
void DisplayAllTasks(int EventList[],int TaskList[],int DaysList[],int size);
int ComputeLongestEvent(int EventList[],int TaskList[], int DaysList[],int size);
double ComputeEventTime(int EventList[],int TaskList[], int DaysList[],int size,int e);
int ComputeProjectTime(int EventList[],int TaskList[], int DaysList[],int size);
int main()
{
const int NumOfTasks = 100;
int EL[NumOfTasks];
int TL[NumOfTasks];
int DL[NumOfTasks];
int ch,evt;
int x=ReadProjectTasks(EL,TL,DL,NumOfTasks);
cout<<"Data about "<<x<<" tasks were saccessfully read from the input file"<<endl;
cout<<"*********************************************************************"<<endl;
ch=DisplayMenu(EL,TL,DL,x);

while (ch>=1 && ch<5)
{

if (ch==1){
cout<<setw(20)<<"Event"<<setw(20)<<"Task"<<setw(20)<<"Number of days"<<endl;
cout<<setw(18)<<"*****************************************************"<<endl;
DisplayAllTasks(EL,TL,DL,x);
}
else if (ch==2){
ComputeLongestEvent(EL,TL,DL,x);
}
else if (ch==3) {
cout<<"Enter event number ";
cin>>evt;
int m=ComputeEventTime(EL,TL,DL,x,evt);
cout<<"Event number "<<EL[evt]<<" can be completed in "<<m<<" days"<<endl;
}
else if (ch==4) {
cout<<" The project can be finished in "<<ComputeProjectTime(EL,TL,DL,x)<<" days"<<endl;
}
else{
cout<<"Goodbye!"<<endl;
}

DisplayMenu(EL,TL,DL,x);
}
return 0;
}
int ReadProjectTasks(int EventList[],int TaskList[],int DaysList[],int size)
{
ifstream fin;
fin.open("project.txt");
if(fin.fail()){
cout<<"Problem in the file"<<endl;
return 1;
}
int k=0;


fin>>EventList[k]>>TaskList[k]>>DaysList[k];
while(!fin.eof()){

k++;

fin>>EventList[k]>>TaskList[k]>>DaysList[k];
}
fin.close();


return k;
}

int DisplayMenu(int EventList[],int TaskList[],int DaysList[],int size)
{
int choice;

cout<<"\t1- Display all tasks"<<endl;
cout<<"\t2- Find the event with longest duration"<<endl;
cout<<"\t3- Compute the time to finish a given event"<<endl;
cout<<"\t4- Compute project completion time"<<endl;
cout<<"\t5- Exit program"<<endl;
cout<<" Enter your choice "<<endl;
cin>>choice;
return choice;
}
void DisplayAllTasks(int EventList[],int TaskList[],int DaysList[],int size)
{
cout<<setw(20)<<"Event"<<setw(20)<<"Task"<<setw(20)<<"Number of days"<<endl;
cout<<setw(20)<<"*****************************************************"<<endl;
for(int i=0;i<size;i++)
cout<<setw(20)<<EventList[i]<<setw(20)<<TaskList[i]<<setw(20)<<DaysList[i]<<endl;
}
double ComputeEventTime(int EventList[],int TaskList[], int DaysList[],int size,int e)
{
int max=0;
for(int i=0;i<size;i++)
if(EventList[i]==e)
if(max<DaysList[i]) max=DaysList[i];
if(max==-1)
return 0;
else
return max;

}
int ComputeLongestEvent(int EventList[],int TaskList[], int DaysList[],int size)
{
int e;
double longestday= ComputeEventTime(EventList,TaskList,DaysList,size,e);
cout<<"The event with the longest duration is ";
for(int i=0;i<size;i++)
{
if (DaysList[i]>longestday)
return EventList[i];
}
}
int ComputeProjectTime(int EventList[],int TaskList[], int DaysList[],int size)
{ int e;
int sum=0;
for(int i=0;i<size;i++)
sum=sum+ComputeEventTime(EventList,TaskList,DaysList,size,e);

return sum;
}



this is the file which is related to the assaigmant



event list task list daylist
1 15 3
1 27 6
1 36 4
2 15 5
3 18 4
3 26 1
4 15 2
4 26 7
4 27 7
5 16 4








You might want to consider using std::vector<> to store your values in rather than arrays.

Also at the moment you are passing arrays into the ReadProjectTasks() function by *value* rather than by *reference*.

That means that when the function exits, all the data will be deleted from them.

The spec says that you don't know the size of the arrays before hand. Really the best thing to do is use std::vector instead like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * Pass in our std::vector's by reference (using &) so that
 * the function does not copy them and fill its internal
 * copies rather than the std::vector's that we pass in.
 *
 * Also there is no need to return the size because each std::vector
 * holds its own size which can be tested like this: EventList.size();
 *
 * So the return value is 0 on success and 1 on failure.
 */
int ReadProjectTasks(std::vector<int>& EventList
	, std::vector<int>& TaskList
	, std::vector<int>& DaysList)
{
	std::ifstream fin;
	fin.open("project.txt");
	if (fin.fail())
	{
		cout << "Problem in the file" << endl;
		return 1; // error
	}

	int event;
	int task;
	int day;

	fin >> event >> task >> day;

	EventList.push_back(event);
	TaskList.push_back(task);
	DaysList.push_back(day);

	while (!fin.eof())
	{
		fin >> event >> task >> day;

		EventList.push_back(event);
		TaskList.push_back(task);
		DaysList.push_back(day);
	}
	fin.close();

	return 0; // success
}
And I strongly recommend working on one unit at a time. Get the input function working properly first and then more on to another part.
Thnx a lot 4 helping us but the instructor told us that we hv to solve it by using arrays!
,so how we can do it? can u explain more?

thnx again ..
Oh, okay. Then arrays it is then.

By the way I was wrong about the arrays being deleted on exit from the function, they are only passing the pointer so its all good and works as you originally expected.

So what is the problem with your code? Is it misbehaving in some way?
I'm going to post your code in between [code] tags so that it will be better formatted.

I have found one mistake:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int ReadProjectTasks(int EventList[], int TaskList[], int DaysList[], int size);
int DisplayMenu(int EventList[], int TaskList[], int DaysList[], int size);
void DisplayAllTasks(int EventList[], int TaskList[], int DaysList[], int size);
int ComputeLongestEvent(int EventList[], int TaskList[], int DaysList[],
	int size);
double ComputeEventTime(int EventList[], int TaskList[], int DaysList[],
	int size, int e);
int ComputeProjectTime(int EventList[], int TaskList[], int DaysList[],
	int size);

int main()
{
	const int NumOfTasks = 100;
	int EL[NumOfTasks];
	int TL[NumOfTasks];
	int DL[NumOfTasks];
	int ch, evt;
	int x = ReadProjectTasks(EL, TL, DL, NumOfTasks);
	cout << "Data about " << x
		<< " tasks were saccessfully read from the input file" << endl;
	cout
		<< "*********************************************************************"
		<< endl;
	ch = DisplayMenu(EL, TL, DL, x);

	std::cout << "bug: " << "ch = " << ch << std::endl;

	while (ch >= 1 && ch < 5)
	{

		if (ch == 1)
		{
			cout << setw(20) << "Event" << setw(20) << "Task" << setw(20)
				<< "Number of days" << endl;
			cout << setw(18)
				<< "*****************************************************"
				<< endl;
			DisplayAllTasks(EL, TL, DL, x);
		}
		else if (ch == 2)
		{
			ComputeLongestEvent(EL, TL, DL, x);
		}
		else if (ch == 3)
		{
			cout << "Enter event number ";
			cin >> evt;
			int m = ComputeEventTime(EL, TL, DL, x, evt);
			cout << "Event number " << EL[evt] << " can be completed in " << m
				<< " days" << endl;
		}
		else if (ch == 4)
		{
			cout << " The project can be finished in " << ComputeProjectTime(
				EL, TL, DL, x) << " days" << endl;
		}
		else
		{
			cout << "Goodbye!" << endl;
		}

		ch = DisplayMenu(EL, TL, DL, x); // ch = was missing!!
	}
	return 0;
}

int ReadProjectTasks(int EventList[], int TaskList[], int DaysList[], int size)
{
	ifstream fin;
	fin.open("project.txt");
	if (fin.fail())
	{
		cout << "Problem in the file" << endl;
		return 1;
	}
	int k = 0;

	fin >> EventList[k] >> TaskList[k] >> DaysList[k];
	while (!fin.eof())
	{

		k++;

		fin >> EventList[k] >> TaskList[k] >> DaysList[k];
	}
	fin.close();

	return k;
}

int DisplayMenu(int EventList[], int TaskList[], int DaysList[], int size)
{
	int choice;

	cout << "\t1- Display all tasks" << endl;
	cout << "\t2- Find the event with longest duration" << endl;
	cout << "\t3- Compute the time to finish a given event" << endl;
	cout << "\t4- Compute project completion time" << endl;
	cout << "\t5- Exit program" << endl;
	cout << " Enter your choice " << endl;
	cin >> choice;
	return choice;
}
void DisplayAllTasks(int EventList[], int TaskList[], int DaysList[], int size)
{
	cout << setw(20) << "Event" << setw(20) << "Task" << setw(20)
		<< "Number of days" << endl;
	cout << setw(20) << "*****************************************************"
		<< endl;
	for (int i = 0; i < size; i++)
		cout << setw(20) << EventList[i] << setw(20) << TaskList[i] << setw(20)
			<< DaysList[i] << endl;
}
double ComputeEventTime(int EventList[], int TaskList[], int DaysList[],
	int size, int e)
{
	int max = 0;
	for (int i = 0; i < size; i++)
		if (EventList[i] == e)
			if (max < DaysList[i])
				max = DaysList[i];
	if (max == -1)
		return 0;
	else
		return max;

}
int ComputeLongestEvent(int EventList[], int TaskList[], int DaysList[],
	int size)
{
	int e;
	double longestday =
		ComputeEventTime(EventList, TaskList, DaysList, size, e);
	cout << "The event with the longest duration is ";
	for (int i = 0; i < size; i++)
	{
		if (DaysList[i] > longestday)
			return EventList[i];
	}
}
int ComputeProjectTime(int EventList[], int TaskList[], int DaysList[],
	int size)
{
	int e;
	int sum = 0;
	for (int i = 0; i < size; i++)
		sum = sum + ComputeEventTime(EventList, TaskList, DaysList, size, e);

	return sum;
}


Line 74 was wrong as you forgot to assign the user's choice to the variable ch!
Last edited on
Topic archived. No new replies allowed.