6 Replies - 322 Views - Last Post: 07 September 2012 - 08:42 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Vector structure utilizing temp variables fails to iterate?

Posted 06 September 2012 - 08:58 PM

The program successfully compiles, but it truly is nightmare trying to get the array contents stored in the vector structure. It seems the block of code in lines 42 to 52, only initializes values for 1 directory and its inventory items.

Also the Items are displayed as address location instead of regular text forum. I've tried adding & to line 70 infront of StoreVec, no difference.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int Size = 3;		// Max directories
const int InvSize = 4;	// Max items per Store

struct Store
{
   int ID;    
   string title[InvSize];			// 3 members per Store.
};

void SetDefaults (vector <Store> & , const int [], const string [][InvSize], int);
void DisplayVector (const vector <Store> &);

int main()
{
	vector<Store> items; 

	const int Directory [Size ] ={ 101, 102, 103};
	const string Inventory [] [InvSize] = {		{"Black", "Cyan", "Magenta", "Yellow"},
												{"OHV", "SOHC", "DOHC", "QOHC"},
												{"Password", "Keycard", "Token", "Retina"},	};


	SetDefaults(items, Directory , Inventory , Size);

	DisplayVector(items);


	system("pause");
	return 0;
}	


void SetDefaults (vector <Store> & StoreVec, const int dir[],
	const string inv[][InvSize], int size)
{

	for (int i = 0; i < size; i++)
	{
		Store temp;
		temp.ID = dir[i];
		StoreVec.push_back(temp);


		for (int i2 = 0; i2 < InvSize; i2++)
		{
			temp.title[i2] = inv[i][i2];
			StoreVec.push_back(temp);
		}

	}
}


void DisplayVector (const vector <Store> & StoreVec)
{
	cout << "Display current...\n" << endl;

	for (int i = 0; i < Size; i++)		// Changing to for (int i = 0; i < 10; i++)	,for examples, there at least 6 copies of each ID/Directory.
	{

		cout << "Store " << (i+1) << ": \nID: " << StoreVec[i].ID << "    Itemss";
		
		for (int i2 = 0; i2 < InvSize; i2++)
		{	
			cout << "   " << StoreVec[i2].title << "   ";	// Changing i2 to i, each row, instead of column, has identical Items address
		
		}

		cout << endl << endl;
	}

	cout << endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Vector structure utilizing temp variables fails to iterate?

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3057
  • View blog
  • Posts: 9,304
  • Joined: 25-December 09

Re: Vector structure utilizing temp variables fails to iterate?

Posted 06 September 2012 - 09:04 PM

After a quick look I don't think you need the push_back on line 52 of your code.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Vector structure utilizing temp variables fails to iterate?

Posted 06 September 2012 - 09:13 PM

Removing the line completely resulted in a fatal crash of the compilation; Debug Assertion Failed.

Then giving the option of Abort Retry & Ignore; Microsoft Visual Studio 2010.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 3057
  • View blog
  • Posts: 9,304
  • Joined: 25-December 09

Re: Vector structure utilizing temp variables fails to iterate?

Posted 06 September 2012 - 09:20 PM

After I removed that line this is the output I received:

Quote

Display current...

Store 1:
ID: 101 Itemss 0x992217c 0x9922190 0x99221a4 0x99221b8

Store 2:
ID: 102 Itemss 0x992217c 0x9922190 0x99221a4 0x99221b8

Store 3:
ID: 103 Itemss 0x992217c 0x9922190 0x99221a4 0x99221b8


sh: pause: command not found


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1933
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: Vector structure utilizing temp variables fails to iterate?

Posted 06 September 2012 - 09:31 PM

You don't need line 52. Line 46 should be after the for i2 loop... something like line 54. The objective is to initialize temp with the store ID, as well as all the inventory.

Line 70 goes out of range since you only have 3 stores, but you are accessing StoreVec[0..InvSize]. I think you mean to access StoreVec[i].title[i2].
Was This Post Helpful? 1
  • +
  • -

#6 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Vector structure utilizing temp variables fails to iterate?

Posted 07 September 2012 - 06:52 AM

Thanks skydiver, the default values to display correctly :).

Now my main challenge is getting the printlist, to display newly added items and Directory ID number by the user.

In lines 63 and 68, I tried to do StoreVec.size(), instead of the constant integers, and I got an infinite loop. And then a fatal crash of the compiler. :(

Edit: I think i figured it out. Line 68 is supposed be stay a constant integer.

This post has been edited by mgrex: 07 September 2012 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4890
  • View blog
  • Posts: 11,286
  • Joined: 16-October 07

Re: Vector structure utilizing temp variables fails to iterate?

Posted 07 September 2012 - 08:42 AM

I feel I should mention that you can initialize your structs like so:
Store items[] = {
	{ 101, {"Black", "Cyan", "Magenta", "Yellow"} },
	{ 102, {"OHV", "SOHC", "DOHC", "QOHC"} },
	{ 103, {"Password", "Keycard", "Token", "Retina"} },
};



Though, a more C++y way would be to use a constructor for the struct. With a constructor, your init becomes pretty simple.

#include <iostream> 
#include <vector> 

using namespace std; 

const int InvSize = 4;

struct Store {
	int ID;
	string title[InvSize];
	Store(int, const char *[]);
};

void display(const Store &);

int main() {
	vector <Store> store;
	
	store.push_back(Store(101, (const char *[]){"Black", "Cyan", "Magenta", "Yellow"} ));
	display(*store.begin());
	
	return 0;
}

void display(const Store &item) {
	cout << "ID: " << item.ID << "    Itemss";
	for (int i = 0; i < InvSize; i++) { cout << "   " << item.title[i] << "   "; }
	cout << endl;
}

Store::Store(int id, const char *items[]) : ID(id) {
	for (int i = 0; i < InvSize; i++) { title[i] = string(items[i]); }
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1