Solved

Operator overloading function - formatting?

Posted on 2006-11-23
4
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now