Structs and Vectors quick question

How do I obtain get the below vectors to form in one column under each heading? I want them to be all aligned under each of the headings I created.

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
97

#include <fstream> 
#include <iostream>
#include <vector>
#include <string> 

using namespace std; 

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


int main()

{ 
	// Declared member types

	cout << "Item ID   ";
	cout << "Item Name   "; 
	cout << "pOrdered    "; 
	cout << "manufPrice    "; 
	cout << "sellingPrice    ";

	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);


		for (int h = 0; h < productList.size(); h++)
			cout << productList[h].heading << endl;

		for (int i = 0; i < productList.size(); i++)
			cout << productList[i].itemID << endl;

			cout << endl;

		for (int j = 0; j < productList.size(); j++)
			cout << productList[j].itemName << endl;

			cout << endl;

		for (int k = 0; k < productList.size(); k++)
			cout << productList[k].pOrdered << endl;

			cout << endl;

		for (int l = 0; l < productList.size(); l++)
			cout << "$" << productList[l].manufPrice <<endl;

			cout << endl;

		for (int m = 0; m < productList.size(); m++)
			cout << "$" << productList[m].sellingPrice << endl;

system("pause");
return 0;
}
Use the std::setw() function provided with the iomanip library.

It works by being part of a std::cout statement.

1
2
3
4
std::cout << std::setw(10) << "10" << " spaces reserved from the left and then filled from the right with the next value or statement.\n";

// In the above, 10 spaces are reserved from the left, then 2 are filled from the right with the characters for "10". The line then prints the rest like normal.
// You'll need to reserve more than you need for it to level off.  


        10 spaces reserved from the left then filled from the right with the next value of statement.


If you have overflow (more value characters than reserved space) it will fill up the reserve spaces then just proceed to print to the left anyway.

Also just a point: You defined variables globally as well as locally. You may want to remove the global variables.
I must be missing the syntax completely. I added in the #include <iomanip> at the top, however I have tried a few combinations (pasted the below as an example). However it is not bringing the words to the 2nd column and so forth. It is actually just moving the 1st set of words over 10 spaces.

1
2
3
4
5
6
		for (int h = 0; h < productList.size(); h++)
			cout << productList[h].heading << endl;
			cout << setw(10) << endl;

		for (int i = 0; i < productList.size(); i++)
			cout << setw(10) << productList[i].itemID << endl;
Oh my! I seem to have not checked what you did for output print!
Here's a fix::

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
#include <fstream> 
#include <iostream>
#include <vector>
#include <string> 

using namespace std; 

struct product 
{
			 int itemID; 
			 string itemName; 
			 int pOrdered;  
			 int manufPrice; 
			 int sellingPrice;
			 string heading;
}; 


int main()

{ 
	// Declared member types

	cout << "Item ID   ";
	cout << "Item Name   "; 
	cout << "pOrdered    "; 
	cout << "manufPrice    "; 
	cout << "sellingPrice    ";

	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);

                // You need to do the printing for an object within the same line.
                // The Command line prints left to right first
                // And does not support going back up.
                // If you want better alignment, use setw()
		for (int i = 0; i < productList.size(); i++)
			cout << endl << productList[i].heading << " "
				<< productList[i].itemID << " "
				<< productList[i].itemName << " "
				<< productList[i].pOrdered << " $"
				<< productList[i].manufPrice << " $"
				<< productList[i].sellingPrice;

		cout << endl;
system("pause");
return 0;
}
Last edited on
I have this working now. How does one input data into the vectors from a file?

I want to be able to provide another option to update records which will than output to a file that I have saved as a.txt. So basically I can include an else statement under my if which gives the user the option to "update records."

Not sure how to get this going... I have a basic work in.. I would be able to insert this after my main programs main statement.

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

{

string temp;
infile.open("Inventory.txt");

if(infile)

{

do

{

getline(infile,temp);

wordlist.push_back(temp);

}


Here is my program as it is now. It is working well right now.

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

#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;
}

Topic archived. No new replies allowed.