Help with Console output

This is a discussion on Help with Console output within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create a function that shows the players inventory. I am having trouble creating a box ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    Help with Console output

    Hi, I am trying to create a function that shows the players inventory. I am having trouble creating a box around the desplay without the sides being pushed out too far depending on the text size. here is the code i am using:

    Code:
    void character::showItems(void)
    {
    	using namespace std;
    	cout << "Items\n";
    	cout << "#################################################\n";
    	cout << "# \tItem Name\t\t   Quantity\t#\n";
    	cout << "# \t---------\t\t   --------\t#\n";
    	int counter = 1;
    	for (int i = 0; i < int(character::inventory.size()); i++)
    	{
    		if (character::inventory[i].quantity)
    		{
    			cout << "# " << counter << ". " << character::inventory[i].name << "\t\t" << character::inventory[i].quantity << "\t#\n";
    			counter++;
    		}
    	}
    	character::checkInventorySpace(); // calculates current inventory space
    	cout << "#\t\t\t\t\t\t#\n";
    	cout << "#\t\t\t\tTotal: " << character::currentInventorySpace << '/' << character::maxInventorySpace << "\t#\n";
    	cout << "#################################################\n";
    }
    This is not homework. I am currently working with a team to create a text based game on our offtime here at Fullsail.

    Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    cout has a member function width() that sets column width. Maybe you can use that somehow, and get rid of the embedded tabs?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by IfYouSaySo
    cout has a member function width() that sets column width. Maybe you can use that somehow, and get rid of the embedded tabs?
    hmm, any chance I can get some help on how to use this member fuction? the help index dosn't have anything on it

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > cout << "# " << counter << ". "
    Use a string stream to generate the formatted output in memory first, where you can then measure it's length and calculate the amount of trailing space needed to put the end # in the right place.

    Or perhaps look at setw() in the i/o manipulators
    #include <iomanip>

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    I tried using the setw() and setfill , but that ran into the same problem with the spacing becoming dependent on the size of the item string. I guess I will have to try to the string idea; however fullsail tells us to avoid strings so im sure this will be a fun learning experience. heh
    Last edited by JeremyCAFE; 12-20-2005 at 09:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output string to both console and file at the same time
    By GOBLIN-85 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2008, 05:52 AM
  2. How to get the console output
    By outlawbt in forum Windows Programming
    Replies: 2
    Last Post: 03-19-2008, 02:25 PM
  3. redirect console output to a String?
    By TwistedMetal in forum C Programming
    Replies: 10
    Last Post: 02-26-2008, 01:54 PM
  4. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  5. Redirecting console output
    By _hannes in forum Windows Programming
    Replies: 3
    Last Post: 11-04-2004, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21