Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,293 people online right now. Registration is fast and FREE... Join Now!




Cat program

 
Reply to this topicStart new topic

Cat program, Cat program

serginsurge
21 May, 2008 - 04:18 AM
Post #1

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 14

Can someone please tell me whats wrong with this

CODE

#include<iostream.h>

class cat
{
public:
int age;
int status;

void meow();
void sleep();
void wake();
};

void cat:: meow()
{
if(status==1)
{
cout<<"Zzzzzzzzzz...\n";
}
else
{
cout<<"Meow!\n";
  }

}
void cat::sleep()
{
if(status==1)
{ cout<<"HI! Nice to see you\n";}
else
{ cout<<"Aaaaaah... Good Morning!\n";
status=0;
}
}
int main()
{
cat Frisky;
int what =0;


Frisky.wake();
cout<<"Hello! How old is your cat?";
cin>>Frisky.age;
cout<<"Thank you!\n";
cout<<"Press 1 to meow, 2 to sleep, 3 to wake, 4 to end";
cin>>what;

while(what !=4)
{
if(what == 1)
{Frisky.meow();}
else if(what == 2)
{Frisky.sleep();}
else if (what == 3)
{Frisky.wake();}
cin>>what;
}
cout<<"Thank you for playing cat-A-Lizer with us\n";

return 0;
}
}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Cat Program
21 May, 2008 - 05:03 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,052



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Does it compile?

cpp

{ cout<<"HI! Nice to see you\n";}


I hate that.

Do this:
cpp

{
cout<<"HI! Nice to see you\n";
}


or this:

cpp

if (status==1) {
cout<<"HI! Nice to see you\n";
}

User is online!Profile CardPM
+Quote Post

foohoo
RE: Cat Program
21 May, 2008 - 05:19 AM
Post #3

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 30


My Contributions
Hi serginsurge,

This could be casuing the problem:

You are using the "cout" command. for this you need to include the "cstdlib" like so:

CODE
#include <cstdlib>


and also you can declare that you are using the namespace "std" after the includes likse so

CODE
using namespace std;


Also at the moment you are using a "if" statements to process the menu options. A switch statement would be more suited to this. It is setup along the lines of :

CODE

while (what != 4){
   switch (what) {
       case 1:
                //what happens if 1 is entered
                break;
       case 2:
                //what happens if 2 is entered
                break;
        case 3:
                //what happens if 3 is entered
                break;
        case 4:
                cout<<"Thank you for playing cat-A-Lizer with us\n";
                break;
       default:
               cout<<"Press 1 to meow, 2 to sleep, 3 to wake, 4 to end";
               cin>>what;
               break;
   }
}



makes it a little easier to read and update. Also you need to initiate "status" to equal something or it could equal anything.

I hope this helps a little!






User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Cat Program
21 May, 2008 - 05:43 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
@foohoo: You don't need to #include <cstdlib> it's #include <iostream> tongue.gif

@serginsurge: Is your problem solved now?
User is offlineProfile CardPM
+Quote Post

foohoo
RE: Cat Program
21 May, 2008 - 06:00 AM
Post #5

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 30


My Contributions
Ah my bad! wink2.gif

I didn't mess anything else up did I?
User is offlineProfile CardPM
+Quote Post

serginsurge
RE: Cat Program
21 May, 2008 - 06:08 PM
Post #6

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 14

It says the problem is with
Frisky.wake()
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Cat Program
21 May, 2008 - 06:50 PM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(serginsurge @ 21 May, 2008 - 10:08 PM) *

It says the problem is with
Frisky.wake()


Indeed, I don't see a void cat::wake().

The style, as noted, is odd. However, I don't like anyone's style aside from mine. wink2.gif

Still, regardless of what you like, be consistent. Particularly with code blocks. I personally prefer inline curlies:
CODE

if(status==1) {
   cout<<"HI! Nice to see you\n";
} else {
   cout<<"Aaaaaah... Good Morning!\n";
   status=0;
}


User is offlineProfile CardPM
+Quote Post

serginsurge
RE: Cat Program
22 May, 2008 - 03:48 AM
Post #8

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 14

Thanks guys, I figured it out
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month