I've encountered a very strange event that happens when running my DisplayMenu function, it seems to run a certain line only once, heres the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void showMenu(menuItemType menuList[])
{
for(int i = 0; i < NUM_ITEMS; i++)
{
std::stringstream * ss = new std::stringstream;
int TempLoopSize = TAB - menuList[i].Name.length(); //Tab is a constant, this equation finds the right number of spaces to use
for(int x=0;x<TempLoopSize;x++){ *ss << ' ';} //adds a ' ' to the stringstream x times
std::cout << " "; //why does this command run only once?!
std::cout << menuList[i].Name << ss->str(); //outpost results
std::cout << menuList[i].Price << std::endl;
delete ss; //free memory
}
}
What this outputs when the function runs:
Plain Egg 1.45
Bacon and Egg 2.45
Muffin .99
French Toast 1.99
Fruit Basket 2.49
Cereal .69
Coffee .5
Tea .75
desired output:
Plain Egg 1.45
Bacon and Egg 2.45
Muffin .99
French Toast 1.99
Fruit Basket 2.49
Cereal .69
Coffee .5
Tea .75
For an object with a short life time (till the current local scope is exited), use an automatic storage duration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for(int i = 0; i < NUM_ITEMS; i++)
{
// std::stringstream * ss = new std::stringstream;
std::stringstream ss ;
// use ss
// *ss << ' ';
ss << ' ' ;
// ...
// delete ss; //free memory
}
Thanks for the help on shotening the code, but im getting the same output. Only the first line is indented then everything else is printed normally, i just dont understand...