Implementing a Stack

Hi,
I have used a struct to create this program but don't know how to implement a stack for the below program. Please your help will be much helpful.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;


struct items_buying {
	
	string productID;
	int proQuantity;
	float unitPrice;
	float lineTotal;
	string prodDescription;

} items [3];

void printitems (items_buying items);

void welcomemessage() 
{
	cout<< setfill(' ')<< setw(27) << " " << setw(15) << " Welcome to Steve's Shop"<< endl<< endl<< endl;
	cout<< setfill(' ')<< setw(15) << " " << setw(15) << "INSTRUCTIONS: Please enter the item description, "<< endl;
	cout<< setfill(' ')<< setw(29) << " " << setw(15) << "quantity and then price per item."<< endl;
};
char userAnswer;

int main ()
{
start_program:
	
	string myString;
	// variable to store intProductId for the generation of auto key ID
	int intProductId = 0;

	// a float variable to store totalPriceItems
	float totalPriceItems = 0;

	// string variable to store strID
	string strID;
	int n = 0;

	welcomemessage();
	cout<< endl;
	//strProductId =  "P";
	for(n = 0; n<3;n++)
	{
		cout<< "================================================================================"<< endl;
		// 1st input
		//intProductId++;
		//items[n].productID = intProductId;
		// increment intProductId by 1
		intProductId++;

		// C++11 function overload for integer convesion to string
		strID = "P0000"+to_string(intProductId);
		items[n].productID = strID;

		// 2nd input
		cout << "Enter product description: ";
		getline (cin,items[n].prodDescription);
		cout<< endl;

		// 3rd input
		cout << "Enter Quantity: ";
		getline(cin,myString);
		stringstream(myString) >> items[n].proQuantity;
		cout<< endl;

		// 4th input
		cout<< "Enter unit price: "<< char(156)<< " ";
		getline(cin,myString);
		stringstream(myString) >> items[n].unitPrice;
		items[n].lineTotal = items[n].proQuantity * items[n].unitPrice;
		totalPriceItems += items[n].lineTotal;

		cout<< "================================================================================"<< endl;
		// then clear the screen for new input
		system("CLS");
	  }
  
  


	cout << endl<< "You have entered these items:"<< endl<< endl;
	// organise the table
	cout << setfill(' ')<< setw(1) << "|" << setw(15) << left<< "Product ID"<< setw(1)<< "|"<< setw(15)<< left<< "Description"<< setw(1) << "|" << setw(5)<< left<< "Quantity"<< setw(2)<< " "<< setw(1)<< "|"<< setw(6)<< left<< "Unit Price"<< setw(2)<< " "<<setw(1)<< "|" << setw(15)<< left << "Line Total"<< endl;
	
	// output the items entered
	for (n=0; n<3; n++)
	{
		printitems (items[n]);
	}
  
	
	 
	cout<< setfill('-')<< setw(1)<< "+"<< setw(15)<< "-"<< setw(1) << "+"<< setw(15)<< "-"<< setw(1)<< "+"<< setw(10)<< "-"<< setw(1)<< "+" << setw(12)<< "-"<< "+" << setw(12)<< "-"<< "+" << endl;
	
	
	cout<< setfill(' ')<< setw(50)<< " "<< right<< "Subtotal "<< char(156)<< "  "<< totalPriceItems<< endl;
	cout<< "Tax is: 20%"<< endl;
	float totalDue = 0.00, taxAcc = 0.00;
	totalDue = (1.20 * totalPriceItems);
	
	cout<< setfill(' ')<< setw(49)<< " "<< right<< "Sales Tax "<< char(156)<< "  "<< (totalDue - totalPriceItems)<< endl;
	cout<< setfill(' ')<< setw(49)<< " "<< right<< "Total Due "<< char(156)<< "  "<< totalDue<< endl;


	cout<< "Do you want to start again? ";
	cin>> userAnswer;
	if (userAnswer=='y'|| userAnswer =='Y')
	{
		goto start_program;
	}
	else
	{
		goto end_program;
	}

end_program:
	cout<< "Press Enter to exit...";
		cin.ignore();

}

// print function to display all the items bought
void printitems (items_buying items)
{
	
	cout<< setfill('-')<< setw(1)<< "+"<< setw(15)<< "-"<< setw(1)<< "+"<< setw(15)<< "-"<< setw(1) << "+"<< setw(10)<< "-"<< setw(1)<< "+"<< setw(12)<< "-"<< setw(1)<< "+" << setw(12)<< "-"<< "+" << endl;
	cout<< setfill(' ')<< setw(1)<< "|"<< setw(15)<< left<< (items.productID)<< setw(1)<< "|"<< setw(15)<< left<< items.prodDescription<< setw(1)<< "|"<< items.proQuantity<< setw(1)<< setw(8)<< " "<< "|"<< setw(4)<< left<< setprecision(6)<< items.unitPrice<< setw(6)<< " "<< setw(1)<< "|"<< setw(12)<< right<< items.lineTotal<< "|"<< endl;
	
}
Last edited on
Topic archived. No new replies allowed.