Solved

Operator overloading function - formatting?

Posted on 2006-11-23
4
173 Views
Last Modified: 2010-04-01

Hello,

I have the following code:

            output << ", " << array[i];

This works fine but I'm left with an extra comma as it goes through a loop.

[, (0, 0), (0, 0)]

Any suggestions as to how I can remove the first comma + space white before the first open bracket ?

I've tried using output.ignore however this throws up an error ?

Thanks.
0
Comment
Question by:andyw27
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 500 total points
Comment Utility
try with this:

output << "[";
for (int i=0; i<array_size; i++) {
        output << array[i];
        if (i != array_size-1) output << ", ";
}
output << "]";
0
 
LVL 7

Expert Comment

by:UrosVidojevic
Comment Utility
Sample code:
============

_______________________________________________________________________________
#include <iostream>
using namespace std;

class Point {
      int x, y;
public:
      Point(int xx = 0, int yy = 0) { x = xx; y = yy; };
      friend istream& operator >> (istream& input, Point& p) {
            return input >> p.x >> p.y;
      }
      friend ostream& operator << (ostream& output, const Point& p) {
            return output << "(" << p.x << "," << p.y << ")";
      }
};

int main () {
      cout << "Number of points? ";
      int array_size;      
      cin >> array_size;
      Point array[array_size];
      for (int i = 0; i < array_size; i++) {
            cout << "Point #" << i+1 << " = ";
            cin >> array[i];
      }
      
      cout << endl;

      cout << "Array: ";
      cout << "[";
      for (int i=0; i<array_size; i++) {
                   cout << array[i];
                   if (i != array_size-1) cout << ", ";
      }
      cout << "]" << endl;
}
_______________________________________________________________________________



Sample input \ output:
======================
_______________________________________
Number of points? 4
Point #1 = 1 2
Point #2 = 3 4
Point #3 = 5 6
Point #4 = 7 8

Array: [(1,2), (3,4), (5,6), (7,8)]
_______________________________________

Regards, Uros.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
Comment Utility
So, idea is to print comma+space after each element except the last. In that way you will get clear text formating.
0
 

Author Comment

by:andyw27
Comment Utility

Thanks for the reply !!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now