Help With Assignment Please

This is a discussion on Help With Assignment Please within the C++ Programming forums, part of the General Programming Boards category; I`m working on an assignment for my class and the instructions contain pseudo code and I`m having a hard time ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Question Help With Assignment Please

    I`m working on an assignment for my class and the instructions contain pseudo code and I`m having a hard time understanding exactly what is going on. Can someone please re-vise the instructions and pseudo code so that my dumb butt can understand it? lol. What I have so far is below. My assignment instructions are located here: assignment

    Code:
    #include <iostream>
    using namespace std;
    
    class count
    {
    public:
    	count();
    	count (int a);
    	~count();
    	void Initialize(int b);
    	int Retrieve();
    	void Print(int p);
    private:
    	int ingr;
    	static int constr;
    	static int destruct;
    };
    
    int count::constr=0;
    int count::destruct=0;
    
    void round_we_go();
    
    int main()
    {
    	round_we_go();
    	return 0;
    }
    void round_we_go()
    {
    	static count counter(0);
    }
    count::count(int a)
    {
    	constr=constr+a;
    }
    count::~count()
    {
    	destruct++;
    }
    void count::Initialize(int b)
    {
    }

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The assignment is:

    Make a class called whatever you want, that:

    -Keeps count of how many times the constructor and destructor are called
    -Has a member variable which is an int
    -Has member functions to (a) set the int, (b) check what the int is, and (c) print the int along with the constructor/destructor count

    After you've made the class, make a test program to demonstrate that your class actually works.

    Tips on the code you have:
    -In the constructor, it shouldn't do "constr = constr + a", it should increase by 1 instead of by a, because you're just trying to count how many times the constructor is called. Also, you should set ingr to the value of a.

    -Initialize() should probably be called set() or something instead. All it should do is assign the value of b to ingr.
    -Retrieve should just return ingr.
    Last edited by Hunter2; 03-22-2004 at 09:41 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Smile More help please :)

    Ok, I`ve made some changes and I think that it looks at least a little better. *sighs* But I think that there are still some major mistakes in there that I need to take care of. Can someone look at my code and give me some input please? Thanks. And thank you Hunter2 for your initial help.

    Code:
    #include <iostream>
    using namespace std;
    
    class doit
    {
    public:
    	doit();
    	doit (int a);
    	~doit();
    	void Set_it(int b);
    	int Retrieve();
    	void Print(int lne);
    private:
    	int ingr;
    	static int constr;
    	static int destruct;
    };
    
    int doit::constr=0;
    int doit::destruct=0;
    
    void round_we_go(int X, int Y);
    
    int main()
    {
    	doit A(9);
    	doit B(-1);
    	int lp=0;
    	A.Print(__LINE__);
    	while(A>=B)
    	{
    		round_we_go(A, lp);
    		A=A-2;
    		lp++
    	}
    	A.Print(__LINE__);
    	return 0;
    }
    void round_we_go(int X, int Y)
    {
    	doit thing;
    	static doit tracer(0);
    	tracer.set(tracer.get() + 1);
    	if(X<0)
    	cout << "The looping function was called " << Y << " times.";
    	thing.Print(__LINE__);
    	while(thing.get() < X.get())
    	{
    		doit thing1;
    		thing1.set(thing.get());
    		thing.set(thing.get() +1);
    	}
    	thing.Print(__LINE__);
    }
    doit::doit(int a)
    {
    	ingr=a;
    	constr++;
    }
    doit::~doit()
    {
    	destruct++;
    }
    void doit::Set_it(int b)
    {
    	ingr=b;
    }
    int doit::Retrieve()
    {
    	return ingr;
    }
    void doit::Print(lne)
    {
    	cout << "On line " << lne << " there have been " << constr <<
    	       << " objects constructed and " << destruct << " destroyed.";
    }

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Wow, looking at the example code in the assignment, I can see why you're confused. Holy ****!!!! Horrible indentation, opening/closing braces don't match up, there's no clear purpose to it, no consistent naming convention... If I were you, the first step I'd take to solving the problem is to talk to the teacher/prof/whatever and get them to fix the assignment. When that fails, then proceed to this next section

    OK, from the wording of the assignment, I'd say that they want you to duplicate what the assignment does as closely as possible, just with different names. If they didn't, I'd suggest something a lot simpler and more practical, but w/e.

    So problem #1 with your code: You can't just compare a and b with >=. If you want to do that, you'd have to define an operator for it, which you aren't told to do in the assignment. In any case, to compare them you need to use Retrieve(). For example:
    while(A.Retrieve() >= B.Retrieve())

    Same problem with "A = A-2;". You need to use Set_it() and Retrieve() to decrease A by 2.

    Now, problem with your call to round_we_go(). The function takes 2 ints, X and Y, but you're passing it a doit and an int. Again, you'll need to use Retrieve() for it to work properly.

    Moving right along, let's take a look at round_we_go(). You'll notice that the .get() and .set() lines probably don't work. That's because in your "doit" class, they're called Retrieve() and Set_it()

    And I think that should pretty much get it to run as it was *intended* to Of course, that doesn't take into account the fact that tracer has absolutely no purpose in round_we_go(), and neither does the line "thing1.set(thing.get());", and about 9999 other things given in the example.

    You can tell your instructor that I think he's a douchebag and should make sure his 'pseudo-pseudocode' actually works before he gives it as an example. Good luck getting him fired
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Originally posted by Hunter2
    <SNIP> Of course, that doesn't take into account the fact that tracer has absolutely no purpose in round_we_go(), and neither does the line "thing1.set(thing.get());", and about 9999 other things given in the example. <SNIP>[/B]
    I was thinking the same thing! I thought that it was just me. I had no clue whatsoever what some of those things were supposed to do. I`ll just work on what you told me and show it to my prof tonight. I really appreciate your help by the way.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Ok, I tried to go through and make the changes that you suggested. However, I`m getting the following error message: "

    Project1.obj : error LNK2019: unresolved external symbol "public: __thiscall doit::doit(void)" (??0doit@@QAE@XZ) referenced in function "void __cdecl round_we_go(int,int)" (?round_we_go@@YAXHH@Z)

    Debug/Project1.exe : fatal error LNK1120: 1 unresolved externals


    Now, here`s my revised code, if you can call it that. lol.
    Code:
    #include <iostream>
    using namespace std;
    
    class doit
    {
    public:
    	doit();
    	doit (int a);
    	~doit();
    	void Set_it(int b);
    	int Retrieve();
    	void Print(int lne);
    private:
    	int ingr;
    	static int constr;
    	static int destruct;
    };
    
    int doit::constr=0;
    int doit::destruct=0;
    
    void round_we_go(int X, int Y);
    
    int main()
    {
    	doit A=9;
    	doit B=-1;
    	int lp=0;
    	A.Print(__LINE__);
    	while(A.Retrieve()>=B.Retrieve())
    	{
    		int tc=A.Retrieve();
    		round_we_go(tc, lp);
    		A.Set_it(A.Retrieve());
    		lp++;
    	}
    	A.Print(__LINE__);
    	return 0;
    }
    void round_we_go(int X, int Y)
    {
    	doit thing(X);
    	static doit tracer(0);
    	tracer.Set_it(tracer.Retrieve()+1);
    	if(X<0)
    	cout << "The looping function was called " << Y << " times.";
    	thing.Print(__LINE__);
    	while(thing.Retrieve()<X)
    	{
    		doit thing1;
    		thing1.Set_it(thing.Retrieve());
    		thing.Set_it(thing.Retrieve() +1);
    	}
    	thing.Print(__LINE__);
    }
    doit::doit(int a)
    {
    	ingr=a;
    	constr++;
    }
    doit::~doit()
    {
    	destruct++;
    }
    void doit::Set_it(int b)
    {
    	b=b-2;
    	ingr=b;
    }
    int doit::Retrieve()
    {
    	return ingr;
    }
    void doit::Print(int lne)
    {
    	cout << "On line " << lne << " there have been " << constr <<
    		" objects constructed and " << destruct << " destroyed.";
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Your code trys calling the default constructor in round_we_go when you do this:

    doit thing1;

    but you don't have a definition for the default constructor that you have declared. When the compiler trys to find the definition in another file by linking to it it can't and therefore sends a linking error. Try adding a pair of empty curly brackets behind the declaration of the default constructor or commenting out the default constructor and see what happens.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't think commenting it out would help; if you define any constructors at all, I think the compiler takes out the automatically generated default constructor.

    Just define a default constructor that initializes ingr to some value.
    Code:
    doit::doit()
    :ingr(0)
    {}
    or
    class doit
    {
    public:
    	doit() {ingr = 0;}
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Ok, I understand it all now after messing with it for awhile. Everything does do something, much to my dismay. lol. That pseudo code was just lame as hell IMO. Anyways, here`s the working and correct (I hope) code:
    Code:
    #include <iostream>
    using namespace std;
    
    class doit
    {
    public:
    	doit();
    	doit (int a);
    	~doit();
    	void Set_it(int b);
    	int Retrieve();
    	void Print(int lne);
    private:
    	int ingr;
    	static int constr;
    	static int destruct;
    };
    
    int doit::constr=0;
    int doit::destruct=0;
    
    void round_we_go(doit X);
    
    int main()
    {
    	doit A(9);
    	doit B(-1);
    	A.Print(__LINE__);
    	while(A.Retrieve()>=B.Retrieve())
    	{
    		round_we_go(A);
    		A.Set_it(A.Retrieve()-2);
    	}
    	A.Print(__LINE__);
    	return 0;
    }
    void round_we_go(doit X)
    {
    	doit thing;
    	static doit tracer(0);
    	tracer.Set_it(tracer.Retrieve()+1);
    	if(X.Retrieve()<0)
    		cout << "The looping function was called " << tracer.Retrieve() << " times.\n";
    	thing.Print(__LINE__);
    	thing.Set_it(0);
    	while(thing.Retrieve()<X.Retrieve())
    	{
    		doit thing1;
    		thing1.Set_it(thing.Retrieve());
    		thing.Set_it(thing.Retrieve() +1);
    	}
    	thing.Print(__LINE__);
    }
    doit::doit()
    {
    	ingr=0;
    	constr++;
    }
    doit::doit(int a)
    {
    	ingr=a;
    	constr++;
    }
    doit::~doit()
    {
    	destruct++;
    }
    void doit::Set_it(int b)
    {
    	ingr=b;
    }
    int doit::Retrieve()
    {
    	return ingr;
    }
    void doit::Print(int lne)
    {
    	cout << "On line " << lne << " there have been " << constr;
    	cout << " objects constructed and " << destruct << " destroyed.\n";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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