CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: setw( ) I almost understand, but I'm getting some weird results.

  1. #1
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    2

    setw( ) I almost understand, but I'm getting some weird results.

    I'm using TutorialsPoint C++ examples to figure some things out, and though I've figured out what I can, I cannot tell why my output isn't going the way I want with the setw() "method"?

    As you can see in the code I've c/p'ed, I'm trying to get the Book info to align with the first line of information after "Book 1:"

    I've kept the Book 2 section of the output as is in case I want to start over, never mind it.

    MY question is, the setw() only responds after I put in a high enough value. For the first setw() value it wouldn't begin moving "Book1.author" until the value was 11 or higher. The second setw() value didn't begin moving "Book1.subject" until the value was 28 or higher, but as you can see, "Book1.book_id" responded properly to setw(15). It didn't follow a trend that I can see, so I'm at a loss as to where it is getting it's marching orders outside of my setw() values.

    Also, thanks for any help. I'm positive my lingo is off, please forgive me. As I progress, the questions will be worded with the proper terminology.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cstring>
    #include <iomanip>
     
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;
     
    struct Books
    {
       char  title[50];
       char  author[50];
       char  subject[200];
       int   book_id;
    };
     
    int main( )
    {
       struct Books Book1;        // Declare Book1 of type Book
       struct Books Book2;        // Declare Book2 of type Book
     
       // book 1 specification
       strcpy( Book1.title, "Learn C++ Programming");
       strcpy( Book1.author, "Chand Miyan"); 
       strcpy( Book1.subject, "a huge ****ing subject line");
       Book1.book_id = 6495407;
    
       // book 2 specification
       strcpy( Book2.title, "Telecom Billing");
       strcpy( Book2.author, "Yakit Singha");
       strcpy( Book2.subject, "Telecom");
       Book2.book_id = 6495700;
     
       // Print Book1 info
       cout << "Book 1: " << Book1.title << endl;
       cout << setw(19) << Book1.author << endl;
       cout << setw(45) << Book1.subject << endl;
       cout << setw(15) << Book1.book_id << endl;
    
       // Print Book2 info
       cout << "Book 2 title : " << Book2.title << endl;
       cout << "Book 2 author : " << Book2.author << endl;
       cout << "Book 2 subject : " << Book2.subject << endl;
       cout << "Book 2 id : " << Book2.book_id << endl;
    
       return 0;
    }
    Thanks

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: setw( ) I almost understand, but I'm getting some weird results.

    setw() sets the width for the next element in the stream ie the number of character positions in which the value should be output. If the value to be output is narrower than the specified width then fill characters are inserted as padding. A value wider than the designated width will not be truncated. If you want the book info to align, try something like this

    Code:
    	// Print Book1 info
    	cout << "Book 1: " << Book1.title << endl;
    	cout << setw(8) << " " << Book1.author << endl;
    	cout << setw(8) << " " << Book1.subject << endl;
    	cout << setw(8) << " " << Book1.book_id << endl;
    You could also use tabs like this
    Code:
    	// Print Book1 info
    	cout << "Book 1: " << Book1.title << endl;
    	cout << "\t" << Book1.author << endl;
    	cout << "\t" << Book1.subject << endl;
    	cout << "\t" << Book1.book_id << endl;
    I would also suggest that you use type string in your struct rather than fixed size char arrays. See http://www.cplusplus.com/reference/string/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  3. #3
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    2

    Re: setw( ) I almost understand, but I'm getting some weird results.

    Thanks. I got it.

    I saw it used here:

    Code:
    // output each array element's value                      
       for ( int j = 0; j < 10; j++ )
       {
          cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
       }
     
       return 0;
    }
    I didn't see the << "[insert character here]" <<... and so on; not in simple terms at least. Matter of fact, even after seeing your use of it in your example, it only clicked a couple hours later that the << " " << designates the character to use in the setw() execution.

    So, I understand that you have initialize(correct term?) setw() with the desired character that is going to repeated for the length of the value.

    Awesome. Thanks 2kaud.

    Also, in these examples I'm copying and pasting, I'm simply working on the bits that apply to my query. I have not heard the part about type string in structs yet, I'll make a note.

    Though I've heard of the "\t" sequence, I didn't even think to try it here. I'm newer than new.

    Again, thanks.

    I became a member here when all of my questions were closed nearly immediately on Stack. Thanks for the direct advice. If I can improve my placement within the forum of the threads I start, I will. And as always, I do a pretty exhaustive search of the terminology I'm familiar with before starting threads.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: setw( ) I almost understand, but I'm getting some weird results.

    Code:
    cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
    what this does is output j in width of 7 and n[j] in a width of 13

    << designates the character to use in the setw() execution.
    In this context << is the stream insertion operator. It inserts into the specified output stream (cout in this case) the value of what follows to its right or sets formatting as specified by an io manipulator.

    setw(n) sets the field width for the following output where n is the required minimum width of the following value. See http://www.cplusplus.com/reference/iomanip/

    There are also other format manipulators that can be used. See http://www.cplusplus.com/reference/ios/


    /t is an example of an escape sequence. There are several that can be used. See https://msdn.microsoft.com/en-us/library/h21280bw.aspx
    Last edited by 2kaud; January 23rd, 2015 at 05:45 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)