Hi guys,
I'm still writing programs to read from files, and I really need a little help.
Well I need to read this info
Event task days
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
My problem is that I need to output to the screen:
1 3
2 1
3 2
4 3
5 1
The first colum list every event, and the next one how many task within the event.
Could you tell me the best way to do this?
Here's my code option 3 is where I'm having problems.
Also my menu selection won't let me go back after selecting an option please could you tell me what I'm doing wrong I will appreciate it
Thank
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int sum(0), icount(1), oldevent(0);
int count(0),event, next_event, task, next_task, days, max_days;
char selection;
bool bstart;
ifstream cpData;
do {
system("cls"); // Clear the screen
cout << " Dilmer Valecillos CS1600-001 Project # 3 \n\n" << endl;
cout<< " ---------------------------" ; // Menu Selection
cout<<" \n | Critical Path Analysis |\n";
cout<< " ---------------------------\n\n";
cout<<" 1 <- Project Completion Timetable \n"
<<" 2 <- Tasks Longer than 5 Days\n"
<<" 3 <- Task per Event\n"
<<" 4 <- Quit\n\n";
cout<< " Choice : ";
cin.get(selection);
switch (selection)
{
case '1':
system("cls");
cout << " Dilmer Valecillos CS1600-001 Project # 3 \n\n" << endl;
cpData.open("cpData.txt"); // To open the file
if (cpData.fail())
{
cout <<" Error opening the file\n";
}
else
{
cout<<" =============================\n"; // Heading Information
cout<<" Project Completition Timeable"<< endl;
cout<<" =============================\n\n";
cout << " Event Task Days\n" << endl;
if (cpData)
{
cpData >> event >> task >> days; // reading from the file
max_days = days;
do
{
while (! cpData.eof())
{
cpData >> next_event >> next_task >> days;
if (event != next_event)
break;
else if (days > max_days)
{
max_days = days;
task = next_task;
}
}
sum = sum + max_days;
cout << setw(3) << event << setw(8) << task << setw(6) << max_days <<endl;
event = next_event;
task = next_task;
max_days = days;
}
while (! cpData.eof());
cout << "\n The total of Days is: " << sum << endl;
cout << " ---------------------"<< endl;
}
cpData.close(); //close the file
system("pause");
break;
case '2':
system("cls");
cout << " Dilmer Valecillos CS1600-001 Project # 3 \n" << endl; // Heading info
cpData.open("cpData.txt"); // Open the CpData File
if (cpData.fail())
{
cout <<" Error opening the file\n";
}
else
{
cout << "\n Task Requiring more than 5 Days " << endl; // This is the Heading Information
cout << " -------------------------------" << endl;
cout << "\n\n Event " << " Task " << " Days " << endl;
cout << " ----- " << " ---- " << " ---- " << endl;
cpData >> event >> task >> days ;
while ( !cpData.eof() ) // keep reading until end-of-file
{
cpData >> event>> task >> days;
if (days > 5 )
{
cout << setw(5) << event << setw(8)<< task << setw(7)<< days << endl;
}
}
cout << " ===================\n" << endl;
}
cpData.close();
system("pause");
break;
case '3':
system("cls");
cout << " Dilmer Valecillos CS1600-001 Project # 3 " << endl;
cpData.open("cpData.txt");
if (cpData.fail())
{
cout <<" Error opening the file\n";
}
else
{
cout << " \n\n Number of Tasks per event" << endl; // Heading Info
cout << " =========================\n" << endl;
cout << " Event " << " Task " << endl;
cout << " ===== " << " ====" << endl;
cpData >> event >> task >> days;
oldevent = event;
count = task;
while (! cpData.eof())// It reads until the last line
{
cpData >> event >> task >> days;
if (bstart == true)
{
icount++;
oldevent = event;
bstart = false;
}
if ( oldevent == event )
{
icount++;
oldevent = event;
}
else
{
cout << setw(5) <<oldevent << setw(7) << icount << endl;
icount = 1;
oldevent = event;
}
}
}
cpData.close();
system("pause");// Pause to let the user see the output to the screen
break;
case '\n':
break;
case '4':
system("cls");
break;
default:
cout << '\a'; //sound the alarm for invalid input!!
}
}while( selection != '4');
return 0;
}