Jump to content

width()

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
kumamako

kumamako

    Newbie

  • Members
  • PipPip
  • 16 posts
hello. This the program from the book. I am having a hard time understanding the code. I looked at the program output and tried to see what it is doing but no luck. can someone explain me what this program does.

CODE
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
int widthValue = 4;
char sentence[ 10 ];

cout << ”Enter a sentence:” << endl;
cin.width( 5 ); // input only 5 characters from sentence


while ( cin >> sentence )
{
       cout.width( widthValue++ );
       cout << sentence << endl;
       cin.width( 5 ); // input 5 more characters from sentence
    }
    return 0;

Edited by Jaan, 03 August 2009 - 11:46 AM.
Please use code tags when you are posting your codes!


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What input/output are you seeing?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Alright, so here's what's going on:

First your program creates an int and a char array with 10 characters, called "widthValue" and "sentence", respectively. The program then prompts you for a sentence, and sets cin to only extract up to five characters on the next extraction (always including a null terminating character, \0).

The program then starts the while loop, which has a cin extraction which prompts the user to place in some text. All of this text is allocated to the cin character buffer, which is sort of a repository for characters that the user inputs before being extracted and used by your program. While cin already has all the characters, it has not given your program any of them! The following extraction operator then removes the characters and places them into the sentence character array (four characters from the buffer and one null-terminating character). It should be noted that if there is a whitespace character (such as a space, tab, or newline) before those four characters are extracted, that cin, by default, treats those characters as terminating characters and will only return the characters found before the whitespace character.

After that, cout has it's width set to widthValue (which after this expression evaluates, widthValue will increment due to the postfix operator), and is given a pointer to the sentence array (which evaluates to all the characters sentence points to). Then, cout prints those characters (which happen to be four, since cin only extracted four characters + one null-terminating character, which equals 5) at the width given prior. cin is again set to a width of 5 (so that only four characters will be extracted again at most), and the circle repeats until the cin buffer is empty (which the user is then, again, prompted for more input).

As the loop progresses, widthValue continues to increase. As such, the distance those extracted letters are from the left of the console window continues to increase. For example, if you only input four characters into cout while it's width value has been given a "10", then cout will simply place six whitespace "spaces" in front of the characters being printed out on the console. As thus, it appears that the sentence slowly makes it's way across the screen.

I hope that made some sense, I tried explaining it the best I could. The only way to turn off the program is to press the "x" on the top left of the console window.