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>
usingnamespace std;
class LargeDigit
{
friend ostream &operator<<(ostream &input, const LargeDigit &);
friend istream &operator>>(istream &output, LargeDigit &);
public:
LargeDigit();
LargeDigit(String );
intoperator+(const LargeDigit &);
private:
vector<char> intz;
};
# include "largeDigit.h"
#include <iostream>
using std::cout;
usingnamespace 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;
}
intoperator+::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?
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.
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.
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
returnfalse; //non-number detected
i++;
}
returntrue; //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.
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> ]
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?