Solved

Operator overloading function - formatting?

Posted on 2006-11-23
4
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 429
C Language combined operators 28 123
Need some help with Microsoft Visual Studio C++ 2003 5 74
Dialogbox API leak? 18 109
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

733 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