Re: newb question: need help with homework assignment

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 05/29/04


Date: Sat, 29 May 2004 16:49:55 GMT


"don't know" <I'mat@nowhere.com> wrote in message
news:10bhcrhrjbsaq0f@corp.supernews.com...
> Hi,
> I have been working on this assignment(with the help of my instructor, she
> told me this was ok) and cannot figure out why it doesn't display to the
> screen. (She didn't know why either).
> Can someone tell me why it doesn't display to the screen? Is it because
the
> showWork function is in the wrong place?
> Thank You,
> a frustrated newb

Actually, what you have should not even compile. Perhaps
you left part of the code out.

I see the problem (below), but first I'd like to point out
several other issues.

#include <cstring> /* for declaration of 'strcpy()' */
#include <iostream> /* for declaration of 'cout' */
#include <ostream> /* for declaration of 'endl' */

using std::strcpy;
using std::cout;
using std::endl;

>
> template<class T>
> T AddTwo(T x, T y)
> {
> T totalMinutes;
> totalMinutes = x + y;
> return (totalMinutes);

1. 'return' is not a function. THe parentheses are
    not needed (but harmless)

2. Note that the above can be simplified:

   return x + y;

> }
> class Homework
> {
> private:
> char className[50];
> char assignment[50];

I suppose you've not learned about the 'string' class yet.

> int minutes;
> int totalMinutes;

You don't appear to be using this member.

> public:
> void SetValues(char classNm[], char assign[], int time);
> void showWork();

Member functions that don't change the object should
be declared 'const'.

    void showWork() const;

> Homework operator+(Homework h);

Better would be:

Homework operator+(const Homework& h) const;

> };
> void Homework::SetValues(char classNm[], char assign[], int time)
> {
> strcpy(className, classNm);
> strcpy(assignment, assign);
> minutes = time;
> }

Have you not learned about constructors? What your
'SetValues()' does could be done at object creation
time.

> void Homework::showWork()

void Homework::showWork() const
> {
> cout<<"Homework assignment "<<assignment<<" for "<<className<<" should
take
> approximately "<<minutes<<endl;
> }

You should try to shorten your lines a bit when posting them
here. My newsreader made the above 'wrap', meaning I can't
just paste in into my editor and compile.
A good place to break up such a statement might be at one
or more of the << operators, for example:

cout<<"Homework assignment "<<assignment<<" for "<<className
    <<" should take approximately "<<minutes<<endl;

> Homework Homework::operator +(Homework h)

Homework Homework::operator+(const Homework& h) const
> {
> Homework temp;
> strcpy(temp.className, "Total Homework");
> strcpy(temp.assignment, "Total Assignment");
> temp.minutes = minutes + h.minutes;
> return (temp);
> }
>
> void main()

int main()

> {
> int a, b, c;

I don't like more than one declaration per line
(but this is a 'style' issue, so the above is
not 'wrong')

> a = 15;
> b = 25;
> c = AddTwo(a,b);

Read about initialization. The above can be simplified to:

int a = 15; /* alternate syntax: int a(15);
int b = 25;
int c = AddTwo(a, b);

> double g, h, i;
> g = 5.15;
> h = 4.15;
> i = AddTwo(g,h);

I'd write:

double g = 5.15;
double h = 4.15;
double i = AddTwo(g, h);

> Homework j, k, l;

Again you should learn about constructors. As is,
the 'Homework' objects 'j', 'k', and 'l' at this
point have unknown, indeterminate states, and
evaluating their members values would give 'undefined
behavior' (not good). A 'default constructor would
rectify this.

> j.SetValues("CS218", "Read Chapter 11", 25);
> k.SetValues("CS310", "Homework: Exercise 12 Page 411", 40);
> l = AddTwo(j,k);

Also another constructor would allow the creation followed
by a call to 'SetValues()' to be simplified with:

Homework j("CS218", "Read Chapter 11", 25);
Homework k("CS310", "Homework: Exercise 12 Page 411", 40);
Homework l (AddTwo(j,k));

Also, I don't think your use of arbitrary (especially single
character) identifier names is good. (Especially that 'l'
which can easily be mistaken for the numeral '1'). Try
to use more descriptive names.

> void showWork();

Finally (:-)) here's the reason you get no output. This
line is not (as you apparently believe) a call to the
function 'showWork()'. It's a declaration of a
(nonmember) function called 'showWork()' (which btw
is not defined anywhere). It has nothing to do with
your 'Homework' class. In order to call a (nonstatic)
member function, you need an object to call it with.
Even if you'd done that, your syntax is still incorrect,
because it's a declaration, not a function call.
Try this:

l.showWork(); /* note that there's no 'void' in this line */
               /* You should only specify a return type for a */
               /* declaration. You've already told the compiler */
               /* the return type above (with the declaration in */
               /* your class */
> getch();

This is not a standard C++ function. If you want to cause a
'pause' like this, use something like:

     cin.get();

/* (this will require the user to press 'Enter' after (or instead of)
   some other key to continue). Standard C++ does not provide any
   functions with the functionality of 'getch()'.

> }

Hope this helps, and feel free to ask questions about what
I've written.

(I didn't actually compile or test the changes I've made,
so I might have missed something. But this should get
you closer to the 'right track'.)

-Mike



Relevant Pages

  • Re: expected - isnt it there?? Please help!
    ... | "1st Homework Grade",) when I run my program. ... A declaration, like any other statement, ends with a semicolon, not a comma. ... Every int declaration you have here is attempting to set it to a String constant. ...
    (comp.lang.java.help)
  • Re: expected - isnt it there?? Please help!
    ... | "1st Homework Grade",) when I run my program. ... A declaration, like any other statement, ends with a semicolon, not a comma. ... Every int declaration you have here is attempting to set it to a String constant. ...
    (comp.lang.java.help)
  • Re: errors after upgrading to VS2008
    ... ambiguous call to overloaded function ... \include\atlosapice.h: or 'int ATL::lstrlenA' ... has no constructors ... declaration of 'size_t' ...
    (microsoft.public.vc.language)
  • Re: errors after upgrading to VS2008
    ... ambiguous call to overloaded function ... \include\atlosapice.h: or 'int ATL::lstrlenA' ... has no constructors ... declaration of 'size_t' ...
    (microsoft.public.vc.language)
  • Re: errors after upgrading to VS2008
    ... ambiguous call to overloaded function ... \include\atlosapice.h: or 'int ATL::lstrlenA' ... has no constructors ... declaration of 'size_t' ...
    (microsoft.public.vc.language)