Is there a way to have 2 vectors mix together?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishirboy
    New Member
    • Mar 2013
    • 48

    #1

    Is there a way to have 2 vectors mix together?

    I want to add 2 vectors to print out so that there on the same line. What I am trying to make is an inventory system that will use 2 vectors to keep the pounds of the item and list the 2 vectors on one line.

    (I am using Microsoft Visual C++ 2010 Express)

    Like this:

    0. empty 0
    1. empty 0
    2. empty 0

    ect...

    Right now it looks like this:

    0. empty
    0. 0

    The code:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    	vector<string> inv;
    	vector<int> pou;
    	int num;
    	int del;
    	int asdf;
    	string name;
    
    	inv.push_back("Empty");
    	pou.push_back(0);
    
    	vector<string>::const_iterator iter;
    	vector<int>::const_iterator iter2;
    	vector<string>::iterator myIterator;
    
    	cout << "\t\t\t\t*Inventory Wight*\n\n";
    	cout << "Your inventory and pounds:\n";
    
    	asdf = -1;
    		for (iter = inv.begin(); iter != inv.end(); ++iter)
    		{
    			++asdf;
    			cout << asdf << ". " << *iter << endl;
    		}
    	asdf = -1;
    		for (iter2 = pou.begin(); iter2 != pou.end(); ++iter2)
    		{
    			++asdf;
    			cout << asdf << ". " << *iter2 << endl;
    		}
    
    system("pause");
    	return 0;
    }

    I am new, I use this type of code because its the code i know the best to use in C++ in my class.
    Last edited by Rabbit; Mar 27 '13, 08:06 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I'm assuming each vector is the same size? Move the cout line from the second loop into the first loop and get rid of the second loop.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I suggest you use only one vector.

      Each inventory item has a name and a number of pounds so your design should imitate reality. In this case I would have a struct named InventoryItem and the struct members would be a string for the name and a int for the number of pounds.

      Then you only have a vector<Inventor yItem>.

      With two vectors, it's too difficult to make sure the string in the 3rd element stays synched with the int in the 3rd element in the other vector. Especially since vectors can move items around without telling you about it.

      Comment

      • fishirboy
        New Member
        • Mar 2013
        • 48

        #4
        How could I have one vector that would imitate a string and int? I my not be following correctly but do you want me to have both pounds and the item inside the vector? Since the vector can move my items around with out notice how would that help?

        Comment

        • fishirboy
          New Member
          • Mar 2013
          • 48

          #5
          I an only 5 weeks trained in C++ and do not understand how to put a string and int together is there a way to do so???

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You are allowed to group related data items under one name. This is done using a struct or class. These two syntaxes are essentially the same and the difference at 5 weeks into C++ is not important.

            You can do this:

            Code:
            struct InventoryItem
            {
               string name;
               int  weight;
            };
            Then you create InventoryItem variables:

            Code:
            InventoryItem  obj;
            Now you can add values to the struct members:
            Code:
            obj.name = "flour";
            obj.weight = 100;
            Then you can place the object in a vector:

            Code:
            struct InventoryItem
            {
               string name;
               int  weight;
            };
            
            int main()
            {
            vector<InventoryItem> MyStuff;
            
            InventoryItem  obj;
            obj.name = "flour";
            obj.weight = 100;
            
            MyStuff.push_back(obj);  //add item to inventory
            Since a copy of obj is tucked away in the vector, you can now use obj for the next inventory item.

            Comment

            • fishirboy
              New Member
              • Mar 2013
              • 48

              #7
              Thank you sir, I have learned a lot while reading this that are teacher does not want to bother with until 2nd year. I think I can fully build on this to make my low class texted based game.

              Comment

              • fishirboy
                New Member
                • Mar 2013
                • 48

                #8
                There seems to be a small problem, the obj does not like being named as:

                InventoryItem obj;
                obj.name = "flour";
                obj.weight = 100;

                Also is there something wrong with this code? I tried to adjust it to yours weakness, but I feel like i am doing something wrong.

                Code:
                #include "stdafx.h"
                #include <iostream>
                #include <vector>
                #include <string>
                #include <fstream>
                #include <cmath>
                
                using namespace std;
                
                
                	struct InventoryItem;
                {
                	string name;
                	int weight;
                };
                
                int main()
                {
                
                	vector<InventoryItem> MyStuff;
                
                	InventoryItem obj;
                	obj.name = "flour";
                	obj.weight = 100;
                	int num;
                	int del;
                	int asdf;
                	string name;
                
                	MyStuff.push_back(obj); //add item to inventory
                
                	vector<InventoryItem>::const_iterator iter;
                	vector<InventoryItem>::iterator myIterator;
                
                	cout << "\t\t\t\t*Inventory Wight*\n\n";
                	cout << "Your inventory and pounds:\n";
                
                	asdf = -1;
                		for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
                		{
                			++asdf;
                			cout << asdf << ". " << *iter << endl;
                		}
                
                system("pause");
                	return 0;
                }
                Last edited by Rabbit; Mar 28 '13, 06:09 PM. Reason: Please use code tags when posting code.

                Comment

                • fishirboy
                  New Member
                  • Mar 2013
                  • 48

                  #9
                  Sorry rabbit am new thought they just appeared... Little bit dumb on my part. ;)

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    This code:

                    Code:
                       struct InventoryItem;   <<<<---!!!
                      {
                          string name;
                          int weight;
                      };
                    has a semi-colon that needs to be removed.

                    There is no problem assigning "flour" to the string.

                    However, when you display you need to remember that *iter is an InventoryItem and the compiler gags because it does not now how to display a user-defined object. ater you will learn how to do this. For now, you need to display each indivdual member:

                    Code:
                    cout << (*iter).name << "\n" << (*iter).weight;
                    Once I removed that semi-colon and changed the cout to display each member your program took off and ran like a bunny.

                    Comment

                    • fishirboy
                      New Member
                      • Mar 2013
                      • 48

                      #11
                      Thank you sir, now I will be able to make a game for my class with the great way to make the inventory system, I take my hat off to you good sir.

                      Comment

                      • fishirboy
                        New Member
                        • Mar 2013
                        • 48

                        #12
                        Now! There is one final thing before I stop this forum post. I have the code, working just like i need. But i have tried to add 2 objects, one is wool and one is flour. The wool's weight is 50 and the flour is 100 I am trying to make it work but that is my only problem right now, i can mix and use one objects numbers but combine it to have 2 or more.

                        I want to have it so it can spit out:

                        0. flour Weight: 100
                        1. wool Weight: 50

                        I am sorry to ask more questions this is stuff I am still trying to grasp.

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          After you do the push_back of obj, the data is now in the vector leaving obj able to be reused. So just change the values in obj and do a second push_back. Both sets of values will now be in the vector.

                          Your loop that starts with begin() and runs to end() will cycle twice and you should see both sets of values.

                          Comment

                          • fishirboy
                            New Member
                            • Mar 2013
                            • 48

                            #14
                            thanks, works great now!

                            I love you man.

                            Code:
                            #include "stdafx.h"
                            #include <iostream>
                            #include <vector>
                            #include <string>
                            #include <fstream>
                            #include <cmath>
                             
                            using namespace std;
                             
                             
                                struct InventoryItem
                            {
                                string name;
                                int weight;
                            };
                             
                            int main()
                            {
                             
                                vector<InventoryItem> MyStuff;
                             
                                InventoryItem obj;
                            	InventoryItem obj2;
                                obj.name = "flour";
                                obj.weight = 100;
                            	obj2.name = "wool";
                            	obj2.weight = 50;
                                int num;
                                int del;
                                int asdf;
                                string name;
                             
                                MyStuff.push_back(obj); //add item to inventory
                            	MyStuff.push_back(obj2);
                             
                                vector<InventoryItem>::const_iterator iter;
                                vector<InventoryItem>::iterator myIterator;
                             
                                cout << "\t\t\t\t*Inventory Wight*\n\n";
                                cout << "Your inventory and pounds:\n";
                             
                                asdf = -1;
                                    for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
                                    {
                                        ++asdf;
                                        cout << asdf << ". " << (*iter).name << " Weight: " << (*iter).weight << "\n";
                                    }
                             
                            system("pause");
                                return 0;
                            }

                            Comment

                            • fishirboy
                              New Member
                              • Mar 2013
                              • 48

                              #15
                              Code:
                              #include "stdafx.h"
                              #include <iostream>
                              #include <vector>
                              #include <string>
                              #include <fstream>
                              #include <cmath>
                               
                              using namespace std;
                               
                                  struct InventoryItem
                              {
                                  string name;
                                  int weight;
                              	int price;
                              };
                               
                              int main()
                              {
                               
                                  vector<InventoryItem> MyStuff;
                               
                              	//Item 1
                                  InventoryItem obj;
                                  obj.name = "flour";
                                  obj.weight = 100;
                              	obj.price = 10;
                              	//Item 2
                              	InventoryItem obj2;
                              	obj2.name = "wool";
                              	obj2.weight = 50;
                              	obj2.price = 20;
                              	//Item 3
                              	InventoryItem obj3;
                              	obj3.name = "sword";
                              	obj3.weight = 150;
                              	obj3.price = 50;
                              
                              
                                  int num;
                                  int del;
                                  int asdf;
                                  string name;
                               
                                  MyStuff.push_back(obj); //add item 1 to inventory
                              	MyStuff.push_back(obj2); //add item 2 to inventory
                              	MyStuff.push_back(obj3); //add item 3 to inventory
                               
                                  vector<InventoryItem>::const_iterator iter;
                                  vector<InventoryItem>::iterator myIterator;
                               
                                  cout << "\t\t\t\t*Inventory Wight*\n\n";
                                  cout << "Your inventory and pounds:\n";
                               
                                  asdf = 0;
                                      for (iter = MyStuff.begin(); iter != MyStuff.end(); ++iter)
                                      {
                                          ++asdf;
                                          cout << asdf << ". " << (*iter).name << " | Weight: " << (*iter).weight << " | Price: " << (*iter).price << "\n";
                                      }
                               
                              system("pause");
                                  return 0;
                              }

                              Comment

                              Working...