Getting wrong output from a class

This is a discussion on Getting wrong output from a class within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm getting confused with this classes. Im able to pass values to the class, but I'm not getting the ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Getting wrong output from a class

    Hi, I'm getting confused with this classes. Im able to pass values to the class, but I'm not getting the right output after I call it.

    if the object is "noticed" (seen = 1) and the player doesn't have the object, then the program should display "you have picked up the.." when i call object::get but it keeps showing me "there's does not to be a ...anywhere.


    here's the code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class object
    {
     private:
      string name;
      int where;			// Where object is, -1 if carried
      int seen;   		         // If you have seen it
      int have;      		         // If you have it
     
     public:
      void setup(string n, int w, int s, int h);
      int get(int room, int s, int h);
      int noticed(int room, int s);
    };
    
    void object::setup(string n, int w, int s, int h)
    {
     name.assign(n);
     where = w;
     seen = s;
     have = h;
    }
    
    int object::get(int room, int s, int h)
    {
     if (((room == where) || (where == -1)) && (s == 1) && (h == 0))
     {
      cout<< "You have picked up the "<< name <<"."<<endl;
      have = 1;
      return have;
     }
     else if (s != 1)
     {
      cout << "There does not seem to be a " << name << " anywhere." <<endl;
      return 0;
     }
     else if (h == 1)
     {
      cout<< "You already have the "<< name <<"."<<endl;
      return 1;
     }
     else 
     {
      cout<<"You cannot get the "<< name <<"."<<endl; 
      return 0;
     }
    }
    
    int object::noticed(int room, int s)
    {	
     if(s == 0)
     {
      cout<<"You notice a "<<name<<"."<<endl;
      seen = 1;
      return seen;
     }
     else
     {
      cout<<"You don't see anything"<<endl;
      return s;
     }
    }
    
    int main()
    {
     object something;
     int s = 0;
     int h = 0; 
     something.setup("can", 1, 0, 0); 
     something.get(1, s, h);               
     something.noticed(1, s); 
    
     return 0;
    }
    Last edited by orikon; 11-14-2005 at 02:41 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > something.get(1, s, h);
    > something.noticed(1, s);
    You need to call get() after noticed():
    Code:
     something.get(1, s, h);               
     something.noticed(1, s); 
     something.get(1, s, h);
    Or if you don't need the first call:
    Code:
     something.noticed(1, s); 
     something.get(1, s, h);

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    Thanks, that got it working, but now I've added more code and its not working right again.

    The cout statements in object::noticed should should change between 1 and 0 whenever you call case 93, but they never change. Am I not passing the values correctly within the classes? and should I be using a pointer somewhere?

    Code:
     
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    extern "C"
    {
    extern int get_input_line(char* verb, char* DO, int &link, char* io);
    extern void endgame();
    }
    
    class object
            {
            private:
                    char name[20];
                    int where;              // Where object is, -1 if carried
                    int seen;               // If you have seen it
                    int have;               // If you have it
                    int room;
    
            public:
                    int setup(char n[20], int w, int s, int h);
                    int get(int room);
                    int noticed(int room);
    
            };
    
    int object::setup(char n[20], int w, int s, int h)
            {
            strcpy(name, n);
            where = w;
            seen = s;
            have = h;
            return have;
            return seen;
            }
    
    int object::get(int room)
            {
            cout<<seen<<endl;
            cout<<have<<endl;
    
            if (((room == where) || (where == -1)) && (seen == 1) && (have == 0))
                    {
                    cout<< "You have picked up the "<< name <<"."<<endl;
                    have = 1;
                    return have;
                    }
            else if (((room == where) || (where == -1)) && (seen != 1) && (have == 0))
    //      else if (seen != 1)
                    {
                    cout << "There does not seem to be a " << name
                            << " anywhere." <<endl;
                    return 0;
                    }
            else if (((room == where) || (where == -1)) && (seen == 1) && (have == 1))
    //      else if (have == 1)
                    {
                    cout<< "You already have the "<< name <<"."<<endl;
                    return 1;
                    }
            else
                    {
                    cout<<"You cannot get the "<< name <<"."<<endl;
                    return 0;
                    }
            }
    
    int object::noticed(int room)
            {
            cout<<seen<<endl;
            if (seen == 0)
                    {
                    cout<<"You notice a "<<name<<"."<<endl;
                    seen = 1;
                    cout<<"seen "<<seen<<endl;
                    return seen;
                    }
            else
                    {
                    cout<<"You don't see anything"<<endl;
                    return 1;
                    }
            }
    
    int room1(int array[100])
            {
            char verbST[100];
            char IO[100];
            char DO[100];
            int link;
            int verb;
            object can;
            int room = 0;
            int seenit = 0;
            int haveit = 0;
    
            can.setup("can", room, seenit, haveit);
    
            verb = get_input_line(verbST, DO, link, IO);
            //itemnum = cvt(DO);
    
            switch (verb)
                    {
                    case 93:
    
                            can.setup("can", 1, seenit, haveit);
                            seenit = can.noticed(1);
                            //cout<<seenit<<endl;
                            break;
                    case 50:
                            can.setup("can", 1, seenit, haveit);
                            haveit = can.get(1);
                            break;
                    default:
                            cout << " You can't do that!" << endl;
                            return 1;
                    }
            return 1;
            }
    
    main ()
            {
            int room;
            int array[100];
            room = 1;
    
            cout<<endl<<endl;
            cout<<"               --==]==[ Title ]==[==--" <<endl<<endl;
    
    
            srandom(time(NULL));
    
            while (room < 5)
                    {
                    cout << endl << endl;
                    switch (room)
                           {
                            case 1:
                                    room = room1(array);
                                    break;
    
                            }
                    }
    
            }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int room1(int array[100])
    > {
    >.
    >.
    > object can;

    I think the problem is object "can" goes away once function room1() ends. So the next time you call room1(), you create a new object. If you want to keep this object between calls to room1, you need to create the object in main(), then pass the object to room1. You would also move the first setup:
    Code:
            can.setup("can", room, seenit, haveit);
    to main.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    ah that makes a lot of sense. I'll give that a shot. Thank you very much

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    I seem to be having some trouble with this. Im familiar with passing values between functions, but how would I pass values of a class between functions ?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I seem to be having some trouble with this. Im familiar with passing values between functions, but how would I pass values of a class between functions ?
    Have you studied the difference between "passing-by-value" and "passing-by-reference"? If you pass-by-value, then the object you send to a function gets copied and any changes you make are made to the copy. When the function returns all local variables are destroyed, so the copy is destroyed leaving you with the original object unchanged. Here is an example of passing-by-value:
    Code:
    #include<iostream>
    using namespace std;
    
    class Apple
    {
    public:
    	Apple(int num)
    	{
    		size = num;
    	}
    	Apple()
    	{}
    
    	void setSize(int num)
    	{
    		size = num;
    	}
    	
    	int getSize()
    	{
    		return size;
    	}
    
    private:
    	int size;
    };
    
    void someFunc(Apple anApple)
    {
    	anApple.setSize(10);
    }
    
    
    int main()
    {
    	Apple myApple(3);
    	cout<<myApple.getSize()<<endl;
    
    	someFunc(myApple);
    	cout<<myApple.getSize()<<endl;
    
    	return 0;
    }
    If you pass-by-reference, then no copy of the object is made for the function and any changes you make are made to the original object. Here is an example:
    Code:
    #include<iostream>
    using namespace std;
    
    class Apple
    {
    public:
    	Apple(int num)
    	{
    		size = num;
    	}
    	Apple()
    	{}
    
    	void setSize(int num)
    	{
    		size = num;
    	}
    	
    	int getSize()
    	{
    		return size;
    	}
    
    private:
    	int size;
    };
    
    void someFunc(Apple& anApple)
    {
    	anApple.setSize(10);
    }
    
    
    int main()
    {
    	Apple myApple(3);
    	cout<<myApple.getSize()<<endl;
    
    	someFunc(myApple);
    	cout<<myApple.getSize()<<endl;
    
    	return 0;
    }
    All you have to do is put a little "&" symbol after the parameter type, and that allows the function to change the object it receives as an argument.
    Last edited by 7stud; 11-16-2005 at 09:43 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For example, say you had room1 declared like this:
    Code:
    int room1(object &can)
    And in main() you had this:
    Code:
            object can;
            int room = 0;
            int seenit = 0;
            int haveit = 0;
    
            can.setup("can", room, seenit, haveit);
    Then to call room1, this would be the next line in main:
    Code:
            room1(can);
    And the rest of your code would be pretty much the same, except for the lines above that are now in main().

    Or you could use a more generic name for your object in room1():
    Code:
    int room1(object &obj)
    Then you'd refer to the object as obj instead of can. There's many ways to do the same thing.
    Last edited by swoopy; 11-16-2005 at 09:31 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, that array you are passing in your original room1:
    Code:
    int room1(int array[100])
    If you're going to need that at some point, you would just make that another parameter, along with object.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    Hey its working great now! thanks so much, really appreciate it. I was somewhat familiar with pass by ref and pass by ref but didnt really understand it, but with that helpful explanation you gave me it all makes sense now. Thank you!

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Lightbulb

    After some thought, although passing the object to room1 works, something you might want to consider is making room1 into a class. Then you could add an object as a member of the room.
    Code:
    class Room
    {
    private:
       object can;
    .
    .
    };
    Then in the constructor for Room, you could do the initial setup:
    Code:
    Room::Room()
    {
            int room = 0;
            int seenit = 0;
            int haveit = 0;
    
            can.setup("can", room, seenit, haveit);
    }
    Then instead of calling room1() in main(), you would create a Room called room1:
    Code:
    int main()
    {
       Room room1;
    And then in the main loop, you could call a member function Enter or something similar.
    Code:
            while (room < 5)
                    {
                    cout << endl << endl;
                    switch (room)
                           {
                            case 1:
                                    room = room1.Enter();
                                    break;
    
                            }
                    }

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    Thanks for the suggestions, that sounds like a good idea. I'll definitely consider doing that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out cause of wrong response from class members
    By imCrushedByCode in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2006, 12:30 AM
  2. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  3. Displaying my glut output in my own class window.
    By Queatrix in forum Windows Programming
    Replies: 0
    Last Post: 10-19-2005, 10:09 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21