GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Apr-2007, 18:58
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

I need help with three C++ assignments asap please


Okay, I'm having a hard time with the three assignments I have left to do. I've been working on these for a week now and I'm getting no where. I asked the prof but she said to use the examples from the book which can help to some point but at the same time confuse me even more. The examples from the book didn't help much and I'm not entirely sure I'm doing it right since I can't get the programs to run. Can anyone help me on what I'm doing wrong and what I should be doing if I'm not already? To avoid confusing of what I'm doing the assignment is: Construct a Time class contaning interger data members, seconds, minutes, and hours. Have the class contain two constructors. The first should be default constructor having the prototype time(int, int, int), which uses default values 0 for each data member. The second constructor should accept a long integer representing a total number of seconds and disassemble the long integer into hours, minutes, and seconds. The final function member should display the class data members. and this is what I have done:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

class Time
{
  private:
    int seconds;
    int minutes;
    int hours;
  public:
   Time(int = 0, int = 0, int = 0);
   void setTime (int, int, int);
   void showTime(void);
};

Time::Time(int ss, int mm, int hr)
{
   seconds = ss;
   minutes = mm;
   hours = hr;
}

void Time::setTime(int ss, int mm, int hr)
{
   seconds = ss;
   minutes = mm;
   hours = hr;

   return;
}

  voidTime::showTime(void)
{
  cout << "The Time is ";
  cout << setfill('0')
         << setw(2) << seconds << ' / '
         << setw(2) << minutes << ' / '
         << setw(2) << hours << ' / ';
  cout << endl;
  return;
}

This program I'm supposed to write a C++ program named larger() that returns the later of any two dates passed to it and this is what I have:

CPP / C++ / C Code:
#include <iostream> 
#include <iomanip>
using namespace std;

class Date
{
  private:
    int month;
    int day;
    int year;

  public:
    aDate(int = 10, int = 9, int = 2005);
    bDate(int = 11, int = 3, int = 2005);
    void setDate(int, int, int); 
    void showDate();            
};

Date::Date(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;
}

void Date::setDate(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;

  return;
};

larger Date::findLastDate (Date & a, Date & b)
{
larger aDate = a.convrt();   
larger bDate = b.convrt();   

if (aDate >= bDate)
return aDate;
else
return bDate;
}

The result is supposed to display 11/3/2005 since it's the most recent date. The last problem I'm working on I'm supposed to add an addition to a program given to me. The assignment is.. Add to the program I provided you another member function named convrt() that does the following: The function should access the month, year, and day data members and should display and then return an integer that is calculated as year * 10000 + month * 100 + day. For example, if the Date is 2/1/2007, the return value is 20070102. This is what I have done so far:

CPP / C++ / C Code:
#include <iostream> 
#include <iomanip>
using namespace std;

class Date
{
  private:
    int month;
    int day;
    int year;
  public:
    Date(int = 7, int = 4, int = 2006); 
    void setDate(int, int, int); 
    void showDate();             
};

Date::Date(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;
}

void Date::setDate(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;

  return;
}

void Date::showDate()
{
  cout << "The date is ";
  cout << setfill('0')
       << setw(2) << month << '/' 
       << setw(2) << day << '/' 
       << setw(2) << year % 100; 
  cout << endl;

  return;
}

int main()
{
  Date a, b, c(4,1,1998); 

  b.setDate(12,25,2007);
  a.showDate();       
  b.showDate();      
  c.showDate();      
  cout << endl;

  return 0;
}

//below is what I added to the program

int Date::convrt()
{
  Date a, b, c(4,1,2006); 

  b.setDate(2006,04,01);
  c.showDate(), b.showDate(), a.showDate;  
  cout << endl;

  return 0;
}

I'm not sure what to do. Am I supposed to use a loop statement? I know I'm supposed to use year * 10000 + month * 100 + day someplace but I don't know where or how to do it. These problems are really confusing to me and I hope I'm not confusing anyone else. If I left anything out please let me know. I'm doing my best to understand this programming stuff. I really need help if anyone can.
  #2  
Old 25-Apr-2007, 22:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: I need help with three C++ assignments asap please


Quote:
Originally Posted by silverneon
,,,, since I can't get the programs to run. Can anyone help me

Why not take them one at a time? Maybe getting the first one going will help you see how to approach the others.

To have reasonable expectations of getting meaningful help with fewest iterations, I respectfully suggest that, instead of saying, "I can't get...," you do the following:

Post the first one.

Tell us what happened when you compiled the program. Paste compiler messages into your post. Paste the entire message (don't edit; don't paraphrase). If there are lots and lots of messages, then just post the first ten or so. Sometimes fixing the first ones will cause other messages to go away, or at least give you some clues to fixing the others.

If it compiles without any messages (no errors; no warnings), then tell us what happened when you try to execute it.

If the assignment is to write a class, don't you need to write a main() function that instantiates class objects and tests whatever methods were assigned?

Show us what input you gave it (if any) and tell us exactly what happened. Tell us what you expected to happen, and ask specific questions about what you don't understand about the difference between what you expected to see and what you saw.

Tell us what compiler you are using. Sometimes it makes a difference to people who want to help. The idea is that maybe we can walk you through it. I know (believe me, I know) that compiler messages aren't always very clear, especially when you are just getting starting, but if we see what you are seeing, we might be able to elucidate.

Regards,

Dave

No one was born knowing this stuff, you know.
---davekw7x
  #3  
Old 26-Apr-2007, 04:40
davis
 
Posts: n/a

Re: I need help with three C++ assignments asap please


Quote:
Originally Posted by silverneon
Okay, I'm having a hard time with the three assignments I have left to do. I've been working on these for a week now and I'm getting no where. I asked the prof but she said to use the examples from the book which can help to some point but at the same time confuse me even more. The examples from the book didn't help much and I'm not entirely sure I'm doing it right since I can't get the programs to run. Can anyone help me on what I'm doing wrong and what I should be doing if I'm not already? To avoid confusing of what I'm doing the assignment is: Construct a Time class contaning interger data members, seconds, minutes, and hours. Have the class contain two constructors. The first should be default constructor having the prototype time(int, int, int), which uses default values 0 for each data member. The second constructor should accept a long integer representing a total number of seconds and disassemble the long integer into hours, minutes, and seconds. The final function member should display the class data members. and this is what I have done:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

class Time
{
  private:
    int seconds;
    int minutes;
    int hours;
  public:
   Time(int = 0, int = 0, int = 0);
   void setTime (int, int, int);
   void showTime(void);
};

Time::Time(int ss, int mm, int hr)
{
   seconds = ss;
   minutes = mm;
   hours = hr;
}

void Time::setTime(int ss, int mm, int hr)
{
   seconds = ss;
   minutes = mm;
   hours = hr;

   return;
}

  voidTime::showTime(void)
{
  cout << "The Time is ";
  cout << setfill('0')
         << setw(2) << seconds << ' / '
         << setw(2) << minutes << ' / '
         << setw(2) << hours << ' / ';
  cout << endl;
  return;
}

This program I'm supposed to write a C++ program named larger() that returns the later of any two dates passed to it and this is what I have:

CPP / C++ / C Code:
#include <iostream> 
#include <iomanip>
using namespace std;

class Date
{
  private:
    int month;
    int day;
    int year;

  public:
    aDate(int = 10, int = 9, int = 2005);
    bDate(int = 11, int = 3, int = 2005);
    void setDate(int, int, int); 
    void showDate();            
};

Date::Date(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;
}

void Date::setDate(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;

  return;
};

larger Date::findLastDate (Date & a, Date & b)
{
larger aDate = a.convrt();   
larger bDate = b.convrt();   

if (aDate >= bDate)
return aDate;
else
return bDate;
}

The result is supposed to display 11/3/2005 since it's the most recent date. The last problem I'm working on I'm supposed to add an addition to a program given to me. The assignment is.. Add to the program I provided you another member function named convrt() that does the following: The function should access the month, year, and day data members and should display and then return an integer that is calculated as year * 10000 + month * 100 + day. For example, if the Date is 2/1/2007, the return value is 20070102. This is what I have done so far:

CPP / C++ / C Code:
#include <iostream> 
#include <iomanip>
using namespace std;

class Date
{
  private:
    int month;
    int day;
    int year;
  public:
    Date(int = 7, int = 4, int = 2006); 
    void setDate(int, int, int); 
    void showDate();             
};

Date::Date(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;
}

void Date::setDate(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;

  return;
}

void Date::showDate()
{
  cout << "The date is ";
  cout << setfill('0')
       << setw(2) << month << '/' 
       << setw(2) << day << '/' 
       << setw(2) << year % 100; 
  cout << endl;

  return;
}

int main()
{
  Date a, b, c(4,1,1998); 

  b.setDate(12,25,2007);
  a.showDate();       
  b.showDate();      
  c.showDate();      
  cout << endl;

  return 0;
}

//below is what I added to the program

int Date::convrt()
{
  Date a, b, c(4,1,2006); 

  b.setDate(2006,04,01);
  c.showDate(), b.showDate(), a.showDate;  
  cout << endl;

  return 0;
}

I'm not sure what to do. Am I supposed to use a loop statement? I know I'm supposed to use year * 10000 + month * 100 + day someplace but I don't know where or how to do it. These problems are really confusing to me and I hope I'm not confusing anyone else. If I left anything out please let me know. I'm doing my best to understand this programming stuff. I really need help if anyone can.

The part that your comment says that you added, the "convrt" operation, seems rather ill-conceived. For one thing, you don't define a "convrt" operation in your class definition. You just lumped it on at the end. Also, we don't usually instantiate instances of the class in a class member operation. You see, in the case of a non-static member, you are not able to invoke the operation until you have a class instance (an object of the type defined by the class) and we reserve the "right" to instantiate objects of the type to its constructor(s) and externals who need to use the type.

Also, naming an operation "convrt" is just plain stupid. If you're going to "convert" a date from something to something else, at least spell out the word convert. You gain nothing by saving the "e" in typing. Also, we try to be to be explicit when naming things such that we provide clarity and remove doubts whenever possible. The notion that the "convert" operation converts to a "int" is made solely by its return type. Since we can not overload convert based solely on its return type, we further breakdown the innate polymorphic reasons for using C++. In other words, if Date::convert would ever want to return a "long" we'd be "stuck." Note that many class libraries return long when converting a Date type to an integral type. Additionally, you may want to consider a different naming scheme, such as: Date::toLong() or Date::toInt() or something along those lines. Another choice might be to overload a convert operation:

CPP / C++ / C Code:
void Date::convert( int& result );
void Date::convert( long& result );
void Date::convert( some_other_type& result );

...returning the "result" as an argument, which demonstrates the polymorphism mentioned earlier.

If you want to "test" the conversion functionality, instantiate a Date object in main or some function called by main and invoke the Date::convert operation. Don't try to create Date objects inside of one of its member operations.

Another naming "gotcha" is to use the class name in a non-ctor/cctor/dtor member operation. We don't use Date::showDate() because Date::show() already tells us all that it is going to be shown. Of course, "show" is rather ambiguous. Better might be Date::toStdOut() or something like it. Better yet is to write streaming I/O operations that you can invoke directly with stream operators.

Things like: Date( int mm, int dd, int yyyy ) {...}
...do not do anything explicit in terms of ensuring that the data is valid. For example, there is nothing in your code that keeps the user from entering a -2934829 for the month, 100 for the day and a 1 for the year (or billions upon billions of other "bad date" combinations). You need to validate your input so that you don't end up with garbage.

The same can be said for your Time class:

CPP / C++ / C Code:
void Time::setTime(int ss, int mm, int hr)
{
   seconds = ss;
   minutes = mm;
   hours = hr;

   return;
}

...what prevents the following input: ss = 1928174, mm = 72, hr = -1? Is that going to translate to a valid time?

Some of the features of a well-rounded date class are missing. Granted, you want to start slowly adding the basics and add more features as you progress, but since I'm "here," I'll mention a few of them:

One would expect that the Date would also be able to tell me things like what day of the week it is. It would also be reasonable to assume that it could tell me what day of the year it is in terms of 1-365 (or 366 if a leap year).

The notion of having an args ctor with the following bounding constraints seems rather poorly considered:

Date(int = 7, int = 4, int = 2006);

...since this date is in the past, when it is ever going to be useful on a wide enough scale as to be criteria for default values if the user doesn't care to explicitly enter one of them? Better is to write a no-args ctor that produces a Date object initialized to the "now" date.

With some minor changes, I was able to get your code to compile and run, though I did not implement the changes that I recommend here.

CPP / C++ / C Code:
#include <iostream> 
#include <iomanip>
using namespace std;

class Date
{
private:
    int m_month;
    int m_day;
    int m_year;
public:
    Date(int = 7, int = 4, int = 2006); 
    void setDate(int, int, int); 
    void showDate();             
    int convrt();
};

Date::Date(int mm, int dd, int yyyy)
{
    m_month = mm;
    m_day = dd;
    m_year = yyyy;
}

void Date::setDate(int mm, int dd, int yyyy)
{
    m_month = mm;
    m_day = dd;
    m_year = yyyy;

    return;
}

void Date::showDate()
{
    cout << "The date is ";
    cout << setfill('0')
    << setw(2) << m_month << '/' 
    << setw(2) << m_day << '/' 
    << setw(2) << m_year % 100; 
    cout << endl;

    return;
}

//below is what I added to the program

int Date::convrt()
{
    Date a, b, c(4,1,2006); 

    b.setDate(2006,04,01);
    c.showDate(), b.showDate(), a.showDate();  
    cout << endl;

    return 0;
}

int main()
{
    Date a, b, c(4,1,1998); 

    b.setDate(12,25,2007);
    a.showDate();       
    b.showDate();      
    c.showDate();      
    cout << endl;

    return 0;
}

Output:

Code:
The date is 07/04/06 The date is 12/25/07 The date is 04/01/98

...in order to determine whether a date is greater-than, less-than or equal-to another Date, I'd say that it should sound a lot like we need some comparison operators in our class. Something like:

CPP / C++ / C Code:
bool Date::operator>( Date const& rhs ) {...};
bool Date::operator<( Date const& rhs ) {...};
bool Date::operator==( Date const& rhs ) {...};


:davis:
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ student needs helps ASAP silverneon C++ Forum 10 06-Mar-2007 15:14
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 21:53
Need Help ASAP yeshwanth C++ Forum 0 04-Oct-2004 01:22
Need Help With My Project spring200301016 Computer Programming Advertisements & Offers 1 28-Mar-2004 03:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:54.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.