ostream operator

Discussion in 'C++' started by joesoap, Aug 20, 2003.

  1. joesoap

    joesoap Guest

    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 == '\0')
    {
    break;
    }
    if((number != '1') && (number != '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 = '\0';
    bitstring = '0';
    (*nBitstring)++;
    }
    if(mod == 1)
    {
    bitstring = '\0';
    bitstring = '1';
    (*nBitstring)++;
    }
    if(number == 0)
    {
    bitstring = '\0';
    bitstring = '1';
    i++;
    (*nBitstring)++;
    bitstring = '\0';
    break;
    }

    }

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

    for(int i = 0; ;i++)
    {
    temp = bitstring[count];
    if(count == 0)
    {
    i++;
    temp = '\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 = '\0';
    bitstring = '0';
    (*nBitstring)++;
    }
    if(mod == 1)
    {

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

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

    }

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

    for(int i = 0; ;i++)
    {
    temp = 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::eek:perator char()
    {


    }
    BitString::eek:perator int()
    {}

    BitString::eek:perator char*()
    {}

    BitString::eek:perator 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::eek:perator & (BitString & number)
    {
    for(int i = 0; i < *(number.nBitstring);i++)
    {
    if(bitstring[i] != number.bitstring[i])
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    }

    bool BitString::eek:perator & (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::eek:perator & (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::eek:perator & (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::eek:perator & (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::eek:perator | (BitString & number)
    {
    for(int i = 0; i < *(number.nBitstring);i++)
    {
    if(bitstring[i] != number.bitstring[i])
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    }

    bool BitString::eek:perator | (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::eek:perator | (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::eek:perator | (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::eek:perator | (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::eek:perator ^ (BitString &number)
    { throw InvalidNumber();}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    BitString BitString::eek:perator ~()
    {throw InvalidNumber();}

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

    BitString BitString::eek:perator << (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;
    //}*/

    }[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    joesoap, Aug 20, 2003
    #1
    1. Advertising

  2. "joesoap" <> 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
    Victor Bazarov, Aug 20, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Irzak

    ABC ostream & operator <<

    Victor Irzak, Sep 20, 2003, in forum: C++
    Replies:
    3
    Views:
    468
    Jerry Coffin
    Sep 21, 2003
  2. Ryan M. Keith
    Replies:
    0
    Views:
    330
    Ryan M. Keith
    Oct 16, 2003
  3. Ryan M. Keith
    Replies:
    0
    Views:
    278
    Ryan M. Keith
    Oct 16, 2003
  4. keit6736
    Replies:
    2
    Views:
    346
    Sergiy Kanilo
    Oct 17, 2003
  5. Replies:
    2
    Views:
    1,881
Loading...

Share This Page