Inputting data into vectors assistance

If I wanted to put each of the products I have in the below code into a .txt file how would I input that into the vectors? Pretty lost and confusing actually. I want to have a function that will input data into the vectors, and than display the menu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96


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

struct product 
{
			 int itemID; 
			 string itemName; 
			 int pOrdered;  
			 int manufPrice; 
			 int sellingPrice;
			 string heading;
}dishWasher, microWave, cookingRange, circularSaw, headings; 


int main()

{ 
	char response; 
	// Declared member types


	product dishWasher;
	dishWasher.itemID = 1111; 
	dishWasher.itemName = "Dish Washer"; 
	dishWasher.pOrdered = 20;
	dishWasher.manufPrice = 250.50; 
	dishWasher.sellingPrice = 550.50;
	
	product microWave;
	microWave.itemID = 2222;
	microWave.itemName = "Microwave"; 
	microWave.pOrdered = 75;
	microWave.manufPrice = 150.00; 
	microWave.sellingPrice = 400.00;

	product cookingRange;
	cookingRange.itemID = 3333;
	cookingRange.itemName = "Cooking Range"; 
	cookingRange.pOrdered = 50;
	cookingRange.manufPrice = 450.00; 
	cookingRange.sellingPrice = 850.00;

	product circularSaw;
	circularSaw.itemID = 4444;
	circularSaw.itemName = "Circular Saw"; 
	circularSaw.pOrdered = 150;
	circularSaw.manufPrice = 45.00; 
	circularSaw.sellingPrice = 125.00;

	
	// Declared member functions

		vector<product> productList; 
		productList.push_back(dishWasher);
		productList.push_back(microWave);
		productList.push_back(cookingRange);
		productList.push_back(circularSaw);


		cout << "Please select from the following options: "<<endl <<endl; 
		cout << "A - Print report" << endl; 
		cin  >> response; 

		if (response == 'A' )
		{

		cout << "Item ID   " << setw(10);
		cout << "Item Name   "<< setw(10); 
		cout << "pOrdered    "<< setw(10); 
		cout << "manufPrice    "<< setw(10); 
		cout << "sellingPrice    "<<endl;

		for (int i = 0; i < productList.size(); i++)

			cout <<productList[i].itemID    << setw(10)
				 <<productList[i].itemName  << setw(10)
				 <<productList[i].pOrdered  << setw(10)<<"$"
				 <<productList[i].manufPrice<< setw(10)<<"$"
				 <<productList[i].sellingPrice <<endl;

			cout << endl;

		}
		else if  ( response != 'A' )
			cout << "Thank you for using our system" << endl; 


system("pause");
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

struct Product
{
	int itemID;
	string itemName;
	int pOrdered;
	float manufPrice; // These are floats
	float sellingPrice; // Floats
	string heading;
};

void menu(); // Make a function for your menu.
void readInData(std::ifstream&, std::vector<Product>&); // Function for read in from files.

int main()
{
	std::ifstream fin("Filename.txt"); // Load the file
	std::vector<Product> productList; // Initialize the vector.

	readInData(fin, productList); // Call the function
}

void readInData(std::ifstream& fin, std::vector<Product>& pL) // Pass BOTH by REFERENCE
{
	Product tmp; // temporary storage for our read in values.
	while (!fin.eof()) // While we haven't seen the end of the file marker
	{
		// Filename.txt should be in this order for this to work
                // Read in the numbers first
		fin >> tmp.itemID >> tmp.pOrdered >> tmp.manufPrice
			>> tmp.sellingPrice;
                // Read in the item's name using getline()
		std::getline(fin, tmp.itemName);

                // Put in vector.
		pL.push_back(tmp);
	}
}


This is the order REQUIRED for the function I define above.
If you try to use fin>> with the itemName and it has a space in the input it will only take the first part before the space.
std::getline avoids this.
Otherwise you'll get a nasty error trying to place strings into integers.

Filename.txt:
1111	20		250.50 		550.50 		Dish Washer
2222	75		150.00 		400.00 		Microwave
3333	50		450.00 		850.00 		Cooking Range
4444	150		45.00 		125.00 		Circular Saw

Topic archived. No new replies allowed.