Thread: cin and formating

  1. #1
    Registered User
    Join Date
    Oct 2003

    Arrow cin and formating

    We are now beginning C++
    How do i format d string that cin is going to print to the screen.It's printing down some numbers like
    12" ends: 2
    8" ends: 1
    1" end: 1

    but i want it to align them so it's better formated(I belive we are suppose to use cin for now,but any help is appreciated)

     while ( height > 0 ) {
       for (int i = 12; i >= 2 && height != 0; i -= 2) {
    		if ( height >= i ) {
    		  std::cout << i << "\" ends: " << height/i << std::endl;
    	height = height%i;

  2. #2
    Registered User
    Join Date
    Sep 2004
    Are you looking for a way to line up what gets printed to the screen? Maybe try the following, and see if that is what you are looking for:

    std::cout << i << "\" \tends:\t" << height/i << std::endl;

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Formatting with cout is done by outputting certain fromatting characters. There are escape sequences: "\n" for example would print a newline. Above you saw the tab escape sequence. The \ starts an escape, so if you want to actually output the backslash, you need to use \\. You may also see the constant, "endl" get outputted between it's own set of insertion operators, and this has the same effect as the \n character in a string.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    source: compsci textbooks,, world wide web, common sense

Popular pages Recent additions subscribe to a feed