Jump to content

Help finding an error?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
Hey guys I just joined and have just became interested in C++ so I bought the book Beginning C++ Through Game Programming, Second Edition by Michael Dawson. I reached Chapter 8 then got stuck on one of the exercises.
I'm pretty sure it's not a syntax error so if I mispelled anything thats probably not the error.
*Chapter 8 Exercise 3*
What design problem does the following program have?


#include<iostream>

using namespace std;


class Critter

{

public:

     int GetHunger() const {return m_hunger;} //Is this where the error is?

private:

     int m_hunger;

};


int main()

{

Critter crit;

cout<<crit.GetHunger() <<endl;

return 0;

}

By the way is there a book with the answers or something?? I find it odd that he gives us exercises and we can't tell if we are right or not.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Perhaps -- and comment slashes are the other ones.

Relo said:

Hey guys I just joined and have just became interested in C++ so I bought the book Beginning C++ Through Game Programming, Second Edition by Michael Dawson. I reached Chapter 8 then got stuck on one of the exercises.
I'm pretty sure it's not a syntax error so if I mispelled anything thats probably not the error.
*Chapter 8 Exercise 3*
What design problem does the following program have?


#include<iostream>

using namespace std;


class Critter

{

public:

     int GetHunger() const [B][COLOR="Blue"][SIZE="6"]{[/SIZE][/COLOR][/B]return m_hunger;[B][COLOR="Red"][SIZE="6"])[/SIZE][/COLOR][/B] \\Is this where the error is?

private:

     int m_hunger;

};


int main()

{

Critter crit;

cout<<crit.GetHunger() <<endl;

return 0;

}

By the way is there a book with the answers or something?? I find it odd that he gives us exercises and we can't tell if we are right or not.


#3
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
Woops sorry messed up the comments slashes.. ... I'm so bad.. I meant to put a bracket there...That's not the error.

*Meant Braces...

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Ah. I'm wondering if you've tried something like this?
#include<iostream>

using namespace std;


class Critter

{

public:

   int GetHunger() const [COLOR="Blue"][B][SIZE="6"]{[/SIZE][/B][/COLOR] return m_hunger; [COLOR="Blue"][B][SIZE="6"]}[/SIZE][/B][/COLOR] //Is this where the error is?

private:

   int m_hunger;

};


int main()

{

   Critter crit;

   cout<<crit.GetHunger() <<endl;

   return 0;

}


#5
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
? I'm confused. That's what the original problem said.

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Relo said:

? I'm confused. That's what the original problem said.
It can be hard to tell, sometimes, what one means by "a bracket there".

Relo said:

Woops sorry messed up the comments slashes.. ... I'm so bad.. I meant to put a bracket there...That's not the error.
For example, I might say, "a right curly brace instead of the close paren" in an attempt at specificity.

But when you run your program, does it produce the result you expect? If not, why do you think not?

#7
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
Once again I forgot to read closely what I typed.. I meant a right curly brace.

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Relo said:

What design problem does the following program have?

I find it odd that he gives us exercises and we can't tell if we are right or not.
Is the output of the program what you expect?

What is it that you expect, and why?

#9
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
Well when I compile the little segment of code it shows 2. I'm not really why though.

#10
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Perhaps because you constructed an object and didn't initialize members?

#11
Relo

Relo

    Newbie

  • Members
  • Pip
  • 7 posts
Actually that's what I was thinking but wasn't sure.

#12
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
So perhaps the design issue is that an object is constructed with indeterminate member values that are later used?

Or more simply: a class is missing a default constructor?