Thread: Files and functions

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    Files and functions

    I am trying to understand this assignment. Im reading my book and the lecture notes the prof. puts online but I am stuck. Could you help me please?

    assignment:

    CSCI 1010 Programming Assignment 8

    A. Purpose
    Write a C++ program that uses files and a function (Read Chapter 11)
    B. Due Date: 8p.m. on Monday 11/20/06
    C. APSU-Rent-A-Car keeps information on each of its rental cars in a file named cars.dat. Each line of the file(cars.dat) consists of a stringthat is a three-symbol serial number for a car and the number of miles that car has been driven to date. Write a C++ program named a8YourLastname.cpp that reads information from cars.dat and produces two output files named under50.dat and 50plus.dat. under50.dat contains the serial numbers (one per line) of all cars that have under 50000 miles driven and 50plus.dat has the serial numbers(one per line) of all cars that have 50000 or more miles driven. An example of the three files is on page 269.

    The program has these additional requirements:
    1. The program must include a void function that is passed the file steam variable and the serial number. The function would write the serial number to the proper file. An example header for the function would be:
    void outputfile (ofstream& fout, string serial_number)

    The function would be passed from main the correct file stream variable associated with under50.dat or 50plus.dat and would write the passed serial number to the file.

    See pages 260-261 for information on passing file streams on parameter lists. Also see slides 20 and 21 on Chapter 7 slides.

    2. The main function will
    (1) Open files (i.e., cars.dat, under50.dat, 50plus.dat) for input and output.
    (2) Read information form the file cars.dat using an appropriate loop structure.
    (3) within the loop call the Outputfile function passing it the correct file stream based on the miles driven.
    (3) Close all the files.

    3. Coding style (clarity, indentation, comments, spelling, etc.)
    (1) Add comments including program’s name, purpose, and author.
    (2) Add comments for each variable and function.







    what i have so far:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void outputfile (ofstream & fileout, string serial);
    //function declaration
    
    int main ()
            {//int main open
                    fileout.open("cars.dat");
                    fileout.open("under50.dat");
                    fileout.open("50plus.dat");

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What is fileout?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    fileout is my stream

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Words are not enough (c)
    add code that tells that. (you should declare all variable you use)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by vart
    Words are not enough (c)
    add code that tells that. (you should declare all variable you use)

    what?

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Show us more.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what?
    The code snippet is incomplete. You are using fileout in main, but it has not been declared in main.

    Do you know how to declare a filestream and open a file? That would be task #1 inside main. You can figure out from the homework instructions whether you need an input stream or an output stream.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by manutd
    Show us more.
    This is why I came here. I do not know what to put.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by WinterInChicago
    what?
    When I want to use integer variable like this:
    Code:
    for(index = 0; index < 10; index++)
    it is not enough just to say that index is integer. Compiler cannot read my mind. I should write
    Code:
    int index;
    for(index=0;index<10;index++)
    And this is the first part that is missing in your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    So I need an int to pass the info in the file to, to pass to the void, to put in the if else to know which file to write what to? Is this correct?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    better?
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void outputfile (ofstream & filesout, string serial);
    //function declaration
    
    int main ()
            {//int main open
                    ifstream fileout;
                    ofstream below, above;
                    fileout.open("cars.dat");
                            if(fileout.fail())
                            {//if open
                                    cerr << "Unable to open file." << endl;
                                    abort();
                            }//if close
                    below.open("under50.dat");
                            if(below.fail())
                            {//if open
                                    cerr << "Unable to open file." << endl;
                                    abort();
                            }//if close
                    above.open("50plus.dat");
                            if(above.fail())
                            {//if open
                                    cerr << "Unable to open file." << endl;
                                    abort();
                            }//if close
                    while (! fileout.eof())
                    {//while loop open
    
                    }//while loop close
    
    
            }//int main close
    
    void outputfile (ofstream & filesout, string serial)
    ~
    oh, and the cars.dat file looks like this:

    A10 51029
    A20 14293
    A30 4123
    B10 61770
    B30 82001
    C05 9182
    C20 90910
    C30 100301
    Last edited by WinterInChicago; 11-19-2006 at 12:56 PM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's a good start. I'm not sure why you call your input file fileout, that is sort of confusing, and you should be checking is_open() instead of fail(), but otherwise it seems ok. Did you compile it and run it to test what you have so far?

    Now, step 2. Your current loop is while (! fileout.eof()) and is empty. First, using while (! fileout.eof()) is often a bad idea. If that is what your instructor taught, then it might work in this case, but if you are just trying that then you should change it as you work on this step.

    So now you have to actually read in data from the file. You know what the file looks like. There is a serial number string and an integer number of miles. So add code to your loop to read in a serial number string and the integer miles. To test that it is working, add a temporary bit of code that cout's the data to the console as it is being read.

    Once you've got that working you can move on to the next step.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void outputfile (ofstream & filesout, string serial);
    //function declaration
    
    int main ()
            {//int main open
                    int miles;
                    string serial;
                    ifstream fileout;
                    ofstream below, above;
                    fileout.open("cars.dat");
                            if(fileout.fail())
                            {//if open
                                    cerr << "Unable to open file." << endl;
                                    abort();
                            }//if close
                    below.open("under50.dat");
                    above.open("50plus.dat");
                    while (fileout >> ws && ! fileout.eof())
                            {//while loop open
                                    fileout >> serial >> miles;
                                    if (miles < 50000)
                                            outputfile (below, serial);
                                    else
                                            outputfile (above, serial);
                            }//while loop close
    
    
            }//int main close
    
    void outputfile (ofstream & filesout, string serial)
            {//void open
                    filesout << serial;
            }//void close
    Last edited by WinterInChicago; 11-21-2006 at 02:07 PM.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When calling a function, you don't put the return type, so remove the void from the lines giving the error, and then see how it goes.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i knew that... i dont know why i didnt catch it, thanks... but nowmy files arent writing correctly. the 50plus and 50below should be written like:

    A10
    B10
    B30
    C20
    C30

    but its writing the dat file like this:

    A10B10B30C20C30


    how to fix that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. separating functions into diff files
    By cuizy in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 03:50 PM
  2. Replies: 7
    Last Post: 02-01-2009, 04:53 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  4. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  5. functions & external files
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-24-2001, 03:13 AM