cin and formating

This is a discussion on cin and formating within the C++ Programming forums, part of the General Programming Boards category; We are now beginning C++ How do i format d string that cin is going to print to the screen.It's ...

  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,246
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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

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