Cout control

This is a discussion on Cout control within the C++ Programming forums, part of the General Programming Boards category; I'm working at the Exercises at the end of chapter 4 of my old C++ instruction book, and the first ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Cout control

    I'm working at the Exercises at the end of chapter 4 of my old C++ instruction book, and the first one which has the out put of ints be limited to 10 columns and lines seemed easy untill I tried to have the numbers line up right. I need to find out the right way to control the output so short and long numbers line up right.
    Is there some instructions on that?

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You could use if() statements to find out how much space they need:
    Code:
    if(number < 10)
    	cout << "    " << number;
    else if(number < 100)
    	cout << "   " << number;
    else if(number < 1000)
    	cout << "  " << number;
    else if(number < 10000)
    	cout << " " << number;
    If you're using decimals then my method may not work too well lol. Good luck soldier!

  3. #3
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Try out std::setw().

    Example code:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    	unsigned int num1 = 51234511;
    	unsigned int num2 = 12;
    
    	std::cout << std::setw(10) << num1 << std::endl << std::setw(10) << num2 << std::endl;
    	std::cin.get();
    
    	return 0;
    }
    My output:
    Code:
      51234511
            12
    Last edited by Kybo_Ren; 01-16-2005 at 07:31 PM. Reason: Added output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Spying" on cout
    By sirjis in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2006, 05:00 PM
  2. tab control
    By tyouk in forum Windows Programming
    Replies: 6
    Last Post: 02-07-2005, 11:47 PM
  3. I'm REALLY confused. Why isn't cout or cin working here?
    By niudago in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2003, 05:53 PM

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