|
-
January 22nd, 2015, 03:41 PM
#1
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|