11 Replies - 503 Views - Last Post: 02 June 2012 - 03:14 PM Rate Topic: -----

#1 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Randomly defining a member of a class?

Posted 31 May 2012 - 08:58 AM

This is partly my homework, but i haven't been able to find any examples of what i'm asking about....

lets say i have this class....

#include <string.h>

class Deliveryobject
{
public:
	string getItem();
	string getSource();
	string getDestination();

private:
	string Item;
	string Source ="123 South Market Street Downtown 101";
	string Destination ="1599 Burbank Road Cinema 10";

};


is it possible to randomly assign "Item" any one of the following : "package, letter, or tube"?

my attempts have failed me...
The source and destination do not change for this project, but id like to randomly change the type of Item that each one is once.

Thank you in advanced! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Randomly defining a member of a class?

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,080
  • Joined: 19-March 11

Re: Randomly defining a member of a class?

Posted 31 May 2012 - 09:00 AM

Well, random generators give you numbers. Is there any way to assign numbers to the possible values you want to select from? Then you could just pick a number...


EDIT: You'll notice I'm being a bit cagey about this... I do want to leave you as much as possible of the pleasure of finding the answer for yourself.

This post has been edited by jon.kiparsky: 31 May 2012 - 09:01 AM

Was This Post Helpful? 1
  • +
  • -

#3 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Re: Randomly defining a member of a class?

Posted 31 May 2012 - 09:05 AM

hmmmm .... Could I maybe make a 3 element array (ex: string type[3]= {package, letter, tube};)
and use each index of an array ? Or could I do the same thing but using an enum?? its been a while but i believe even enums are indexed numerically too?

These are my initial thoughts xD
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,080
  • Joined: 19-March 11

Re: Randomly defining a member of a class?

Posted 31 May 2012 - 09:09 AM

Array was my thought, but I've never been a big enum user. Probably ought to try playing with them more, just to add them to my kit.

Try it with the enum, just to see - you already know you can do it with the array, so you'll learn more this way.
Was This Post Helpful? 1
  • +
  • -

#5 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Re: Randomly defining a member of a class?

Posted 31 May 2012 - 09:11 AM

View Postjon.kiparsky, on 31 May 2012 - 09:00 AM, said:

EDIT: You'll notice I'm being a bit cagey about this... I do want to leave you as much as possible of the pleasure of finding the answer for yourself.



You dont come off cagey :D Id much rather find the answer on my own as you stated. In the form that you asked me about my own question to trigger my thoughts is the best way to approach it in my opinion!

View Postjon.kiparsky, on 31 May 2012 - 09:09 AM, said:

Array was my thought, but I've never been a big enum user. Probably ought to try playing with them more, just to add them to my kit.

Try it with the enum, just to see - you already know you can do it with the array, so you'll learn more this way.


Thanks for the VERY quick replies man, Ill come back in a bit (after some food) and let you know my results :D

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,080
  • Joined: 19-March 11

Re: Randomly defining a member of a class?

Posted 31 May 2012 - 09:18 AM

Glad to help.
Was This Post Helpful? 0
  • +
  • -

#7 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Re: Randomly defining a member of a class?

Posted 01 June 2012 - 11:35 PM

So I could NOT for the life of it get it to work either way with an array or enums, it made sense in my head but I couldn't write the proper code for it if my life depended on it. So I decided to scratch that and Automatically initialized all the members of the class from the start so I could deal with it later and get something from this project done.

Now I'm running into another problem...
In main{} I have created a vector of classes so... vector<Deliveryobject>vectObjects;

I have a function called Decision() that asks the user what they want to do (into a switch statement)
depending on what they pick it will call the appropriate function[ either add, delete, or swap something in the vector]

My problem is that I cant access this vector that is in main from any of these functions, so if i try to do a vectObjects.push_back(*obj) from with in the add() function it will tell me that its undeclared and that the "left of .push_back " must have class/struct/union as an error...

anyway here's my code.

Ive been tugging away at this for so long now my brain is gonna fry!! Would anyone be kind enough to lend me a hand?


#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;
using namespace System;


void Decision();
void vector();
void addPackage();
void deletePackage();
void movePackage();

int main()
{

	Decision();

	Deliveryobject *obj = new Deliveryobject;

	std::vector<Deliveryobject*>vectObjects;
	//cout << "There are now " << vectObjects.size() << " objects to be delivered"<< endl;
	//cout << "\n\n\n";

	system ("pause");
	return 0;
}


void Decision()
{
	int choice;
	cout <<"What would you like to do?\n";
	cout <<"\t 1. Add a Package \n";
	cout <<"\t 2. Delete a Package \n";
	cout <<"\t 3. Move a Package \n";
	cout <<"\t 4. Exit the application";
	cin >> choice;
		switch(choice)
		{
			case 1:
		
				cout <<"You chose to add a Package\n";
				addPackage();
				break;
			case 2:
				cout <<"You chose to delete a Package\n";
				//deletePackage();
				break;
			case 3:
				cout <<"You chose to move a Package \n";
				//movePackage();
				break;
			case 4:
				cout <<"You chose to exit! \n Good Bye!\n";
				break;
			default:
				cout <<"You typed something incorrectly\n";
		}
	
}

void addPackage()
{
	int numpackagestoadd;
	cout << "How many packages do you want to add?\n";
	cin >> numpackagestoadd;


        /*
	//1>PreReq.cpp(72): error C2065: 'vectObjects' : undeclared identifier
	//1>PreReq.cpp(72): error C2228: left of '.push_back' must have class/struct/union
	//1>          type is ''unknown-type''
	//1>PreReq.cpp(72): error C2065: 'obj' : undeclared identifier
	while( numpackagestoadd >0)
	{
		vectObjects.push_back(obj);
		numpackagestoadd--;
	}*/
}

void deletePackage()
{
	//empty for now
}

void movePackage()
{
	// empty for now
}



	1>PreReq.cpp(72): error C2065: 'vectObjects' : undeclared identifier
	1>PreReq.cpp(72): error C2228: left of '.push_back' must have class/struct/union
	1>          type is ''unknown-type''
	1>PreReq.cpp(72): error C2065: 'obj' : undeclared identifier



To me this seems like a scope problem I tried used to scope :: operator but its a part of main and not the global namespace? I've been at this for too long today! maybe I just need a break?

Also if you guys think there might be any other issues with some headers, I can post the code to those too, the code currently compiles if I leave things commented out the way it is pasted on this forum.

Thank you!
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Randomly defining a member of a class?

Posted 02 June 2012 - 05:41 AM

Moved to C++
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,549
  • Joined: 25-December 09

Re: Randomly defining a member of a class?

Posted 02 June 2012 - 06:16 AM

In the following sinippet:

Quote

1>PreReq.cpp(72): error C2065: 'vectObjects' : undeclared identifier

Where in addPackage() was vectObject declared? If you want to use the vectObjects that was defined in main in this function you must pass that object into this function as a parameter.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Re: Randomly defining a member of a class?

Posted 02 June 2012 - 08:25 AM

Thank you for your reply jimblumberg

vectObjects was only declared in main, so therefore not declared in addPackage().

I have been reading about passing a vector through a function, passing by reference seems like the only feasible choice.

The way this program looks is that main makes a call to decision() which depending on user input calls one of the appropriate functions to add/delete/move, so should I have to pass the vectObjects all the way through decision into add/delete/move ??

finally since the user will be mainly staying in the decision() function do you think it might be a better idea for me to put the vectobject in the decision function?

If there's any other suggestions you guys could make , like habits , or anything that sticks out ! Let me know, id hate to form and keep a bad habit!
Was This Post Helpful? 0
  • +
  • -

#11 xCmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 12

Re: Randomly defining a member of a class?

Posted 02 June 2012 - 12:01 PM

Alright well incase anyone is interested I figured out how to pass the vector by reference, and the function calls finally work correctly!

Heres the source incase anyone wants to take a peek

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;
using namespace System;


void Decision(vector<Deliveryobject> &x);
void addPackage(vector<Deliveryobject> &x);
void deletePackage(vector<Deliveryobject> &x);
void movePackage(vector<Deliveryobject> &vx);

int main()
{
	Deliveryobject *obj = new Deliveryobject;
	

	vector<Deliveryobject> vectObjects;
	
	Decision(vectObjects);

	cout << "There are now " << vectObjects.size() << " objects to be delivered\n\n\n"<< endl;

	system ("pause");
	return 0;
}


void Decision(vector<Deliveryobject> &x)
{
	int choice;
	cout <<"What would you like to do?\n";
	cout <<"\t [1] Add a Package \n";
	cout <<"\t [2] Delete a Package \n";
	cout <<"\t [3] Move a Package \n";
	cout <<"\t [4] Exit the application\n";
	cin >> choice;
		switch(choice)
		{
			case 1:
		
				cout <<"You chose to add a Package\n";
				addPackage(x);
				break;
			case 2:
				cout <<"You chose to delete a Package\n";
				deletePackage(x);
				break;
			case 3:
				cout <<"You chose to move a Package \n";
				movePackage(x);
				break;
			case 4:
				cout <<"You chose to exit! \n Good Bye!\n";
				break;
			default:
				cout <<"INCORRECT INPUT:\nSELECT [1], [2], [3], [4] ONLY\n";
				Decision(x);
		}
	
}

void addPackage(vector<Deliveryobject> &x)
{
	int numpackagestoadd;
	cout << "How many packages do you want to add?\n";
	cin >> numpackagestoadd;
	//x.push_back(obj);

Decision(x);
}

void deletePackage(vector<Deliveryobject> &x)
{
	cout <<"Which package do you want to delete?\n";
Decision(x);
}

void movePackage(vector<Deliveryobject> &x)
{
	// empty for now
	cout <<"Which package do you want to move and to where?\n";
Decision(x);
}



My only problem now is once im in addPackage()

void addPackage(vector<Deliveryobject> &x)
{
	int numpackagestoadd;
	cout << "How many packages do you want to add?\n";
	cin >> numpackagestoadd;
	//x.push_back(obj);

Decision(x);
}


i want to add more "obj" to the vector(as you can see on the commented line), obj was constructed in main, as such:
Deliveryobject *obj = new Deliveryobject;


Im thinking i need to pass this constructor or the object through the functions too?
Does anyone have an input on how to better do this?
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Randomly defining a member of a class?

Posted 02 June 2012 - 03:14 PM

It should be fairly obvious that you need to to create and populate the Deliveryobject you're adding in the addPackage() method, and it should NOT be a pointer. This means remove the obj pointer in main as it's completely pointless.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1