Simple formatting question..

This is a discussion on Simple formatting question.. within the C++ Programming forums, part of the General Programming Boards category; New here, thought I'd start off with something easy. Ok I have this function that converts an (int) into a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Simple formatting question..

    New here, thought I'd start off with something easy.

    Ok I have this function that converts an (int) into a 4 octect IP address, storing each octet of the IP into an array. To output this as a normal IP address, I have to add the dots between each number. The code goes as thus ( all you really need to note is the cout statement at the bottom )-

    Code:
        int i = 0;
        int n = 24;
        IP octet[4];  //unsigned int
    
        for ( i = 0 ; i < 4 ; i++)
        {
            octet[i] = ( ( address >> n ) & 255 );
            n -=8;
        }
    
    cout << octet[0] << "." << octet[1] << "." << octet[2] << "." << octet[3];
    So the output looks something like 192.24.159.1, just as an example.

    My question is, I am going to output this in a list with multiple lines, and will be outputting items after this on the same line. So I need to set width to 16 and align this IP to the left, then output the rest of the line uniformly. However, I am unsure how to do this, since the setw() function only grabs the next item on the output stream ( in this case, octet[0] ), and the rest will be pushed a dozen or so spaces over. I need to find a way to make it so this entire bit of code ( the cout statement of this function, basically ) is aligned to the left of a 16-space spot. Not sure if this would involve changing my output, but I'm up for any suggestions.

    Sorry if this description is not adequate, I can sure repost if necessary. Thanks for any help.
    Last edited by Mr Moe; 02-10-2005 at 11:43 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    is using tabs after your last octet acceptable? depending on your environment, one tab may be enough to do what you want...

    in other words, have you tried
    Code:
    cout << octet[0] << "." << octet[1] << "." << octet[2] << "." << octet[3]<<'\t';
    if not, what about blank spaces? by that I mean can you output this:
    Code:
    192.168.  0. 12
    if so, try using width()
    Last edited by major_small; 02-10-2005 at 11:49 PM. Reason: a lot of changes
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Actually the tab will work perfectly. Thanks! I knew it had to be something simple that just wasn't coming to mind.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Seems like a perfect opportunity to use a class. I don't know your specific implementation but you could probably adapt it or come up with something similar (could probably be improved upon as well ie..another constructor to accept an array of 4 ints, I whipped it up real quick).

    OctetAddress.h
    Code:
    #ifndef OCTETADDRESS_H
    #define OCTETADDRESS_H
    
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using std::cout;
    using std::ostringstream;
    using std::string;
    
    class OctetAddress {
    	
    	private:
    		string octetString;
    		int octetField[4];
    		void setOctetString();
    	
    	public:
    		OctetAddress( int = 0, int = 0, int = 0, int = 0 );
    		void setOctet( int = 0, int = 0, int = 0, int = 0 );
    		string getOctetString() const;
    
    };
    
    OctetAddress::OctetAddress( int a, int b, int c, int d )
    {
    	setOctet( a, b, c, d );
    }
    
    void OctetAddress::setOctet( int a, int b, int c, int d )
    {
    	octetField[0] = ( a > 255 || a < 0 ? 0 : a );	//
    	octetField[1] = ( b > 255 || b < 0 ? 0 : b );	// Ensure values
    	octetField[2] = ( c > 255 || c < 0 ? 0 : c );	// are in range
    	octetField[3] = ( d > 255 || d < 0 ? 0 : d );	//
    	setOctetString();
    }
    
    void OctetAddress::setOctetString()
    {
    	ostringstream buildString;
    	buildString << octetField[0] << "." << octetField[1] << "." << octetField[2] << "." << octetField[3];
    	octetString = buildString.str();
    }
    
    string OctetAddress::getOctetString() const
    {
    	return octetString;
    }
    
    #endif
    And a simple example how it's easy to provide organized output.

    sampleUse.cpp
    Code:
    #include <iostream>
    #include <iomanip>
    #include "OctetAddress.h"
    
    using std::cout;
    using std::cin;
    using std::setw;
    using std::left;
    using std::endl;
    
    int main()
    {
    	
    	int octet[4];
    	
    	cout << "Enter 4 ints:" << endl;
    	for (int n = 0 ; n < 4 ; n++ )
    		cin >> octet[n];
    	
    	OctetAddress ipAddress1( octet[0], octet[1], octet[2], octet[3] );
    	OctetAddress ipAddress2;
    	
    	
    	cout << setw(16) << left << ipAddress1.getOctetString() << "random text" << endl;
    	cout << setw(16) << left << ipAddress2.getOctetString() << "random text" << endl;
    	
    	ipAddress2.setOctet( 192, 168, 0, 1 );
    	cout << setw(16) << left << ipAddress2.getOctetString() << "random text" << endl;
    	
    	return 0;
    }
    Last edited by Scribbler; 02-11-2005 at 03:04 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If you want more control over the output than a tab, you could also do it like this:
    Code:
    int octet[] = {192, 24, 159, 1};
    
    //Create a variable that allows you use the << operator to append non-string types:
    stringstream combined_string; 
    combined_string << octet[0] << "." << octet[1] << "." << octet[2] << "." << octet[3];
    
    //Use the str() function to get the contents of the variable:
    cout << combined_string.str()<< "xxxx" <<endl;  
    
    //Use setw() and left to format the output for the variable, then return to default settings:
    cout<<setw(16)<<left<<combined_string.str()<<setw(0)<<right<<"xxxx"<<" yyyy"<<endl;
    You need to include <sstream> and <iomanip>.
    Last edited by 7stud; 02-11-2005 at 09:10 AM.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Thanks a bunch guys, I'll give some of that a shot. I really appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  2. Replies: 5
    Last Post: 10-08-2004, 06:30 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM
  5. Newbie Printer Formatting Question
    By Foffo Spearjig in forum C Programming
    Replies: 3
    Last Post: 01-24-2002, 01:38 PM

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