Solved

Operator overloading function - formatting?

Posted on 2006-11-23
4
175 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
ID: 18005195
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
ID: 18005229
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
ID: 18005238
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
ID: 18011172

Thanks for the reply !!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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