big integer class

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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(numstr[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 right track 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 drivers and I am getting logic errors. What do I need to do in + overloaded operator function?

Thanks for your help!
 
this.intz = 0;


should not compile.

Do not use "using" clauses in header files.

vector<T> does not have operator<< or operator>> defined for it, yet you are attempting to
use these operators. These should result in compile errors.

operator+ for every other type on the planet returns the same type as the object on the
left-hand side of the +. So operator+ for std::string returns a std::string. Your operator+
returns an int(!)

You are using (capital) "String" in places where you mean (lower-case) "string". These
should not compile.
Can you help me to write the body of the operator overloading function of + please?
I'm a bit confused. You say in your post that operator+ is supposed to concatenate the digits,
but in your code, it looks like you want to add them. Which is it? Intuitively speaking,
operator+ should add them. If you want also to be able to concatenate, then I'd make a
separate method for that.
Hi,

i think your whole class makes no sense! You would work with strings only, so you dont need to overload any of these operators above.

Here a few suggestions:

- To check if the entered string only include numbers make a function like this:

1
2
3
4
5
6
7
8
9
10
bool test_if_number(string test){
int i=0;
while(test[i] != NULL){
      int j=(int)(test[i]); //get ascii-number of the digit
      if (j > 57 || j < 48) //code for 0-9 in the ascii table
          return false;     //non-number detected
	  i++;
	}
	return true; //only numbers detected 
}


- To concatenates 2 strings you can simply add them:

string testC = testA+testB;

- #include<vector> instead using <vector>

There are much more problems with your class, but i finish here.

Greetings
Last edited on
Good heavens:

 
bool hasNonDigits = test.find_first_not_of( "0123456789" ) != test.npos;


But let's let OP answer my question above first.
ok I figure it out but I have a question! In overloading my outnput operator <<, I dod something like
1
2
3
4
5
ostream &operator<<(ostream &output, const LargeDigit &num)
{
  output << num.intz; // intz is a vector
  return output;
}


why is it giving me this error :
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::vector<_Ty>' (or there is no acceptable conversion)
1> with with
1> [
1> _Ty=char
1> ]

1> 1 error(s), 0 warning(s)

Do I need to get rid of const in ostream?
std::vector doesn't have an overloaded << operator. You have to iterate through the vector and write it's contents out one by one.
1
2
3
4
5
6
7
ostream &operator<<(ostream &output, const LargeDigit &num)
{
   for ( unsigned i = 0; i < intz.size(); i++)
   { 
       output << num.intz;  // intz is a vector
    }
    return output;


That is what I can think now. but it is not working. Could you please help me out.

Thanks,

Yes, I would like + operator to add big integers. Sorry for the confusion.
1
2
3
4
5
6
7
ostream &operator<<(ostream &output, const LargeDigit &num)
{
   for ( unsigned i = 0; i < num.intz.size(); i++)
   { 
       output << num.intz[i];  // intz is a vector
    }
    return output;


Representing "big integers" as strings is a very poor choice if you want to actually implement arithmetic operations for them. I would suggest you to use std::vector<unsigned long> as a container for these integers instead, but to be honest I doubt you have the knowledge and skills to make such class yet (no offence). Maybe try something easier first?
Topic archived. No new replies allowed.