Reputation: 153
In the code below, I'm attempting to output a list of pizza toppings using the getToppings() method. I'd like to clear this listing of toppings at the end of each inner 'while' loop (i.e. each new pizza order must be initiated with no toppings). Unfortunately, I have found no way to clear my listOfToppings string. When I explicitly use the .clear() method, the last topping of the previous pizza is transferred to the new pizza. How can I clear the listOfToppings for each new pizza?
int main()
{
PizzaOrder pizza;
string size;
string toppingSelection;
bool innerLoop = true;
bool outerLoop = true;
int currentToppingsNum;
// Outer loop
while (outerLoop)
{
// Outer loop
...
// Inner loop
while (innerLoop && currentToppingsNum < 5)
{
pizza.PizzaOrder::getToppings(list);
cout << "Current Pizza: " << pizza.stringizeSize() << list << "\n\n";
cout << "Select an item by number (0 when done): \n\n";
...
cout << "Selection: ";
...
...
pizza.displayPizza();
} list.clear();
}
return 0;
}
...
void PizzaOrder::getToppings(string &listOfToppings)
{
for (int i = 0; i < numToppings; i++)
listOfToppings = listOfToppings + " + " + toppings[i];
}
Upvotes: 1
Views: 1774
Reputation: 73490
You can avoid the need for an explicit clear
by structuring your code slightly differently.
Since each order is a new pizza, you should create a PizzaOrder object inside the outer loop.
int main() {
while(true) {
...
if ( size[0] == 'q' || size[0] == 'Q' ) {
break;
}
PizzaOrder pizza;
...
pizza.displayPizza();
}
}
However as Matt McNabb alludes to in the comments, there's a lot of other issues with the original code.
Running this through an online checker (http://www.gimpel.com/html/pcl.htm) I get the following warnings / info
PizzaOrder::getSize(void)
could be made conststd::getline
PizzaOrder::setSize(int)
pizza.toppingsOffered[i]
should be PizzaOrder::toppingsOffered[i]
Ignoring return value of function PizzaOrder::addTopping(int)
Last line of this does nothing
PizzaOrder::PizzaOrder() {
numToppings = 0;
size=DEFAULT_SIZE;
toppings;
}
Declaration of symbol 'size' hides symbol 'PizzaOrder::size', Last value assigned to variable 'size' not used in
PizzaOrder::PizzaOrder(int size) {
if(!setSize(size))
size=DEFAULT_SIZE;
}
Return value from PizzaOrder::addTopping(string topping)
and PizzaOrder::addTopping(int n)
- Implicit conversion to Boolean,
if (arraySize == 5)
always evaluates to TrueselectedTopping
is referenced only by its constructor or destructorAnd a bunch more.
One final thought:
Initial code looked like this
while (outerLoop) {
string list;
...
while (...) {
PizzaOrder::getToppings(list);
...
}
list.clear();
}
getToppings
is adding to the list, and clear is clearing a string that is about to be destroyed anyway.
Better would be
while (outerLoop) {
string list;
...
while (...) {
list.clear();
PizzaOrder::getToppings(list);
...
}
}
so that the list is cleared each time.
But better yet would be to
e.g.
string list = PizzaOrder::getToppings(list);
while (outerLoop) {
...
while (...) {
...
}
}
Upvotes: 1