Reading File Help Please!

This is a discussion on Reading File Help Please! within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm still writing programs to read from files, and I really need a little help. Well I need ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Reading File Help Please!

    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;	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    The first colum list every event, and the next one how many task within the event.
    How can the same event have varying number of tasks?
    In other words, what does it mean to have repeated event numbers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...and there is no event with less than 15 tasks, so how does this make sense:
    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.
    I can't help but notice that in the desired output all the task counts are less than 15.

    Put some running shoes on, go run around the block, and then come back and try to post something that is logical.
    Last edited by 7stud; 03-05-2006 at 01:09 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    while (! cpData.eof())
    You don't read from files like that unless you don't mind infinite loops. Use this format instead:

    while(...the read statement here...)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Have you heard of functions yet?
    Code:
      selection = ShowMenuAndReadResponse( );
      switch (selection) {
        case '1':
          doProjectTimeTable();
          break;
        // etc etc
      }
    > while (! cpData.eof())// It reads until the last line
    Actually, it doesn't - it appears to read the last line twice since you fail to do any error checking.
    So it fails, and you just re-process the last line over.
    See the FAQ for why eof() in a loop construct is bad.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by Dilmerv
    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?
    To get the best help, you really should explain your problem. At first glance there seems to be no correlation between your input and output data. You really should describe that all the output is doing is counting the number of times the first colum number is seen.

    Then you don't describe what the program is doing, so we can understand what problem you are having.

    Plesae add enough information so we can understand what you already know.


    Quote Originally Posted by Dilmerv
    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:
    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;  
            cout << "  =========================\n"     << endl;
            cout << "  Event " << " Task "              << endl;
            cout << "  ===== " << " ===="               << endl;
    		
            cpData >> event >> task >> days;  // read the first line
            oldevent = event;
            count = task;
    		
            while (! cpData.eof())// It reads until the last line
            {
                cpData >> event >> task >> days;  // read the next line
                                                  // what happened to the data from the first read?
                                                  // Move this line to the end of the loop
    			
                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"); // why call the operating system when a simple cin would pause the program?
    			
        break;
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21