Thread: cin and formating

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    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)


    Code:
     
     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
    Location
    California
    Posts
    3,268
    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:

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

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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
    Posts
    696
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed