Question

Overloading HugeInteger

Asked by: rebelcowgirl_73

I am back LOL...  I am trying to overload a previous version of HugeInteger.  All methods worked and such before attempt to overload.  I am gonna go threw this bit by bit.  Since I was suppose to overload every single function in my already completed work, i figured I would start with one I thought I kind of understood.  HA HA what a joke that was.  Here is my attempt at overloading output <<  However I get four different errors.  I have included the .h file and all methods up to the output << method hoping someone can tell me why I am getting these errors.
.h file

class OLHugeInteger
{
      friend ostream &operator<<(ostream &, const OLHugeInteger &);

public:
      OLHugeInteger();      //constructor
      void input();      
      void copyBigNumber(int [], const int []) const ;

      OLHugeInteger add(const OLHugeInteger &) const;
      OLHugeInteger subtract(const OLHugeInteger &) const;
      OLHugeInteger multiply(const OLHugeInteger &) const;
      
      OLHugeInteger multiplySub(int) const;
      OLHugeInteger opposite(const OLHugeInteger &) const;
      

      bool equalTo(const OLHugeInteger &) const;
      bool notEqualTo(const OLHugeInteger &) const;
      bool greaterThan(const OLHugeInteger &) const;
      bool greaterThanOrEqualTo(const OLHugeInteger &) const;
      bool lessThan(const OLHugeInteger &) const;
      bool lessThanOrEqualTo(const OLHugeInteger &) const;
      bool isZero() const;

private:
      int const arraySize;      //const array size
      bool isNegative;      //determines if array of integers is negative
      char line[41];
      int bigNumber[40];      //array to hone users inputted integer
      void copyBigNumber(int [], const int []);      //creates copy of bigNumber[]


};      //End class OLHugeInteger

#endif

.cpp up to output<<
#include <iostream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;

#include <string>      //for string length (strlen)

#include "OLHugeInteger.h"      //include definition of class HugeInteger

int inputSize = 0;      //default size of users integer

//default constructor const arraySize = 40
OLHugeInteger::OLHugeInteger() :arraySize(40)
{
      for (int loopSize = 0; loopSize < arraySize; loopSize++)
            //initialize array with 0's
            bigNumber[loopSize] = 0;      
      
      //set bool isNegative to false
      isNegative = false;

}//end constructor

void OLHugeInteger::input()
{
      //characters read in to hold integer values
    char line[41];      
     
      //ask for usre input and make sure no more than 40 digits where entered
      //if more than 40 digits are entered, drop extra from bigNumber
      cout << "Enter Huge Integer (Max: 40 digits, extra digits will be dropped!): " << endl;
      cin.getline(line, sizeof line);
      cin.sync();
      cin.clear();

    //determine size of users integer
      inputSize = strlen(line);      

      //check for negative integer value and set bool
      if (inputSize > 0)      
      {
            int negative = '-';
            if (line[0] == negative)
                  isNegative = true;
      }

      //set negative to zero
      if (isNegative)
            line[0] = '0';

      //check that all digits entered where integer values
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            if (!isdigit(line[(inputSize - 1) - loopSize]))
            {
                  cout << "Not all digits entered where integers.  Array initialized to zero" << endl;
                  return;
            }

      //add digits to array bigNumber
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            //subtract ASCII value for '0' to get raw value store digits at
            //end of 40 digit array all non initialized parts of array stay a 0
            bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
            
}//end input function

ostream &operator<<(ostream &output, const OLHugeInteger &bigNumber2)
{
      // Display negative sign if isNegative is set.
      if (isNegative)
            output << '-';
      
      //if array isZero then print a single zero
      if(isZero())
            output << '0';
      
      bool leadingZero = true;
      
      //avoid printing all leading zero'
      for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
      {
            int digit = bigNumber[digitIndex];
            //if it is not a zero, print it, and set leadingZero to false;
            if (digit != 0)
            {
                  leadingZero = false;
                  output << digit;
            }//end if
            else
                  //else if digit is zero, print only if not leading
                  if (!leadingZero)
                        output << digit;
                  
      }//end for
      return output;
     
}//end output function

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-04-29 at 17:59:24ID23364121
Tags

C++

Topic

C++ Programming Language

Participating Experts
3
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. indent std::cout ?
    is there a way to produce indented text with std::cout? I want to be able to do something like this: std::cout << "text (normal indentation).\n"; std::cout << indent(4) << "text indented by 4 spaces\nwhich also wraps lines.\n"; std::cout...
  2. iostream and iostream.h
    I can run the following program. But, I want to use iostream instread of iostream.h When I replace iostream.h with iostream and using namespace std, I get two compiler errors (1.i':cannot access private member declared in class 'Num', 2. 'operator <<' is ambiguous) Que...
  3. cout/cin overload
    Is it possible to overload cout & cin ? if so, how? if not, why ?
  4. To IOStream gurus
    Hi, I am trying to implement logging system using iostreams. My question is: how can I add date/time stamp on iostream basis? What I want to acheive is as following: cmylog << "Test" << std::endl; And the output shall be 10:00 Test (I will be able t...
  5. using std::iostream ?
    This might be an easy question for most of you I guess. I want to use "using std::iostream" or similar first in my cpp-files. This is because I have a lot of "cout" and "endl" in my code and it would be dirty code if I have to replace it with &q...
  6. How does iostream cout really work?
    I'm puzzled by the following code. using printf() works fine, but for some reason, setting X_SIZE and Y_SIZE to 60 (and some other values) causes the program to output nothing using iostream's cout. With other values, cout works as expected. I tried cout.flush(), but no diffe...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rebelcowgirl_73Posted on 2008-04-29 at 18:00:27ID: 21467425

The errors would probably help!

Here they are!
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(89) : error C2065: 'isNegative' : undeclared identifier
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(93) : error C3861: 'isZero': identifier not found
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(99) : error C2065: 'arraySize' : undeclared identifier
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(101) : error C2065: 'bigNumber' : undeclared identifier

 

by: rebelcowgirl_73Posted on 2008-04-29 at 18:12:11ID: 21467464

figured out the bigNumber error, but now instead of that error my fourth error is this:
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(101) : error C2676: binary '[' : 'const OLHugeInteger' does not define this operator or a conversion to a type acceptable to the predefined operator

 

by: evilrixPosted on 2008-04-29 at 18:20:57ID: 21467490

You are trying to access a members of OLHugeInteger in your operator<<() but this function is not a member of OLHugeInteger it is a free standing function so it has no implicit 'this' pointer.

 

by: evilrixPosted on 2008-04-29 at 18:24:44ID: 21467506

>> figured out the bigNumber error, but now instead of that error my fourth error is this:
I presume you've changed the code then? Can you repost just the bit you've changed (I guess it's operator<<() ?)

 

by: rebelcowgirl_73Posted on 2008-04-29 at 18:26:03ID: 21467512

jjust removed the 2 from bigNumber
ostream &operator<<(ostream &output, const OLHugeInteger &bigNumber)

 

by: lwinkenbPosted on 2008-04-29 at 18:28:44ID: 21467522

You need to access the member variables through the class like so:

ostream &operator<<(ostream &output, const OLHugeInteger &bigNumber2)
{
      // Display negative sign if isNegative is set.
      if (bigNumber2.isNegative)
            output << '-';
      
      //if array isZero then print a single zero
      if(bigNumber2.isZero())
            output << '0';
      
      bool leadingZero = true;
      
      //avoid printing all leading zero'
      for (int digitIndex = 0; digitIndex < bigNumber2.arraySize; digitIndex++)
      {
            int digit = bigNumber2.bigNumber[digitIndex];
            //if it is not a zero, print it, and set leadingZero to false;
            if (digit != 0)
            {
                  leadingZero = false;
                  output << digit;
            }//end if
            else
                  //else if digit is zero, print only if not leading
                  if (!leadingZero)
                        output << digit;
                  
      }//end for
      return output;
     
}//end output function

                                              
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:

Select allOpen in new window

 

by: rebelcowgirl_73Posted on 2008-04-29 at 18:40:07ID: 21467565

ok now I did input, and I get two errors.  Here is my input>>

istream &operator>>(istream &input, OLHugeInteger &bigNumber)
{
      //characters read in to hold integer values
    char line[41];      
     
      //ask for usre input and make sure no more than 40 digits where entered
      //if more than 40 digits are entered, drop extra from bigNumber
      cout << "Enter Huge Integer (Max: 40 digits, extra digits will be dropped!): " << endl;
      cin.getline(line, sizeof line);
      cin.sync();
      cin.clear();

    //determine size of users integer
      inputSize = strlen(line);      

      //check for negative integer value and set bool
      if (inputSize > 0)      
      {
            int negative = '-';
            if (line[0] == negative)
                  bigNumber.isNegative = true;
      }

      //set negative to zero
      if (bigNumber.isNegative)
            line[0] = '0';

      //check that all digits entered where integer values
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            if (!isdigit(line[(inputSize - 1) - loopSize]))
            {
                  cout << "Not all digits entered where integers.  Array initialized to zero" << endl;
                  return;
            }

      //add digits to array bigNumber
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            //subtract ASCII value for '0' to get raw value store digits at
            //end of 40 digit array all non initialized parts of array stay a 0
            input >> bigNumber[(bigNumber.arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
      
      return input;
}//end input function

Here are my errors:
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(75) : error C2561: 'operator >>' : function must return a value
1>        d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.h(27) : see declaration of 'operator >>'
1>d:\homework\visual studio 2005\projects\olhugeinteger\olhugeinteger\olhugeinteger.cpp(82) : error C2676: binary '[' : 'OLHugeInteger' does not define this operator or a conversion to a type acceptable to the predefined operator

 

by: lwinkenbPosted on 2008-04-29 at 18:51:41ID: 21467588

The first error is the line which just has:
return;

You need to return an istream.  For instance:
return input;

The next error is on the line:
input >> bigNumber[(bigNumber.arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';

First of all, you need something more like:
input >> bigNumber.bigNumber[(bigNumber.arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
(notice the bigNumber.bigNumber).

This still won't compile though.  I think it would help you to break this line into smaller chunks to help you understand what the code is doing.  For instance is this what you meant to do?
int i;
input >> i;
bigNumber.bigNumber[(bigNumber.arraySize - 1) - loopSize] = i;

 

by: rebelcowgirl_73Posted on 2008-04-29 at 19:00:10ID: 21467610

I see what you mean aobut the int, i did int digit = bigNumber.bigNumber blah blah blah, but as for the first error, the return originally was in place for the error, and if I do return input, the program just keeps doing blank lines after I input an integer, for example 12, then hit enter it just goes to the next line with no print out.  I don't know if it helps but here is the part of main that pertains to it.

//input huge integer one
      OLHugeInteger one;
      cin >> one;

      //input huge integer two
      OLHugeInteger two;
      cin >> two;

as you can see it should repeat meaning ask for an integer twice, but without return input I get the above error and with return input it goes to the next line and it is blank rather then asking for another integer.  Hope this makes sense.

 

by: lwinkenbPosted on 2008-04-29 at 19:09:27ID: 21467639

I'm not sure what the goal of that function is.  Do you want each integer placed in the bigNumber array?

So if the user enters "1234", do you want the bigNumber array to look like:
bigNumber[0] = 1;
bigNumber[0] = 3;
bigNumber[0] = 3;
bigNumber[0] = 4;
?

Also, you definitely want to change:
cin.getline(line, sizeof line);
cin.sync();
cin.clear();
to
input.getline(line, sizeof line);
input.sync();
input.clear();

 

by: lwinkenbPosted on 2008-04-29 at 19:10:21ID: 21467641

Oops, I mean to write:
bigNumber[0] = 1;
bigNumber[2] = 2;
bigNumber[3] = 3;
bigNumber[4] = 4;

 

by: rebelcowgirl_73Posted on 2008-04-29 at 19:16:10ID: 21467668

ok here is input from top to bottom.  as I said this program was completed already however, we now have to take our completed HugeInteger and overload everything.  Final assignment.

ask user for input
get line
determine size
if size is greater then 0 check for negative
if negative set bool isNegative to true
if bigNumber is negative
set line[0] to '0'
check all digits are integers now
if they are not
set all digits to 0 and tell user
add digits to array (so 1234 would be)
bigNumber[36]=1
bigNumber[37]=2
bigNumber[38]=3
bigNumber[39]=4

I need the program to tell user they did not enter all integers for example they want to type 1234 but type 12e4 or 1w34 then it is set to all zeros

 

by: lwinkenbPosted on 2008-04-29 at 19:32:46ID: 21467742

Well here is how you could zero out the array:

	  //check that all digits entered where integer values
	  for (int loopSize = 0; loopSize < inputSize; loopSize++)
		  if (!isdigit(line[(inputSize - 1) - loopSize]))
		  {
			  cout << "Not all digits entered where integers.  Array initialized to zero" << endl;
			  for(int i = 0; i < bigNumber.arraySize; ++i)
				  bigNumber.bigNumber[i] = 0;
			  return input;
		  }

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

 

by: rebelcowgirl_73Posted on 2008-04-29 at 19:39:41ID: 21467762

I am still getting a blank line after i try to input 1234
I am pretty sure it has nothing to do with the whole array set to zero thing becasue when I cut it out and build and then start without debugging, I get the blank llline after i input 1234 still

 

by: rebelcowgirl_73Posted on 2008-04-29 at 19:43:39ID: 21467773

so there is no confusion this is my input>>

istream &operator>>(istream &input, OLHugeInteger &bigNumber)
{
      //characters read in to hold integer values
    char line[41];      
     
      //ask for usre input and make sure no more than 40 digits where entered
      //if more than 40 digits are entered, drop extra from bigNumber
      cout << "Enter Huge Integer (Max: 40 digits, extra digits will be dropped!): " << endl;
      input.getline(line, sizeof line);
      input.sync();
      input.clear();

    //determine size of users integer
      inputSize = strlen(line);      

      //check for negative integer value and set bool
      if (inputSize > 0)      
      {
            int negative = '-';
            if (line[0] == negative)
                  bigNumber.isNegative = true;
      }

      //set negative to zero
      if (bigNumber.isNegative)
            line[0] = '0';

      //check that all digits entered where integer values
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            if (!isdigit(line[(inputSize - 1) - loopSize]))
            {
                  cout << "Not all digits entered where integers.  Array initialized to zero" << endl;
                  for (int digitIndex = 0; digitIndex < bigNumber.arraySize; digitIndex++)
                        bigNumber.bigNumber[digitIndex] = 0;
                  return input;
            }

      //add digits to array bigNumber
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
      {
            int digit = bigNumber.bigNumber[(bigNumber.arraySize - 1) - loopSize];
            //subtract ASCII value for '0' to get raw value store digits at
            //end of 40 digit array all non initialized parts of array stay a 0
            bigNumber.bigNumber[(bigNumber.arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
            input >> digit;
      }
      return input;
}//end input function

 

by: lwinkenbPosted on 2008-04-29 at 19:57:28ID: 21467813

I think I understand you now.  Try this:

      //add digits to array bigNumber
      for (int loopSize = 0; loopSize < inputSize; loopSize++)
            //subtract ASCII value for '0' to get raw value store digits at
            //end of 40 digit array all non initialized parts of array stay a 0
	  {
            bigNumber.bigNumber[loopSize] = line[loopSize] - '0';
	  }
      return input;
}//end input function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

 

by: rebelcowgirl_73Posted on 2008-04-29 at 19:59:17ID: 21467817

but that will put then at the beginning of the array and not the end

 

by: lwinkenbPosted on 2008-04-29 at 19:59:20ID: 21467818

I tested with this code, and it works:

int main(void)
{
	OLHugeInteger o;
	cin >> o;
	cout << o;
}

                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: lwinkenbPosted on 2008-04-29 at 20:00:21ID: 21467821

Oops, I thought you wanted it at the beginning.  Well, putting the characters at the end of the array instead of the beginning should be pretty trivial for you.

 

by: rebelcowgirl_73Posted on 2008-04-29 at 20:02:57ID: 21467825

ya well whenever I think that, I get all screwed up, now I gotta do add brb I am sure!

 

by: lwinkenbPosted on 2008-04-29 at 20:07:57ID: 21467839

Hehe, you already have written the code to do this.

(copied from your earlier post)
bigNumber.bigNumber[(bigNumber.arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';

 

by: rebelcowgirl_73Posted on 2008-04-29 at 20:27:29ID: 21467887

wow, lots of errors here, first, does this look right from .h in public part:
OLHugeInteger operator+(const OLHugeInteger &) const;
second, here is add, I haven't changed anything yet:
OLHugeInteger operator+(const OLHugeInteger &bigNumber2) const
{
      OLHugeInteger result;
                        
      //if both bigNumbers are positive or both are negative, add together
      if (this.isNegative == bigNumber2.isNegative)
      {
            int carry = 0;      //holds carry over
            //add all digits in array
            for (int loopSize = arraySize - 1; loopSize >= 0; loopSize--)
            {
                  int temp = bigNumber[loopSize];
                  result.bigNumber[loopSize] = (temp + bigNumber2.bigNumber[loopSize] + carry) %10;      //remainder
                  carry = (temp + bigNumber2.bigNumber[loopSize] + carry) / 10;
            }//end for
            result.isNegative = isNegative;
            return result;
      }//end if
      //else subtract because neg + pos and pos + neg needs to be subtracted
      else
            return subtract(opposite(bigNumber2));
      
}//end add function

 

by: rebelcowgirl_73Posted on 2008-04-29 at 20:36:04ID: 21467922

I don't really have an example to go off of, just a reference to a list of operators that can be overloaded.  But no examples in the text, so I am not sure where to go from here.

 

by: lwinkenbPosted on 2008-04-29 at 20:43:34ID: 21467943

Close, the prototype should be:
OLHugeInteger OLHugeInteger::operator+(const OLHugeInteger &bigNumber2) const

And instead of this.isNegative you need this->isNegative (this is a pointer).

 

by: rebelcowgirl_73Posted on 2008-04-29 at 20:49:06ID: 21467959

ok how would i change the add part of the following from my subtract function?

//else one is positive and one is negative
      else
            return add(opposite(bigNumber2));

 

by: rebelcowgirl_73Posted on 2008-04-29 at 21:17:46ID: 21468037

Nevermind, got it, moving on!

Is it better to do the equalities in .h or do them in .cpp?  I could do this:
bool operator==(const HugeInteger &) const;      //test bn1 == bn2
      bool operator>(const HugeInteger &) const;      //test bn1 > bn2

      //test bn1 != bn2
      bool operator!=(const HugeInteger &bigNumber2) const
      {
            return !(*this == bigNumber2);
      }

      //test bn1 >= bn2
      bool operator>=(const HugeInteger &bigNumber2) const
      {
            return !(bigNumber2 > *this);
      }

      //test bn1 < bn2
      bool operator<(const HugeInteger &bigNumber2) const
      {
            return bigNumber2 > *this;
      }
      
      //test bn1 <= bn2
      bool operator<=(const HugeInteger &bigNumber2) const
      {
            return !(*this > bigNumber2);
      }
or should I do them insde the .cpp file that I have already?
bool OLHugeInteger::equalTo(const OLHugeInteger &bigNumber2) const
{
      //if one is positive and one is negative
      if (isNegative != bigNumber2.isNegative)
                  return false;
      
      for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
            //if any integers from first array does not equal integers
            //from second array, return false
            if(bigNumber[digitIndex] != bigNumber2.bigNumber[digitIndex])
                  return false;

      return true;

}//end equalTo predicate function

bool OLHugeInteger::notEqualTo(const OLHugeInteger &bigNumber2) const
{
      //if all first array integers are not equal to second array integers
      //return true
            if(!equalTo(bigNumber2))
                  return true;
      return false;

}//end notEqualTo predicate function

bool OLHugeInteger::greaterThan(const OLHugeInteger &bigNumber2) const
{
      //if equal return false
      if (equalTo(bigNumber2))
            return false;
      else
      {
            //If the two numbers are of the same sign
            if (isNegative == bigNumber2.isNegative)
            {
                  //If both are positve
                  if (!isNegative)
                  {
                        //the first 1st array int > 2nd array int return true
                        //logic( 200 is greater than 100 {bigger = greater})
                        for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
                        {
                              if (bigNumber[digitIndex] == bigNumber2.bigNumber[digitIndex])
                                    continue;
                              if (bigNumber[digitIndex] <= bigNumber2.bigNumber[digitIndex])
                                    return false;
                              else
                                    return true;
                        }
                  }
                  //else both are negative
                  else
                  {
                        // When both are negative, use the both-are-positive logic to
                        //get the job done flipflop with opposite and check again
                        if (isNegative && bigNumber2.isNegative)
                        {
                              OLHugeInteger positveThis = opposite(*this);         // Positive copy of this number
                              OLHugeInteger positveThat = opposite(bigNumber2);    // Positive copy of bigNumber2
                              if (positveThis.greaterThan(positveThat))
                                    return false;
                              else
                                    return true;
                        }
                  }
            }
            //else the two numbers are of differing signs
            else
            {
                  //if (-)*this and (+)bigNumber2
                  if (isNegative && !(bigNumber2.isNegative))
                        return false;
                  //else (+)*this and (-)bigNumber2
                  else            
                        return true;
            }
      return true;
      }
}//end greaterThan predicate function


bool OLHugeInteger::greaterThanOrEqualTo(const OLHugeInteger &bigNumber2) const
{
      //if equal to or greater than return true
      if (equalTo(bigNumber2) || greaterThan(bigNumber2))
            return true;
      return false;

}//end greaterThanOrEqualTo predicate function

bool OLHugeInteger::lessThan(const OLHugeInteger &bigNumber2) const
{
      //if not equal to and not greater than return true
      if (!equalTo(bigNumber2) && !greaterThan(bigNumber2))
            return true;
      else
            return false;
}// end lessThan function

bool OLHugeInteger::lessThanOrEqualTo(const OLHugeInteger &bigNumber2) const
{
      //if equal to or less than return true
      if (equalTo(bigNumber2) || lessThan(bigNumber2))
            return true;
      return false;

}//end lessThanOrEqualTo predicate function

 

by: rebelcowgirl_73Posted on 2008-04-29 at 21:24:53ID: 21468060

of course I would have to change all the ones in the .cpp file and it would be simpler to just do them in .h I guess, so (another bad example in the book) does that mean if I do them in .h that I only do == and > in .cpp?

 

by: lwinkenbPosted on 2008-04-29 at 21:38:45ID: 21468114

>> Is it better to do the equalities in .h or do them in .cpp?
That's up to you.  I usually will only put implementation in the header file if it fits on one line.  So maybe like:
bool operator!=(const HugeInteger &bigNumber2) const { return !(*this == bigNumber2); }

 

by: josgoodPosted on 2008-05-02 at 13:03:09ID: 21489652

How are you doing on this?

 

by: rebelcowgirl_73Posted on 2008-05-03 at 05:40:09ID: 21492122

well this one was way easier.  I got it done, just small little syntax errors.  I love to see your still around.  I take my final tomorrow and then yeah it is summer vacation!!!!!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...