Thread: I need a hint.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    18

    I need a hint.

    How would I get this to work?

    DISCUSSION: Use the fstream library and the class types, ifstream and ofstream, to declare input and output files. Control the input loop with eof(). During each pass through the loop your program will read an ID and zero to four lap times for each race car. The lap times are terminated with a -1. Your program must read in the lap times by nesting a sentinel (looking for -1) controlled loop inside the eof controlled loop. If a racecar did not complete four laps, it wrecked or had mechanical problems.


    INPUT: The input file is named "race1.dat" The data file consists of an unknown number of racecar entries organized so that the information for each vehicle displays an ID number and zero to four lap times measured in seconds. The end of the lap times is marked with a “-1”. The following sample file illustrates how the data are organized. The file is terminated with a hidden <eof> character sequence.

    345676
    21
    17
    19
    22
    -1
    456671
    24
    25
    -1 <eof>

    OUTPUT: Your program should generate an output file called “race1.out". Remember to label your output. Example:

    Vehicle ID Average Speed (Mph) Status
    --------------------------------------------------------------
    345676 182.3 Mph Qualified
    456671 _ DNF


    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<conio>
    #include<string>
    
    using namespace std;
    
    
    const int sentinel = -1;
    
    int main(){
      
    int numin;
    ifstream input;
    ofstream out;
    
    const int eof = -1;
    
    int id;
    int l1;
    int l2;
    int l3;
    int l4;
    
    int race[4];
    
    race[0] = id;
    race[1] = l1;
    race[2] = l2;
    race[3] = l3;
    race[4] = l4;
    
    input.open("race1.dat");
    out.open("raceresults.txt");
    
    out << "Vehicle Id     Average Speed (Mph)     Status" << endl;
    out << "*********************************************" << endl;
    
    int i = 0;
    
    for (i=0; i>30; i++){
    while (!input.eof())
    {
      do  
      {
         
    input >> numin >> race[0];
    //input >> numin >> race[1];
    //input >> numin >> race[2];
    //input >> numin >> race[3];
    //input >> numin >> race[4]; 
      }
      while (numin != sentinel);
    cout << race[0] << endl;
    //cout << race[1] << endl;
    //cout << race[2] << endl;
    //cout << race[3] << endl;
    //cout << race[4] << endl;
    
    
    	
    out << race[0] << endl;
    //out << race[1] << endl;
    //out << race[2] << endl;
    //out << race[3] << endl;
    //out << race[4] << endl;
      
    //break;
      
    }
    }
    
    	
    input.close();
    out.close();
    
    return 0;
    }
    
    
    
    //***************************

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well what part are you having trouble with? From what I read all you need to do is read the contents of a file in and every time there is a -1 increment a variable and if that variable doesn't equal 4 then it crashed or whatever, and the output needs to be just some of the things you read into memory? That shouldn't be too hard, maybe try a little bit and you could get it.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Read the FAQ to see why you should never use eof() to control an input loop. The correct way of doing it is also shown there.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int id;
    int l1;
    int l2;
    int l3;
    int l4;
    
    int race[4];
    
    race[0] = id;
    race[1] = l1;
    race[2] = l2;
    race[3] = l3;
    race[4] = l4;
    Um, you know that id and l1 and l3 etc are undefined?
    Code:
    for (i=0; i>30; i++){
    That will loop exactly zero times.

    Why is race an aray?

    Read the FAQ to see why you should never use eof() to control an input loop.
    I agree. It's right here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    This looks like it's for a programming competition question. USACO, AIO, etc.

    With those ones they're not looking at good programming techniques, rather at the way you solve it. Yes, you shouldn't use eof(), but that's what they want you to use, so you risk getting marked wrong if you don't.


    Anyway, 1. Why have you included string, iomanip and conio? With the stuff you've used all you need is fstream and iostream.

    2.
    Code:
    for (i=0; i>30; i++){
    Looks like a typo, should be
    Code:
    for (i=0; i<30; i++){
    3. Remember that when you write a new value to a variable (including in an array) it overwrites anything already in it, so if you haven't stored it elsewhere, outputted it, or done anthything with it then it's lost, so I hope you didn't need it (hint, think lap times and +=)

    4. In the output file it shows average speed (I'm guessing), however the only input is seconds for each lap, and we're not told how long each lap is, how are you supposed to get the speed?

    5. What exactly is the problem you're having? The code is clearly wrong, but what in particular did you need help with? And don't say all of it, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need hint how to filter the results
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 01-29-2005, 06:27 PM
  2. hi! could anybody help me, a hint at the least
    By alvarorahul in forum C Programming
    Replies: 1
    Last Post: 07-07-2004, 02:37 AM
  3. Just a HINT
    By Diceman in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2003, 05:27 PM
  4. i need hint...
    By jk81 in forum C Programming
    Replies: 3
    Last Post: 09-12-2002, 08:38 PM
  5. Programming hint required
    By abhijit in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 04:03 AM