Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Problem with comparing strings

I am working on a class assignment where I have 40 character arrays of numbers.  I need to add/subtract/multiply these numbers as well as do > < == => =< operations (don't need to overload yet, that comes later, just need to do functions that will work) I tried using strcmp (hugeinter1,hugeinteger2) but it doesn't compile right.  Am I missing something.  My code is below.

#include"HugeInteger.h"
#include<iostream>
#include<string>
//const int SIZE = 40;

using namespace std;

HugeInteger::HugeInteger()
{
     setHugeInteger();
     errorFlag = false;
}

//read in value from the user to set huge integer
void HugeInteger::enterHugeInteger()
{
     
     char userInput [SIZE+1];
     bool notNumber  = false;
     int i = 0;
       
     cin.getline( userInput, sizeof userInput );

     
     cin.sync();
     cin.clear();

     //see if the string entered is an unsigned integer
     while(i < strlen(userInput)-1)
     {
          if (!isdigit(userInput[i]))
               notNumber = true;
          i++;
     }

     if (!notNumber)
     {
          i = strlen(userInput);

          while(i >= 0)
          {
             numberArray[SIZE-strlen(userInput)+i] = userInput[i];
             i--;
          }
     }
}

void HugeInteger::setHugeInteger()
{
     for (int i = 0; i < SIZE; i++)
          numberArray[i] = '0';
     numberArray[SIZE] = '\0';
}

int HugeInteger::theLength()
{
      int i;
     for ( i = 0; i < sizeof numberArray-2; i++) //the last 0 is significant
     {                                             //so preserve it
          if( numberArray[i] == '0');
          else
               break;
     }
     return i;
}


void HugeInteger::print()
{
     for (int x = theLength();  x < sizeof numberArray; x++)
          cout << numberArray[x];
}

void HugeInteger::addition (HugeInteger a,HugeInteger b)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int temporarySum = 0;
     int value = 0;
     int carry = 0;

     currentPosition = SIZE-1;

     while (currentPosition >= 0)
     {
          currentValueB = b.numberArray[currentPosition]-'0';
          currentValueA = a.numberArray[currentPosition]-'0';
          temporarySum = currentValueA + currentValueB + carry;
          carry = temporarySum/10;
          value = temporarySum%10;
          numberArray[currentPosition] = value + '0';
          currentPosition--;
     }

     if (carry > 0)
     {
          setHugeInteger();
         errorFlag = true;
     }
}

void HugeInteger::subtraction (HugeInteger a,HugeInteger b)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int value = 0;
     int borrow = 0;

     currentPosition = SIZE-1;

     while (currentPosition >= 0)
     {
          currentValueB = b.numberArray[currentPosition]-'0';
          currentValueA = a.numberArray[currentPosition]-'0';

          value = currentValueA-currentValueB+borrow;
          if (value < 0)
          {
               value += 10;
               borrow = -1;
          }
          else
               borrow = 0;

          numberArray[currentPosition] = value + '0';
          currentPosition--;
     }

     if (borrow < 0) // an underflow has occurred
     {
          setHugeInteger();
         errorFlag = true;
     }
}

void HugeInteger::multiplication (HugeInteger a, HugeInteger b)
{
     int currentPositiona = 0;
     int currentPositionb = 0;
     int theDigit = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int n = 0;
     int value = 0;
     int carry = 0;
     HugeInteger c,d;

     currentPositionb = SIZE-1;
     while (currentPositionb >= 0)
     {
          theDigit = b.numberArray[currentPositionb]-'0';
         c = c.multiplicationHelper(a,theDigit,currentPositionb);
        d.addition(d,c);
          currentPositionb--;
     }

     for (int i = 0; i < sizeof numberArray - 1; i++)
          numberArray[i] = d.numberArray[i] ;
}


HugeInteger HugeInteger::multiplicationHelper (HugeInteger a, int theDigit, int thePosition)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int temporaryMultipliedValue = 0;
     int value = 0;
     int carry = 0;
     HugeInteger c;

     currentPosition = SIZE-1;
     for (int i = thePosition; i < SIZE-1; i++) theDigit *= 10;
     while (currentPosition >= 0)
     {
          currentValueB = theDigit;
          currentValueA = a.numberArray[currentPosition]-'0';
          temporaryMultipliedValue = currentValueA * currentValueB + carry;
          carry = temporaryMultipliedValue/10;
          value = temporaryMultipliedValue%10;
          c.numberArray[currentPosition] = value + '0';
          currentPosition--;
     }
     if (carry > 0) // an overflow has occurred
     {
          c.setHugeInteger();
          c.errorFlag = true;
     }    
     return c;
}
bool isEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      if (strcmp (hugeInteger1, hugeInteger2) == 0)
      {
            return true;
      }
      else
      {
            return false;
      }
}

bool isNotEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      if (strcmp (hugeInteger1, hugeInteger2) == 0)
      {
            return false;
      }
      else
      {
            return true;
      }
}
bool isGreaterThan (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
bool isLessThan (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isGreaterThanOrEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isLessThanOrEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isZero (HugeInteger hugeInteger)
{
      //still need to work
      return true;
}
-----------------------------------------

#pragma once

#ifndef HugeInteger_H
#define HugeInteger_H

class HugeInteger
{
public:
     HugeInteger();
     int theLength();
       void enterHugeInteger();
     void print();
     void addition (HugeInteger,HugeInteger);
     void subtraction (HugeInteger,HugeInteger);
     void multiplication (HugeInteger,HugeInteger);
     HugeInteger multiplicationHelper (HugeInteger a, int theDigit, int thePosition);
     bool isEqualTo (HugeInteger,HugeInteger);
     bool isNotEqualTo (HugeInteger,HugeInteger);
     bool isGreaterThan (HugeInteger,HugeInteger);
     bool isLessThan (HugeInteger,HugeInteger);
     bool isGreaterThanOrEqualTo (HugeInteger,HugeInteger);
     bool isLessThanOrEqualTo (HugeInteger,HugeInteger);
     bool isZero (HugeInteger);
       enum { SIZE = 40 };

 private:
       void setHugeInteger();
     char numberArray[41];
     bool errorFlag;
       //static const int SIZE= 40;       
       //char userInput [SIZE+1];
      
     
};

#endif
0
urobins
Asked:
urobins
  • 9
  • 9
  • 5
  • +1
3 Solutions
 
jkrCommented:
For 'strcmp()', you need to add

#include <string.h>
0
 
jkrCommented:
BTW, your

#include<string>

only pulls in teh 'std::string' class. The C-style 'strcmp()' that compares character arrays can be found in 'string.h'
0
 
jkrCommented:
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
urobinsAuthor Commented:
Thanks, I put that in, but it tells me the following..  I thought since these were essentially character arrays I could do this... am I wrong in thinking?


error C2664: 'strcmp' : cannot convert parameter 1 from 'HugeInteger' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
error C2664: 'strcmp' : cannot convert parameter 1 from 'HugeInteger' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
0
 
jkrCommented:
Try



bool isEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      if (strcmp (hugeInteger1.numberArray, hugeInteger2.numberArray) == 0)
      {
            return true;
      }
      else
      {
            return false;
      }
}

You want to compare the arrays, not the classses.
0
 
Deepu AbrahamR & D Engineering ManagerCommented:

in HugeInteger.h under public declaration define the function

public:
char* getNumberArray(){ return numberArray;}

and in the cpp file write the code as below:

if (strcmp (hugeInteger1.getNumberArray(), hugeInteger2.getNumberArray()) == 0)

Best Regards,
DeepuAbrahamK
0
 
jkrCommented:
Ooops, sorry, that will only work if you ,ake the functions friends. Better add an accessor, e.g.

class HugeInteger
{
public:
     HugeInteger();
     int theLength();
       void enterHugeInteger();
     void print();
     void addition (HugeInteger,HugeInteger);
     void subtraction (HugeInteger,HugeInteger);
     void multiplication (HugeInteger,HugeInteger);
     HugeInteger multiplicationHelper (HugeInteger a, int theDigit, int thePosition);
     bool isEqualTo (HugeInteger,HugeInteger);
     bool isNotEqualTo (HugeInteger,HugeInteger);
     bool isGreaterThan (HugeInteger,HugeInteger);
     bool isLessThan (HugeInteger,HugeInteger);
     bool isGreaterThanOrEqualTo (HugeInteger,HugeInteger);
     bool isLessThanOrEqualTo (HugeInteger,HugeInteger);
     bool isZero (HugeInteger);
       enum { SIZE = 40 };

     const char* getArray() const () { return numberArray;} // <-----------------!
 private:
       void setHugeInteger();
     char numberArray[41];
     bool errorFlag;
       //static const int SIZE= 40;      
       //char userInput [SIZE+1];
     
     
};

//...

bool isEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      if (strcmp (hugeInteger1.getArray(), hugeInteger2.getArray()) == 0)
      {
            return true;
      }
      else
      {
            return false;
      }
}
0
 
urobinsAuthor Commented:
ohhhh... can't believe I missed that.  Thanks I'll give it a shot
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
same thing as I mentioned :
char* getNumberArray(){ return numberArray;}
0
 
evilrixSenior Software Engineer (Avast)Commented:
strcmp is for comparing strings, 'HugeInteger' is a user defined class that is not implicitly convertible to a const char * data type.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
almost the same time there :)
0
 
urobinsAuthor Commented:
Thanks that does work only I wasted my time going about this the wrong way :)
I think the way I want to use these in my driver it will be easier if I just over ride the operator from the beginning
If I wanted to try something like this how would I go about it...   What I have below is returning 4 errors

---------------
Errors

error C2228: left of '.getNumberArray' must have class/struct/union
1>        type is 'const HugeInteger *const '
1>        did you intend to use '->' instead?
error C2662: 'HugeInteger::getNumberArray' : cannot convert 'this' pointer from 'const HugeInteger' to 'HugeInteger &'
1>        Conversion loses qualifiers
error C2228: left of '.getNumberArray' must have class/struct/union
1>        type is 'const HugeInteger *const '
1>        did you intend to use '->' instead?
error C2662: 'HugeInteger::getNumberArray' : cannot convert 'this' pointer from 'const HugeInteger' to 'HugeInteger &'
1>        Conversion loses qualifiers



_______________


bool operator==( const HugeInteger &operand2 ) const
 {
       if (this.getNumberArray() == operand2.getNumberArray())
             return true;
       else
             return false;
 }
 bool operator!=( const HugeInteger &operand2 ) const
 {
       if (this.getNumberArray() == operand2.getNumberArray())
             return true;
       else
             return false;
 }
0
 
evilrixSenior Software Engineer (Avast)Commented:
Note: Rather than this...

if (this.getNumberArray() == operand2.getNumberArray())
             return true;
       else
             return false;


You can just do this...

return (this.getNumberArray() == operand2.getNumberArray());
0
 
evilrixSenior Software Engineer (Avast)Commented:
this requires -> and not .

this->getNumberArray()
0
 
jkrCommented:
Well, that should be

bool operator==( const HugeInteger &operand2 ) const
 {
       if (!strcmp(this.getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }
 bool operator!=( const HugeInteger &operand2 ) const
 {
       if (strcmp(this.getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }
0
 
jkrCommented:
Sorry, correction:

bool operator==( const HugeInteger &operand2 ) const
 {
       if (!strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }
 bool operator!=( const HugeInteger &operand2 ) const
 {
       if (strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }

No 'this' needed inside an object.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Something like this: -

Nb. you can use == operator to get !=
bool operator==( const HugeInteger &operand2 ) const
{
	return(this->getNumberArray() == operand2.getNumberArray());
}
bool operator!=( const HugeInteger &operand2 ) const
{
	return !(*this == operand2);
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Or, as jkr has pointed out, you might need to use strcmp
bool operator==( const HugeInteger &operand2 ) const
{
	return(strcmp(this->getNumberArray(), operand2.getNumberArray()));
}
bool operator!=( const HugeInteger &operand2 ) const
{
	return !(*this == operand2);
}

Open in new window

0
 
urobinsAuthor Commented:
I tried JKR's solution and I am getting 5 errors is there something I am missing... I have been making changes along the way here is my new code... I really appreciate all of your help, you guys are great.  I've been working on this all week.  I had completely scrapped my originial code and restarted twice :)  Good thing I am an admin and don't have any aspirations of being a professional coder I guess :)

#include"HugeInteger.h"
#include<iostream>
#include<string>
#include <string.h>

//const int SIZE = 40;

using namespace std;

HugeInteger::HugeInteger()
{
     setHugeInteger();
     errorFlag = false;
}

//read in value from the user to set huge integer
void HugeInteger::enterHugeInteger()
{
     
     char userInput [SIZE+1];
     bool notNumber  = false;
     int i = 0;
       
     cin.getline( userInput, sizeof userInput );

     
     cin.sync();
     cin.clear();

     //see if the string entered is an unsigned integer
     while(i < strlen(userInput)-1)
     {
          if (!isdigit(userInput[i]))
               notNumber = true;
          i++;
     }

     if (!notNumber)
     {
          i = strlen(userInput);

          while(i >= 0)
          {
             numberArray[SIZE-strlen(userInput)+i] = userInput[i];
             i--;
          }
     }
}

void HugeInteger::setHugeInteger()
{
     for (int i = 0; i < SIZE; i++)
          numberArray[i] = '0';
     numberArray[SIZE] = '\0';
}

int HugeInteger::theLength()
{
      int i;
     for ( i = 0; i < sizeof numberArray-2; i++) //the last 0 is significant
     {                                             //so preserve it
          if( numberArray[i] == '0');
          else
               break;
     }
     return i;
}


void HugeInteger::print()
{
     for (int x = theLength();  x < sizeof numberArray; x++)
          cout << numberArray[x];
}

void HugeInteger::addition (HugeInteger a,HugeInteger b)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int temporarySum = 0;
     int value = 0;
     int carry = 0;

     currentPosition = SIZE-1;

     while (currentPosition >= 0)
     {
          currentValueB = b.numberArray[currentPosition]-'0';
          currentValueA = a.numberArray[currentPosition]-'0';
          temporarySum = currentValueA + currentValueB + carry;
          carry = temporarySum/10;
          value = temporarySum%10;
          numberArray[currentPosition] = value + '0';
          currentPosition--;
     }

     if (carry > 0)
     {
          setHugeInteger();
         errorFlag = true;
     }
}

void HugeInteger::subtraction (HugeInteger a,HugeInteger b)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int value = 0;
     int borrow = 0;

     currentPosition = SIZE-1;

     while (currentPosition >= 0)
     {
          currentValueB = b.numberArray[currentPosition]-'0';
          currentValueA = a.numberArray[currentPosition]-'0';

          value = currentValueA-currentValueB+borrow;
          if (value < 0)
          {
               value += 10;
               borrow = -1;
          }
          else
               borrow = 0;

          numberArray[currentPosition] = value + '0';
          currentPosition--;
     }

     if (borrow < 0) // an underflow has occurred
     {
          setHugeInteger();
         errorFlag = true;
     }
}

void HugeInteger::multiplication (HugeInteger a, HugeInteger b)
{
     int currentPositiona = 0;
     int currentPositionb = 0;
     int theDigit = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int n = 0;
     int value = 0;
     int carry = 0;
     HugeInteger c,d;

     currentPositionb = SIZE-1;
     while (currentPositionb >= 0)
     {
          theDigit = b.numberArray[currentPositionb]-'0';
         c = c.multiplicationHelper(a,theDigit,currentPositionb);
        d.addition(d,c);
          currentPositionb--;
     }

     for (int i = 0; i < sizeof numberArray - 1; i++)
          numberArray[i] = d.numberArray[i] ;
}


HugeInteger HugeInteger::multiplicationHelper (HugeInteger a, int theDigit, int thePosition)
{
     int currentPosition = 0;
     int currentValueA = 0;
     int currentValueB = 0;
     int temporaryMultipliedValue = 0;
     int value = 0;
     int carry = 0;
     HugeInteger c;

     currentPosition = SIZE-1;
     for (int i = thePosition; i < SIZE-1; i++) theDigit *= 10;
     while (currentPosition >= 0)
     {
          currentValueB = theDigit;
          currentValueA = a.numberArray[currentPosition]-'0';
          temporaryMultipliedValue = currentValueA * currentValueB + carry;
          carry = temporaryMultipliedValue/10;
          value = temporaryMultipliedValue%10;
          c.numberArray[currentPosition] = value + '0';
          currentPosition--;
     }
     if (carry > 0) // an overflow has occurred
     {
          c.setHugeInteger();
          c.errorFlag = true;
     }    
     return c;
}

bool operator==( const HugeInteger &operand2 ) const
 {
       if (!strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }
 bool operator!=( const HugeInteger &operand2 ) const
 {
       if (strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }



bool isGreaterThan (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
bool isLessThan (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isGreaterThanOrEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isLessThanOrEqualTo (HugeInteger hugeInteger1,HugeInteger hugeInteger2)
{
      //still need to work
      return true;
}
     
bool isZero (HugeInteger hugeInteger)
{
      //still need to work
      return true;
}

--------------
#define HugeInteger_H

class HugeInteger
{
public:
     HugeInteger();
     int theLength();
       void enterHugeInteger();
     void print();
     void addition (HugeInteger,HugeInteger);
     void subtraction (HugeInteger,HugeInteger);
     void multiplication (HugeInteger,HugeInteger);
     HugeInteger multiplicationHelper (HugeInteger a, int theDigit, int thePosition);
     bool operator==( const HugeInteger &operand2 ) const;
     bool operator!=( const HugeInteger &operand2 ) const;
     bool isGreaterThan (HugeInteger,HugeInteger);
     bool isLessThan (HugeInteger,HugeInteger);
     bool isGreaterThanOrEqualTo (HugeInteger,HugeInteger);
     bool isLessThanOrEqualTo (HugeInteger,HugeInteger);
     bool isZero (HugeInteger);
      
       //Accessors
       char* getNumberArray(){ return numberArray;}
       //Static
       enum { SIZE = 40 };
 private:
       void setHugeInteger();
     char numberArray[41];
     bool errorFlag;
       //static const int SIZE= 40;       
       //char userInput [SIZE+1];
      
     
};

#endif


error C3861: 'getNumberArray': identifier not found
error C2270: '!=' : modifiers not allowed on nonmember functions
error C2805: binary 'operator !=' has too few parameters
error C2662: 'HugeInteger::getNumberArray' : cannot convert 'this' pointer from 'const HugeInteger' to 'HugeInteger &'
1>        Conversion loses qualifiers
 error C3861: 'getNumberArray': identifier not found
0
 
jkrCommented:
That should be


       //Accessors
       const char* getNumberArray() const { return numberArray;}

and

bool HugeInteger::operator==( const HugeInteger &operand2 ) const
 {
       if (!strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }
 bool HugeInteger::operator!=( const HugeInteger &operand2 ) const
 {
       if (strcmp(getNumberArray(),operand2.getNumberArray()))
             return true;
       else
             return false;
 }

You were missing the class part.
0
 
urobinsAuthor Commented:
doh!  it has been a long day :)
0
 
urobinsAuthor Commented:
That did it, thanks guys, I really appreciate I will split the points because you guys chimed in at the same time and all really helped
0
 
urobinsAuthor Commented:
everyone was very helpful and really helped me out.
0
 
urobinsAuthor Commented:
Another question.  Is there a string function that will allow me to determine if a string is > than another?

I want to overload the > than and < than but I don't know how to get a value on it...
0
 
jkrCommented:
Yes, it is 'strcmp()' ;o)

From http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html

Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
0
 
urobinsAuthor Commented:
oh :)
so I could say if if (strcmp(getNumberArray(),operand2.getNumberArray()>0))
return true

I think I get it :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 9
  • 9
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now