473,513 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostream operator

Hi can anybody please tell me what is wrong with my ostream operator???

this is the output i get using the 3 attached files.

this is the output after i run assignment2

-joesoap

#include "BitString.h"

BitString::BitString()
{
bitstring = new char[100];
nBitstring = new int;
*nBitstring = 0;
}
BitString::BitString(char * number)
{
nBitstring = new int(strlen(number));
bitstring = new char[*nBitstring];
for(int i = 0; ; i++)
{
if( number[i] == '\0')
{
break;
}
if((number[i] != '1') && (number[i] != '0'))
{
throw InvalidNumber();
}
//*(nBitstring++);
}
bitstring = number;
}

BitString::BitString(int number)
{

nBitstring = new int();
bitstring = new char[100];
int mod;
*nBitstring = 0;

for (int i = 0; ; i++)
{

mod = number % 2;
number = number / 2;

if(mod == 0)
{
bitstring[i] = '\0';
bitstring[i] = '0';
(*nBitstring)++;
}
if(mod == 1)
{
bitstring[i] = '\0';
bitstring[i] = '1';
(*nBitstring)++;
}
if(number == 0)
{
bitstring[i] = '\0';
bitstring[i] = '1';
i++;
(*nBitstring)++;
bitstring[i] = '\0';
break;
}

}

int count = (*nBitstring) - 2;
char temp[count];

for(int i = 0; ;i++)
{
temp[i] = bitstring[count];
if(count == 0)
{
i++;
temp[i] = '\0';

break;
}
count--;
}

bitstring = temp;
//cout<<bitstring<<endl;
}

BitString::BitString(double number)
{
int first;
int last;
double temp1;
int mod;
nBitstring = new int(0);;
bitstring = new char[100];
first = (int)number;
temp1 = number - first;
last = (int)(temp1 * 100);

for (int i = 0; ; i++)
{

mod = first % 2;
first = first / 2;

if(mod == 0)
{

bitstring[i] = '\0';
bitstring[i] = '0';
(*nBitstring)++;
}
if(mod == 1)
{

bitstring[i] = '\0';
bitstring[i] = '1';
(*nBitstring)++;
}
if(first == 0)
{

bitstring[i] = '\0';
bitstring[i] = '1';
i++;
(*nBitstring)++;
bitstring[i] = '\0';
break;
}

}

int count = (*nBitstring) - 2;
char temp[count];

for(int i = 0; ;i++)
{
temp[i] = bitstring[count];
if(count == 0)
{
i++;
temp[i] = '\0';

break;
}
count--;
}
bitstring = temp;
bitstring[(*nBitstring)-1] = '.';
(*nBitstring)++;

//int dot = nBitstring;
bitstring[(*nBitstring)-1] = '\0';

for (int i = (*nBitstring-1) ; ; i++)
{

mod = last % 2;
last = last / 2;
if(mod == 0)
{

bitstring[i] = '\0';
bitstring[i] = '0';
(*nBitstring)++;
}
if(mod == 1)
{

bitstring[i] = '\0';
bitstring[i] = '1';
(*nBitstring)++;
}
if(last == 0)
{

bitstring[i] = '\0';
bitstring[i] = '1';
i++;
(*nBitstring)++;
bitstring[i] = '\0';
break;
}

}

//cout<<bitstring<<endl;

/*int c = nBitstring -(dot+1);
cout<<c<<endl;
char t[c];

for(int i = c; ;i++)
{
t[i] = bitstring[c];
if(c == 0)
{
i++;
t[i] = '\0';

break;
}
c--;
}
bitstring = t;

cout<<bitstring<<endl;
*/
}

BitString::BitString(char num)
{
char * a;
a = &num;
int mod;
int number = 0;
number = atoi(a);

if(number > 9)
{ cout<<"Only characters from 0 to 9 allowed"<<endl;
}

nBitstring = new int(0);
bitstring = new char[3];

for (int i = 0; ; i++)
{

mod = number % 2;
number = number / 2;

if(mod == 0)
{
bitstring[i] = '\0';
bitstring[i] = '0';
(*nBitstring)++;
}
if(mod == 1)
{
bitstring[i] = '\0';
bitstring[i] = '1';
(*nBitstring)++;
}
if(number == 0)
{
bitstring[i] = '\0';
bitstring[i] = '1';
i++;
(*nBitstring)++;
bitstring[i] = '\0';
break;
}

}

int count = (*nBitstring)++; - 2;
char temp[count];

for(int i = 0; ;i++)
{
temp[i] = bitstring[count];
if(count == 0)
{
i++;
temp[i] = '\0';

break;
}
count--;
}

bitstring = temp;
//cout<<bitstring<<endl;
}
BitString::operator char()
{
}
BitString::operator int()
{}

BitString::operator char*()
{}

BitString::operator double()
{}

char* BitString:: get()
{
return bitstring;
}

int BitString::HammingDistance(BitString &number)
{throw InvalidNumber();}
istream &operator >>(istream & is , BitString &number)
{
return is;
}

ostream & operator << (ostream & os, BitString &number)
{

for(int i = 0; i <*(number.nBitstring) ; i++)
{
if(number.bitstring[i] == '\0')
break;
os<<number.bitstring[i];

}

return os;
}

bool BitString::operator & (BitString & number)
{
for(int i = 0; i < *(number.nBitstring);i++)
{
if(bitstring[i] != number.bitstring[i])
{
return false;
}
else
{
return true;
}
}

}

bool BitString::operator & (int number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator & (double number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator & (char number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator & (char * number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator | (BitString & number)
{
for(int i = 0; i < *(number.nBitstring);i++)
{
if(bitstring[i] != number.bitstring[i])
{
return false;
}
else
{
return true;
}
}

}

bool BitString::operator | (int number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator | (double number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator | (char number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

bool BitString::operator | (char * number)
{
BitString a(number);

for(int i = 0; i <*(a.nBitstring);i++)
{
if(bitstring[i] != a.bitstring[i])
{
return false;
}
else
{
return true;
}
}
}

BitString BitString::operator ^ (BitString &number)
{ throw InvalidNumber();}

char &BitString::operator [] (int number)
{throw InvalidNumber();}

char BitString::operator [] (int number) const
{throw InvalidNumber();}

BitString BitString::operator + (int number)
{throw InvalidNumber();}

BitString BitString::operator + (double number)
{throw InvalidNumber();}

BitString BitString::operator + (char number)
{throw InvalidNumber();}

BitString BitString::operator + (char* number)
{throw InvalidNumber();}

BitString BitString::operator + (BitString &number)
{throw InvalidNumber();}

BitString BitString::operator += (int number)
{throw InvalidNumber();}

BitString BitString::operator += (double number)
{throw InvalidNumber();}

BitString BitString::operator += (char number)
{throw InvalidNumber();}

BitString BitString::operator += (char* number)
{throw InvalidNumber();}

BitString BitString::operator += (BitString &number)
{throw InvalidNumber();}

BitString BitString::operator - (int number)
{throw InvalidNumber();}

BitString BitString::operator - (double number)
{throw InvalidNumber();}

BitString BitString::operator - (char number)
{throw InvalidNumber();}

BitString BitString::operator - (char* number)
{throw InvalidNumber();}

BitString BitString::operator - (BitString &number)
{throw InvalidNumber();}

BitString BitString::operator -= (int number)
{throw InvalidNumber();}

BitString BitString::operator -= (double number)
{throw InvalidNumber();}

BitString BitString::operator -= (char number)
{throw InvalidNumber();}

BitString BitString::operator -= (char* number)
{throw InvalidNumber();}

BitString BitString::operator -= (BitString &number)
{throw InvalidNumber();}

BitString BitString::operator ~()
{throw InvalidNumber();}

BitString BitString::operator >> (BitString &number)
{throw InvalidNumber();}

BitString BitString::operator << (BitString &number)
{throw InvalidNumber();}
#ifndef BITSTRING_H
#define BITSTRING_H

#include <iostream>
#include <istream>
#include <cctype>
#include <cstdio>
#include <stdlib.h>
#include <string.h>

using namespace std;
class BitString
{

private:

char * bitstring;
int *nBitstring;

public:

BitString();
BitString(char *);
BitString(int);
BitString(double);
BitString(char );
class InvalidNumber{};

operator char();
operator int();
operator char*();
operator double();

char* get();
int HammingDistance(BitString &);
friend istream &operator >> (istream & , BitString &number);
friend ostream &operator << (ostream &,BitString &number);
bool operator & (BitString &);
bool operator & (int);
bool operator & (double);
bool operator & (char);
bool operator & (char *);

bool operator | (BitString &);
bool operator | (int);
bool operator | (double);
bool operator | (char);
bool operator | (char *);

BitString operator ^ (BitString &);

char &operator [] (int);
char operator [] (int) const;
BitString operator + (int);
BitString operator + (double);
BitString operator + (char);
BitString operator + (char *);
BitString operator + (BitString &);

BitString operator += (int);
BitString operator += (double);
BitString operator += (char);
BitString operator += (char *);
BitString operator += (BitString &);

BitString operator - (int);
BitString operator - (double);
BitString operator - (char);
BitString operator - (char *);
BitString operator - (BitString &);

BitString operator -= (int);
BitString operator -= (double);
BitString operator -= (char);
BitString operator -= (char *);
BitString operator -= (BitString &);

BitString operator ~();

BitString operator >> (BitString&);
BitString operator << (BitString&);
};
#endif
#include "BitString.h"
#include "BitString.C"
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int main()
{

char *t = "100110";
int n = 123;

//try{
BitString z();
BitString a(t);
BitString b(n);
BitString c(123.45);
BitString d('3');
//BitString e(t);

//char test;

//test = char(d);
//cout<<test<<endl;
cout<<"This is a: "<<a<<endl;
cout<<"This is b: "<<b<<endl;
cout<<"This is c: "<<c<<endl;
cout<<"This is d: "<<d<<endl;

//}
//catch(BitString::InvalidNumber)
//{
//cout <<"Invalid Binary number, use only the numbers 0 and 1"<<endl;
//}*/

}

Jul 19 '05 #1
1 3855
"joesoap" <js@websurfer.co.za> wrote...
Hi can anybody please tell me what is wrong with my ostream operator???
It requires a non-const BitString to be output. You probably want to
define it as

ostream& operator <<(ostream&, BitString const&);
this is the output i get using the 3 attached files.
Please don't post attachments. If you have some code you want to share
with the group, copy and paste it into the message.

And please read FAQ, section 5: http://www.parashift.com/c++-faq-lite/
Question #5.8 should be of the most interest to you.
this is the output after i run assignment2


I don't see any output.

Victor
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8245
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
0
2057
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
2
5954
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
1
2540
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
5
4068
by: Banshee | last post by:
Hi there, I tried with Visual C++ 6.0 to overloading the ostream operator for an object. While compiling I receive this error: Compiling... main.cpp C:\\main.cpp(8) : error C2678: binary...
6
3236
by: silversurfer2025 | last post by:
Hello, I am currently trying to derive a class from ostream (which is giving output to my GUI), such that I can give my methods either std::cout or my own outputstream-class to be used as output...
6
4710
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the...
8
1925
by: dev_15 | last post by:
Hi, i have the following program to display the input received separated into any word that has Uppercase letters and all lowercase words. For the following input "Upper lower" i get the...
3
2415
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.