Hello,

I am writing a class that can handle large number of digits.

I am trying to overload >> operator so that it can store the user input value and if user entered nondigit then it should print an error message and display the default value of 1 ; overload << to output the large digits and overload + operator which adds up two large digits ( it doesn't sum the digits rather it concatenates two strings together for example if user enters 123456 for the first time and 67899 second time then + operator will create 122456 67899). But if user enter nondigits like 12a, 5r, and so on it should print an error message and set the user input value to 1.

#include <iostream>
#include <string>
using std::ostream;
using std::istream;
using std::string;
using <vector>
using namespace std;

class LargeDigit
{
  friend ostream &operator<<(ostream &input, const LargeDigit &);
  friend istream &operator>>(istream &output, LargeDigit &);
public:
  LargeDigit();
  LargeDigit(String );
  int operator+(const LargeDigit &);
private:
  vector<char> intz;
};

# include "largeDigit.h"
#include <iostream>
using std::cout;
using namespace std;

LargeDigit::LargeDigit()
{
  this.intz = 0;
}

LargeDigit::LargeDigit(String str)
{
  for (int i = 0; i < str.length(); i++)
  {
    if(!isdigit(str[i])
    {
      cout<<"non-digit number is entered, so 1 is used as default  
      value"<<endl;
      this.intz = 1;
    }
    else
    {
      intz.push_back(str[i]);
    }
  }
} 

istream &operator<<(istream &input, const LargeDigit &num)
{
  input >> num.intz;
  (how do I check if user entered valid number here?)
  return input;
}

ostream &operator<<(ostream &output, const LargeDigit &digit)
{
  output << digit.intz;
  return output;
}

int operator+::LargeDigit(const VeryLongInt &digit)
{
   return  digit.intz+=digit.intz;
}

I know I am in right direction but can you guys tell me if I am doing everything correctly. Are my overloaded operators okay? it compiles fine and I tested my function with my quick and dirty driver and I am getting logic errors. What do I need to do in + overloaded operator function?

Thanks for your help!

Recommended Answers

All 3 Replies

int operator+::LargeDigit(const VeryLongInt &digit)
{
   return  digit.intz+=digit.intz;
}

it should be

int LargeDigit::operator +(const LargeDigit &digit)//SEE THE SYNTAX properly
{
     /*your code*/
}

where did u get that VeryLongInt ........?????
U are overloading the operator + for the class LargeDigit right ...!!!

I fix that but I need help in writing the body of that +operator overloading function.
Thanks,

I fix that but I need help in writing the body of that +operator overloading function.
Thanks,

int LargeDigit::operator +(const LargeDigit &digit)
{
     return (this->intz + digit.intz);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.