Homework Help

Discussion in 'C++' started by Revman, Dec 7, 2003.

  1. Revman

    Revman Guest

    I'm having problems opening and reading from a file to test a Class.

    My diver main.cpp is fairly short but with a longer file open function

    // Project #4 -- Main/driver program


    #include "daytime.h"
    #include <string>
    #include <fstream>
    #include <iostream>

    using namespace std;

    void openFile (ifstream&);


    int main()
    {

    DayTime Day1, Day2;

    ifstream file;

    openFile(file);

    //DEBUG: test read from the file

    int one, two, three;

    file >> one >> two >> three;

    cout << one << two << three << endl;

    //DEBUG END!

    Day1 >> file;

    Day2 >> file;


    // Watch -- not intutitive.
    Day1 << Day2;

    Day2 << Day1;

    return 0;
    };



    //**************************************************************************
    *******
    // openFile Function
    //**************************************************************************
    *******

    // Precondition:
    // recieves inputstream

    // Postcondition:

    // Verifies user identified file exists, reprompts if necessary.
    // opens the file

    void openFile(/* in/out */ ifstream& anyFile)

    {
    string fileName;

    int choice = 0;

    // used to test if the file was opened suscessfully
    int test;

    do
    {
    test = 0; // assume file will be opened correctly

    do
    {
    cout << "Enter the name of the file to open." << endl;

    cin >> fileName;

    //Prompt for standard file extension(s), if omitted.
    cout << endl << "Choose the type of file to open." << endl;

    cout << "1 = TXT" << endl << "2 = DAT" << endl;

    cout << "3 = Entered file type with file name" << endl;

    cin >> choice;

    if (choice < 1 || choice > 3)
    {
    cout << "Error with file type selection." << endl;

    cout << "Restarting open file process" << endl;
    }
    }
    while (choice < 1 && choice > 3);

    // add file name extension, if required
    switch (choice)
    {
    case 1 : fileName = fileName + ".txt";
    break;

    case 2 : fileName = fileName + ".dat";
    break;

    default: break; // no action necessary already included
    };

    //try to open the file
    anyFile.open(fileName.c_str());

    cout << "Attempting to open file" << endl;

    if (!anyFile)
    {
    cout << "Error opening file" << endl;

    test = 1;

    anyFile.clear();
    }

    }
    while(test == 1);

    cout << "File open suscessfully" << endl;

    return;
    }


    My class slecification file has some overloaded operators, required by the
    project, is as follows:

    //**********************************************************
    //*** This is the decelaration file for the daytime class***
    //*** Specifications can be found in the daytime.cpp file***
    //**********************************************************

    #include<fstream>

    using namespace std;

    class DayTime
    {
    public:
    DayTime(); // default constructor

    // used to extract day hour minute to the class private members
    void operator>> (/* in */ ifstream& anyFile);


    void operator << (DayTime otherDayTime);
    // used to output to screen the dayTime


    // use to compare see if two insitances of DayTime are equal
    bool operator == (DayTime otherDayTime);

    // used to determine of one DayTime is less than another DayTime
    // Assumes Sunday< Monday< Tuesday< ... < Saturday
    bool operator < (DayTime otherDayTime);

    private:
    enum days {ERROR, Sun, Mon, Tue, Wen, Thu, Fri, Sat} day;

    int hour;

    int min;
    };

    The implemation file is as follows:

    //**************************************************************************
    *******
    //*** This is theimplementation file of DayTime class
    //**************************************************************************
    *******

    #include "daytime.h"
    #include <iostream>

    using namespace std;

    // private members of class

    // enum days {ERROR, Sun, Mon, Tues, Wen, Thu, Fri, Sat} day;

    // int hour;

    // int min;

    //Class declrations

    //**************************************************************************
    *******

    DayTime::DayTime() // default constructor

    // Precondition:
    // None

    //Postsondition:
    // Constructed, day set to ERROR, how and min set to "0"

    {

    day = ERROR;

    hour = 0;

    min = 0;

    }

    //**************************************************************************
    *******

    void DayTime::eek:perator>> (/* in */ ifstream& anyFile)

    // Precondition:
    // stream is in day/hour/min order
    //0 < Day < 8 , -1 < Hour < 24 , and -1 < Min < 60

    //Postcondition:

    // Day, hour, and minute are set

    {
    int inDay;

    int inHour;

    int inMin;

    anyFile >> inDay >> inHour >> inMin;

    //debug statement
    cout << inDay << inHour << inMin;

    switch (inDay) // use inDay to set enum day
    {
    case 1 : day = Sun;

    break;

    case 2: day = Mon;

    break;

    case 3: day = Tue;

    break;

    case 4: day = Wen;

    break;

    case 5: day = Thu;

    break;

    case 6: day = Fri;

    break;

    case 7: day = Sat;

    break;

    default: cout << "Error!" << endl;

    break;
    }

    hour = inHour;

    min = inMin;
    }

    //**************************************************************************
    *******

    void DayTime::eek:perator<<(DayTime otherDayTime)

    //Precondition:
    // DayTime has been set

    //Postcondition:
    // private members are displayed to the screen in

    // DDD HH:MM 'endl'
    //format

    {
    cout << day << " ";

    if (hour < 10)

    cout << "0";

    cout << hour << ":";

    if (min < 10)

    cout << "0";

    cout << min << endl;

    }

    //**************************************************************************
    *******

    bool DayTime::eek:perator==(/* in */ DayTime otherDayTime)

    //Precondition:
    // Current and otherDayTime are declared

    //Postcondition:
    // if DayTime == otherDayTime, returns true
    // else, returns false

    {
    return (day == otherDayTime.day &&
    hour == otherDayTime.hour &&
    min == otherDayTime.min);
    }


    //**************************************************************************
    *******

    bool DayTime::eek:perator <(/* in */ DayTime otherDayTime)

    //Precondition:
    // Current and otherDayTime are declared

    //Postcondition:
    // if DayTime < otherDayTime, returns true
    // else, returns false

    {
    if (day < otherDayTime.day)
    return true;

    if (day == otherDayTime.day &&
    hour < otherDayTime.hour)

    return true;

    if (day == otherDayTime.day &&
    hour == otherDayTime.hour &&
    min <otherDayTime.min)

    return true;

    return false;
    }

    What I recieve as output when debugging these files I get the following as
    output:


    Enter the name of the file to open.

    Choose the type of file to open.

    1 = TXT

    2 = DAT

    3 = Entered file type with file name

    Attempting to open file

    File open suscessfully

    15-858993460

    -858993460-858993460-858993460Error!

    -858993460-858993460-858993460Error!

    0 0-858993460:0-858993460

    0 0-858993460:0-858993460



    Any help is greately appericated.

    Revman
    Revman, Dec 7, 2003
    #1
    1. Advertising

  2. Revman

    Jack Klein Guest

    On Sun, 7 Dec 2003 13:46:29 -0600, "Revman" <> wrote in
    comp.lang.c++:

    > I'm having problems opening and reading from a file to test a Class.
    >
    > My diver main.cpp is fairly short but with a longer file open function


    First problem, you posted way too much code. Did you type all this
    without testing it? Do you know how to use the debugger that comes
    with your compiler? What have you attempted to find and fix the
    problem already?

    I'm not going to go through all your code, but I will point out
    something that jumped out at me:


    > void openFile(/* in/out */ ifstream& anyFile)
    >
    > {
    > string fileName;
    >
    > int choice = 0;
    >
    > // used to test if the file was opened suscessfully
    > int test;
    >
    > do
    > {
    > test = 0; // assume file will be opened correctly
    >
    > do
    > {

    [snip]
    > }
    > while (choice < 1 && choice > 3);


    The while expression above will ALWAYS be false. It is impossible for
    any value to be both less than 1 AND greater than 3 at the same time.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 8, 2003
    #2
    1. Advertising

  3. "Jack Klein" <> wrote in message
    news:...
    > On Sun, 7 Dec 2003 13:46:29 -0600, "Revman" <> wrote in
    > comp.lang.c++:


    > I'm not going to go through all your code, but I will point out
    > something that jumped out at me:

    <<snip>>
    > > while (choice < 1 && choice > 3);

    >
    > The while expression above will ALWAYS be false. It is impossible for
    > any value to be both less than 1 AND greater than 3 at the same time.


    Thanks, Jack. This is one of my top 10. For those who didn't think it was
    such a big deal, I offer this as an example of the kind of thing that comes
    up often in class.
    --
    Gary
    Gary Labowitz, Dec 8, 2003
    #3
  4. Revman

    Jack Klein Guest

    On Mon, 8 Dec 2003 06:38:47 -0500, "Gary Labowitz"
    <> wrote in comp.lang.c++:

    > "Jack Klein" <> wrote in message
    > news:...
    > > On Sun, 7 Dec 2003 13:46:29 -0600, "Revman" <> wrote in
    > > comp.lang.c++:

    >
    > > I'm not going to go through all your code, but I will point out
    > > something that jumped out at me:

    > <<snip>>
    > > > while (choice < 1 && choice > 3);

    > >
    > > The while expression above will ALWAYS be false. It is impossible for
    > > any value to be both less than 1 AND greater than 3 at the same time.

    >
    > Thanks, Jack. This is one of my top 10. For those who didn't think it was
    > such a big deal, I offer this as an example of the kind of thing that comes
    > up often in class.


    Yes, I've never written code like this... today! Of course it just so
    happens that I had today off, and didn't go to work.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 9, 2003
    #4
  5. Revman wrote:

    > I'm having problems opening and reading from a file to test a Class.
    >
    > My diver main.cpp is fairly short but with a longer file open function
    >
    > // Project #4 -- Main/driver program
    >
    >
    > #include "daytime.h"
    > #include <string>
    > #include <fstream>
    > #include <iostream>
    >
    > using namespace std;
    >
    > void openFile (ifstream&);
    >
    >
    > int main()
    > {
    >
    > DayTime Day1, Day2;
    >
    > ifstream file;
    >
    > openFile(file);
    >
    > //DEBUG: test read from the file
    >
    > int one, two, three;
    >
    > file >> one >> two >> three;
    >
    > cout << one << two << three << endl;
    >
    > //DEBUG END!
    >
    > Day1 >> file;
    >
    > Day2 >> file;

    I'm surprised nobody caught this one!.
    You have declared the file as an "ifstream", which
    is a read-only stream and yet here you are writing
    to it. You need to either open the file as bi-directional
    or close it and open it in write mode.


    > // Watch -- not intutitive.
    > Day1 << Day2;
    >
    > Day2 << Day1;
    >
    > return 0;
    > };

    [snip -- openfile function]


    > My class slecification file has some overloaded operators, required by the
    > project, is as follows:
    >
    > //**********************************************************
    > //*** This is the decelaration file for the daytime class***

    Spelling: "declaration" or "deceleration". One is to slow down
    the other to announce something. Guess which one?


    > //*** Specifications can be found in the daytime.cpp file***

    Specifications should be in the header file.


    > //**********************************************************
    >
    > #include<fstream>
    >
    > using namespace std;
    >
    > class DayTime
    > {
    > public:
    > DayTime(); // default constructor
    >
    > // used to extract day hour minute to the class private members
    > void operator>> (/* in */ ifstream& anyFile);
    >
    >
    > void operator << (DayTime otherDayTime);
    > // used to output to screen the dayTime
    >
    >
    > // use to compare see if two insitances of DayTime are equal
    > bool operator == (DayTime otherDayTime);
    >
    > // used to determine of one DayTime is less than another DayTime
    > // Assumes Sunday< Monday< Tuesday< ... < Saturday
    > bool operator < (DayTime otherDayTime);
    >
    > private:
    > enum days {ERROR, Sun, Mon, Tue, Wen, Thu, Fri, Sat} day;
    >
    > int hour;
    >
    > int min;
    > };

    If the hour and min variables can be negative then use "int",
    otherwise use an "unsigned int". This will allow the compiler
    to use more error checking.


    >
    > The implemation file is as follows:
    >
    > //**************************************************************************
    > *******
    > //*** This is theimplementation file of DayTime class
    > //**************************************************************************
    > *******
    >
    > #include "daytime.h"
    > #include <iostream>
    >
    > using namespace std;
    >
    > // private members of class
    >
    > // enum days {ERROR, Sun, Mon, Tues, Wen, Thu, Fri, Sat} day;
    >
    > // int hour;
    >
    > // int min;
    >
    > //Class declrations
    >
    > //**************************************************************************
    > *******
    >
    > DayTime::DayTime() // default constructor
    >
    > // Precondition:
    > // None
    >
    > //Postsondition:
    > // Constructed, day set to ERROR, how and min set to "0"
    >
    > {
    >
    > day = ERROR;
    >
    > hour = 0;
    >
    > min = 0;
    >
    > }

    Prefer an initialization list:
    DayTime::DayTime()
    : day(ERROR), hour(0), min(0)
    >
    > //**************************************************************************
    > *******
    >
    > void DayTime::eek:perator>> (/* in */ ifstream& anyFile)
    >
    > // Precondition:
    > // stream is in day/hour/min order
    > //0 < Day < 8 , -1 < Hour < 24 , and -1 < Min < 60
    >
    > //Postcondition:
    >
    > // Day, hour, and minute are set
    >
    > {
    > int inDay;
    >
    > int inHour;
    >
    > int inMin;
    >
    > anyFile >> inDay >> inHour >> inMin;
    >
    > //debug statement
    > cout << inDay << inHour << inMin;

    cout << "Day: " << inDay
    << ", Hour: " << inHour
    << ", Min: " << inMin
    << endl;


    >
    > switch (inDay) // use inDay to set enum day
    > {
    > case 1 : day = Sun;
    >
    > break;

    [snip -- many cases]
    The compiler can convert an enum value into an integer and
    an integer into an enum value, provided that the integer
    is within the range of an enum.
    day = inDay;

    Check out this article:
    http://www.embedded.com/story/OEG20030319S0021

    > default: cout << "Error!" << endl;
    >
    > break;
    > }
    >
    > hour = inHour;
    >
    > min = inMin;
    > }


    This could be simplified:
    anyFile >> (int) day >> hour >> min;

    >
    > //**************************************************************************
    > *******
    >
    > void DayTime::eek:perator<<(DayTime otherDayTime)
    >
    > //Precondition:
    > // DayTime has been set
    >
    > //Postcondition:
    > // private members are displayed to the screen in
    >
    > // DDD HH:MM 'endl'
    > //format
    >
    > {
    > cout << day << " ";
    >
    > if (hour < 10)
    >
    > cout << "0";
    >
    > cout << hour << ":";
    >
    > if (min < 10)
    >
    > cout << "0";
    >
    > cout << min << endl;
    >
    > }

    This could be simplified by using the width() and fill() functions:
    int saved_width;
    char saved_fill;

    saved_width = cout.width(2);
    saved_fill = cout.fill('0');
    cout << hour << ":" << min << endl;
    cout.width(saved_width);
    cout.fill(saved_fill);


    [snip -- definition of operator==()]

    > //**************************************************************************
    > *******
    >
    > bool DayTime::eek:perator <(/* in */ DayTime otherDayTime)
    >
    > //Precondition:
    > // Current and otherDayTime are declared
    >
    > //Postcondition:
    > // if DayTime < otherDayTime, returns true
    > // else, returns false
    >
    > {
    > if (day < otherDayTime.day)
    > return true;
    >
    > if (day == otherDayTime.day &&
    > hour < otherDayTime.hour)
    >
    > return true;
    >
    > if (day == otherDayTime.day &&
    > hour == otherDayTime.hour &&
    > min <otherDayTime.min)
    >
    > return true;
    >
    > return false;
    > }

    Try something like this:
    {
    if (day != otherDayTime.day)
    return day < otherDayTime.day;

    if (hour != otherDayTime.hour)
    return hour < otherDayTime.hour;

    return min < otherDayTime.min;
    }

    In essence, the hours only need to be compared if the day values
    are equal. Minutes only need to be compared if the hours are
    equal.


    [Example output snipped]

    > Any help is greately appericated.
    >
    > Revman



    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Dec 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    678
    Guest
    Feb 25, 2004
  2. Rhino

    need help with homework

    Rhino, Nov 16, 2003, in forum: Java
    Replies:
    1
    Views:
    305
    Roedy Green
    Nov 16, 2003
  3. Andrew Thompson
    Replies:
    33
    Views:
    892
    Alex Hunsley
    Oct 30, 2004
  4. xscarsofdesirex

    Homework Help! Urgent!

    xscarsofdesirex, Feb 27, 2005, in forum: Java
    Replies:
    5
    Views:
    533
    Alun Harford
    Feb 28, 2005
  5. Replies:
    2
    Views:
    411
    Roedy Green
    Oct 10, 2005
Loading...

Share This Page