Command runs only once in a program

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


Any help is appreciated, thanks!
Last edited on
Use <iomanip> for formatting:

1
2
3
4
5
6
7
8
9
10
11
#include <iomanip>

void showMenu( menuItemType menuList[] )
{

    for(int i = 0; i < NUM_ITEMS; i++)
    {
        std::cout << "   " << std::setw(TAB) << std::left << menuList[i].Name
            << std::fixed << std::setprecision(2) << menuList[i].Price << '\n' ;
    }
}


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...

new function:
1
2
3
4
5
6
7
8
9
10
void showMenu(menuItemType menuList[])
{

    for(int i = 0; i < NUM_ITEMS; i++)
    {
        std::cout << "   " << std::setw(TAB) << std::left << menuList[i].Name
         << std::fixed << std::setprecision(2) << menuList[i].Price << std::endl;
    }

}
Last edited on
So everything else is looping correct?
This code produced the desired output for me so don't know why it's not working for you?

As a test instead of std::cout << " "; maybe std::cout << "xxxx"; to see if it prints.

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

//using namespace std;

const int NUM_ITEMS = 3;
struct menuItemType
{
    std::string Name;
    double Price;
};

const int TAB = 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
    }
}


int main (int argc, char *argv[])
{
  menuItemType menuList[NUM_ITEMS];
  menuList[0].Name = "Plain Egg";
  menuList[0].Price = 1.45;
  menuList[1].Name = "Bacon Egg";
  menuList[1].Price = 1.70;
  menuList[2].Name = "Muffin";
  menuList[2].Price = 0.99;

  showMenu(menuList);

  return 0;
}

Topic archived. No new replies allowed.