Help with a Switch that calls back to previous input

Ok so basically I have a switch with 6 options. The first 5 are selecting a shape. In the first 5 cases it will calculate the area of the selected shape. Now the 6th option would be to allow a user to add the total area of the shapes they had selected so far. So say the selected 1, 4, and 5. Then they go to main menu and select 6. It would calculate the total area of 1, 4, and 5 and display each shape listed inside 1, 4, and 5. EG. Circle, Rectangle, Square. So it would go.
Circle
Length = Previous Input
Width = Previous Input
Area = Previous calculated area
Rectangle
Same as above
Circle
Same as above
Total area
Total of all of the three above.
Only thing is, I can't figure out how to get the 6th switch case to display what the user selected previously. Any help would be GREATLY appreciated.

Edit:

Ok this is what I currently have.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string>
#include <iomanip>
#include <vector> 

using namespace std;

double SLength, SWidth, SArea, RLength, RWidth, RArea, radius, CArea, base, height, TArea, total_area, labor, carpet, total, taxcost; 
const double pi = 3.14; 
const double cost = 2.45;
const double tax = .085;
vector<int> choices;


void AreaSquare() //creating an equation to be called to in my switch for fahrenheit conversion 
{	
		SArea = pow(SLength,2.0); 
}

void AreaRectangle() //creating an equation to be called to in my switch for fahrenheit conversion 
{	
		RArea = (RWidth * RLength); 
}

void AreaCircle() //creating an equation to be called to in my switch for fahrenheit conversion 
{	
		CArea = pi * pow(radius,2.0);  
}

void AreaTriangle() //creating an equation to be called to in my switch for fahrenheit conversion 
{	
		TArea = 0.5 * base * height;  
}



int main()
{



	unsigned short choice;

	do 
	{
		cout << "Please choose from the following: " << "\n";
		cout << "1: Square"<< "\n";
		cout << "2: Rectangle"<< "\n";
		cout << "3: Circle"<< "\n";
		cout << "4: Triangle"<< "\n";
		cout << "5: Total Area"<< "\n"; 
		cout << "0: To Exit the Program"<< "\n"; 
		cin >> choice; 
		system ("cls"); 

		switch (choice)
		{

		case 1: 
			cout << "Please enter the Length: ";
			cin >> SLength; 
			
			
			AreaSquare();

			cout << "The area of the Square is " << SArea << "\n";
			total_area += SArea;

			system ("pause"); // pauses the program 
			system ("cls"); //clears the screen
			break; 

		case 2: 
			cout << "Please enter the Length: "; 
			cin >> RLength;

			cout << "Please enter the Width: ";
			cin >> RWidth; 

			AreaRectangle(); 

			cout << "The area of the Rectangle is " << RArea<< "\n"; 
			total_area += RArea;

			system ("pause"); // pauses the program 
			system ("cls"); //clears the screen
			break; 


		case 3: 
			cout << "Please enter the Radius of the Circle: "; 
			cin >> radius; 

			AreaCircle(); 

			cout << "The area of the Circle is: " << CArea<< "\n"; 
			total_area += CArea;

			system ("pause"); // pauses the program 
			system ("cls"); //clears the screen
			break;


		case 4: 
			cout << "Please enter the base of the Triangle: "; 
			cin >> base; 

			cout << "Please enter the height of the Triangle: "; 
			cin >> height; 

			AreaTriangle();

			cout << "The area of the Triangle is " << TArea << "\n";
			total_area += TArea;

			system ("pause"); // pauses the program 
			system ("cls"); //clears the screen
			break;


		case 5: 
			for ( size_t i = 0; i < choices.size(); i++ )
			{
      if ( choices[i] == 1 ) 
	  {
          cout << "Shape: Square" << "\n"; 
		  cout << "Length: " << SLength << "\n"; 
		  cout << "Area: " << SArea << "Square Feet" << "\n"; 

      }
	  else if ( choices[i] == 2 ) 
	  {
          cout << "Shape: Rectangle" << "\n"; 
		  cout << "Length: " << RLength << "\n"; 
		  cout << "Width: " << RWidth << "\n";
		  cout << "Area: " << RArea << "Square Feet"<< "\n"; 
      }
	  else if ( choices[i] == 3 ) 
	  {
          cout << "Shape: Circle" << "\n"; 
		  cout << "Radius: " << radius << "\n"; 
		  cout << "Area: " << CArea << "Square Feet" << "\n"; 
      }
	  else if ( choices[i] == 4 ) 
	  {
          cout << "Shape: Triangle" << "\n"; 
		  cout << "Base: " << base << "\n";
		  cout << "Height: " << height << "\n";
		  cout << "Area: " << TArea << "Square Feet" << "\n"; 
      }
      
			}


			labor = (total_area / 50) * 35.72;
			carpet = (total_area * 2.45); 
			taxcost = (labor + carpet) * tax; 
			total = (labor + carpet + taxcost) ;

		 cout << fixed << showpoint << setprecision(2);
		 cout << "Total area: $" << total_area << " Square Feet " << "\n"; 
		 cout << "Carpet Cost: $" << carpet << "\n"; 
		 cout << "Labor cost: $" << labor << "\n"; 
		 cout << "Tax: $" << taxcost << "\n"; 
		 cout << "Total Cost: $" << total << "\n"; 


		case 0:		//case in case user selects 0 the program will exit
			cout << "Program is terminating..."<< endl; 
			
			system ("pause");
			return 0;
			break; //causes the prgram to execute the next statement outside the switch


		default: //the third case incase the user enters an invalid option

			cout << "That is not an option!" << endl;
			cout << "Please try again." << endl;

			cout << "\n" << endl;
			cout << "\n" << endl;
			cout << "\n" << endl;

			system ("pause"); // pauses the program 
			system ("cls"); //clears the screen
			break; //causes the program to execute the next statement outside the switch 
		}




		}	while (choice != 0);


}
Last edited on
Hi,
you can have a std::vector<double> where you store the areas of the selected figures. When option 6 is selected, you just print the sum of the values which are stored in the vector.
Hey man, we haven't quite gotten into vectors yet. To build up the output as requested format, you should define a string variable in main(), for example, string output then pass it to each user-defined function as a reference such that the information of the shape can be added together to the output to display them in main(). For example, double areaSquare(string& output) is a recommendation from the prof in how to do this but I haven't been able to figure out yet how to assimilate this into the code and have it work appropriately
Do you really have to print out the previous details when the user selects option 6, or do you just have to print out the total area? The latter would be a whole lot easier because, as minomic points out, you could just keep a running total of the area of the figures.

Otherwise you could do this with a Shape class that has derived classes for Square, circle etc, and a collection of pointers to the previously entered shapes, but if you haven't studied vectors then I doubt you've studied this stuff.
Topic archived. No new replies allowed.