Thread: How to make it easier?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14

    How to make it easier?

    Requirements
    At the recent British Indoor Rowing Championships, twenty men contested the Open Heavyweight competition.

    The race takes place over the standard distance of 2000 metres and the time is recorded as each competitor passes 500, 1000, 1500 and 2000 metres.

    The winner is the rower that completes the 2000 metres in the fastest time.

    There is a further prize for the rower that completes any intermediate 500 metre segment of the race in the fastest time.

    The data for the results of the race is stored in the file "birc.txt".

    This data file is available for testing your programs from the following link: birc.txt

    Your program should:

    * define appropriate user-defined data types (enumerations and/or structs) to model the data of the application.
    * read the race data from the data file and store it in a convenient form.
    * perform a search through the data for the overall winner of the race and report the name and winning time of the winner.
    * perform a search through the data for the winner of the "fastest segment" prize and report the name of the winner, which segment he completed the fastest and the actual time taken to row the 500 metre segment.

    birc.txt
    http://download.yousendit.com/11EA79BD6FA3E952

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct Player
    {
      string name;
      string club;
      string age;
      string time1;
      double timeconv[4];
      string time2;
      string time3;
      string time4;
      double bestsegment;
      double total;
    };
    
    
    
    int main(int argc, char *argv[])
    {
    
      if (argc != 2)
        {
          cerr << "Usage ./a.out filename";
          exit(1);
        }
    
      int zonk;
      int fastestq, totaltimeq;
      double fastest= 0;
      double totaltime = 0;
      Player rower[100];
    
      ifstream myfile;
      myfile.open(argv[1]);
    
      string line;
      vector<string> linevec;
    
    if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          linevec.push_back(line);
        }
        myfile.close();
      }
    
    linevec.erase(linevec.begin());
    
     vector<int> scolon;
    
     // extracting data
     for (int z = 0; z < linevec.size()-1; z++)
       {
         for (int i = 0; i < linevec[z].size(); i++)
           {
    	 if (linevec[z][i] == ';')
    	   {
    	     scolon.push_back(i);
    	   }
           }
         
         // extracting names 
         for (int i = scolon[0]-1; i > 0; i--)
           if (!isspace(linevec[z][i]))
    	 {
    	   rower[z].name = linevec[z].substr(0,i+1);
    	   break;
    	 }
    
         //extracting clubs
         for (int i = scolon[1]-1; i > scolon[0]; i--)
           {
    	 if (!isspace(linevec[z][i]))
    	   {
    	     rower[z].club = linevec[z].substr(scolon[0]+1,i-scolon[0]);
    	     break;
    	   }
           }
    
    	 //extracting age
         for (int i = scolon[2]-1; i > scolon[1]; i--)
           {
    	 if (!isspace(linevec[z][i]))
    	   {
    	     rower[z].age = linevec[z].substr(scolon[1]+1,i-scolon[1]);
    	     break;
    	   }
           }
    
         //extracting time1
         for (int i = scolon[3]-1; i > scolon[2]; i--)
           {
    	 if (!isspace(linevec[z][i]))
    	   {
    	     rower[z].time1 = linevec[z].substr(scolon[2]+1,i-scolon[2]);
    	     rower[z].timeconv[0] = 60 * atof(rower[z].time1.substr(0,2).c_str())
    	       + atof(rower[z].time1.substr(3,4).c_str());
    
    	     break;
    	   }
           }
    
         //extracting time2
         for (int i = scolon[4]-1; i > scolon[3]; i--)
           {
    	 if (!isspace(linevec[z][i]))
    	   {
    	     rower[z].time2 = linevec[z].substr(scolon[3]+1,i-scolon[3]);
    	     
    	     rower[z].timeconv[1] = (60 * atof(rower[z].time2.substr(0,2).c_str())
    	       + atof(rower[z].time2.substr(3,4).c_str())) - rower[z].timeconv[0];
    
    	     break;
    	   }
           }
    
         //extracting time3
         for (int i = scolon[5]-1; i > scolon[4]; i--)
           {
    	 if (!isspace(linevec[z][i]))
    	   {
    	     rower[z].time3 = linevec[z].substr(scolon[4]+1,i-scolon[4]);
    	     rower[z].timeconv[2] = (60 * atof(rower[z].time3.substr(0,2).c_str())
    	       + atof(rower[z].time3.substr(3,4).c_str())) - rower[z].timeconv[1]
    	       - rower[z].timeconv[0];
    
    	     break;
    	   }
           }
    
         //extracting time4 and totaltime
         for (int i = scolon[5]+1; i < scolon[5]+7; i++)
           {
    	 rower[z].time4 = linevec[z].substr(scolon[5]+1, scolon[5]+6);
    	 rower[z].timeconv[3] = (60 * atof(rower[z].time4.substr(0,2).c_str())
    	   + atof(rower[z].time4.substr(3,4).c_str())) - rower[z].timeconv[2]
    	   - rower[z].timeconv[1] - rower[z].timeconv[0];
    	 rower[z].total = (60 * atof(rower[z].time4.substr(0,2).c_str())
    			   + atof(rower[z].time4.substr(3,4).c_str()));
    
    	 break;
           }
    
         
    
    
         // best segment
         rower[z].bestsegment = rower[z].timeconv[0];
         for (int i = 0; i < 4; i++)
           {
    	 if (rower[z].bestsegment >= rower[z].timeconv[i])
    	 {
    	 
    	   rower[z].bestsegment = rower[z].timeconv[i];
    	   zonk = i+1;
    	 }
           }
    	 
         //comparing best segments of each rower
         if (fastest == 0)
           fastest =  rower[0].bestsegment;
         if (fastest >= rower[z].bestsegment)
           {
           fastest = rower[z].bestsegment;
           fastestq = z;
           }
    
         //comparing final times
         if (totaltime == 0)
           totaltime = rower[0].total;
         if (totaltime >= rower[z].total)
           {
    	 totaltime = rower[z].total;
    	 totaltimeq = z;
           }
    
    
         scolon.clear();
       }
    
    
    
     cout << "Best time(winner)" << endl
          << "Name: " << rower[totaltimeq].name << endl
          << "Club: " << rower[totaltimeq].club << endl
          << "Time :" << totaltime << endl;
    
     cout << "Best segment time" << endl  
          << "Name: " << rower[fastestq].name << endl
          << "Club: " << rower[fastestq].club << endl
          << "Time: " << fastest << endl
          << "Segment: " << zonk << endl;
    
    }
    I think my way of doing it is a little bit overcomplicated, isnt it? I need any clues how to write it in a less complicated way...

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Everytime I try to download your input file, the site indicates that the server is too busy to handle my download request. It suggests that I try again later. I've attempted and failed to download it quite a few times.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM