4 Replies - 553 Views - Last Post: 17 February 2016 - 11:42 PM Rate Topic: -----

#1 miguellz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-January 16

Classes and Objects

Posted 14 February 2016 - 09:36 PM

I have to add two strings using classes and objects. However I'm omitting the "addBig" function for now since I'm having trouble with the other two working. I have done the same problem without using classes and objects so I'm assuming this is where I have made the mistakes but I just don't see the issue. After I type the number to add I get random numbers printed instead of the ones I typed. Thanks everyone and it would really help if I get pointed out my mistakes not trying to get my homework done for me.
 
// The Header file
// Declare a class
 // This is file bigNumber.h

#include <iostream>
#include <string>
 
const int MAX_DIGITS=10;
 class bigNumber
 {
public:
     void readBig();
     void printBig();
     bigNumber addBig(bigNumber);
private:
     int num[MAX_DIGITS];
     
 };

/////////////////////////////////////////////////

// Define member functions of class dateType.
// This is file bigNumber.cpp.
 #include "bigNumber.h" // gain access to specification of class
 #include <string>
 #include <iostream>
 using namespace std;
 
 void bigNumber::readBig()
 {
	string str;
	cin>>str;
	int i,temp;
	for(i=0;i<=str.length()-1;i++) //This loop initializes values of array to 0.
	{
		num[i]=0;	
	}
	for (i=0;i<=str.length()-1;i++)  //This loop coverts array element to an integer and stores it
		{                            // in the array.
			num[i]=str.at(i)-'0';
		}
	for (i=0;i<=str.length()-1;i++)  //The loop reverses the elements in the array.
	{
		temp=num[i];
		num[i]=str.at((str.length()-1)-i);
		str.at((str.length()-1)-i)=temp;		
 	} 
 }
 
 /*bigNumber bigNumber::addBig(bigNumber num2)
 {
	bigNumber sum;
	int i=0, carry=0;
	for (i=0;i<=MAX_DIGITS-1;i++) //The loop will add the arrays and store them in the new array.
	{
		if(num[i]+num2.num[i]+carry>9)  //If the result interger has two digits the carry operation is performed.
		{
			
			sum.num[i]=num[i]+num2.num[i]+carry;
			sum.num[i]=sum.num[i]%10;
			carry=1;
			}
		else{
			sum.num[i]=num[i]+num2.num[i]+carry;
			carry=0;
			}
		}
		return sum;
 }
 */
 void bigNumber::printBig()
 {
	 int i, count = MAX_DIGITS-1;
	while(num[count]==0 and count >= 1)
		count--;
		
	for (i=count; i>=0; i--)
	    cout<<num[i];	    

}

/////////////////////////////////////////////////////////////

// This is the main function

#include <iostream>
#include "bigNumber.h"
using namespace std;

int main()
     {
     bigNumber num1;
     bigNumber num2;
     
     bool done = false;
     char response;
     while (not done)
         {
         cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
         num1.readBig();
         cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
         num2.readBig();
         num1.printBig();
         cout << "\n+\n";
         num2.printBig();
         cout << "\n=\n";
         //num1.addBig(num2).printBig();
         cout << "\n";
         cout <<"test again?";
         cin>>response;
         cin.ignore(900,'\n');
         done = toupper(response)!='Y';
         }
     return 0;




Is This A Good Question/Topic? 0
  • +

Replies To: Classes and Objects

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Classes and Objects

Posted 15 February 2016 - 04:00 AM

I see two problems

First:
Look at this line of code:
for(i=0;i<=str.length()-1;i++) //This loop initializes values of array to 0 

The value of str.length() depends on the length of the string, and will not necessarily cover the entire array. Your array is size MAX_DIGITS.


Second:
When you reverse your elements in your array, you are overwriting the content of num using character values from str. (So your earlier code which subtracts the value of '0' has no effect on the output you see).
num[i]=str.at((str.length()-1)-i);



'reverse' suggestion:
There are more "C++ like" ways of doing some of these things than for loops. It's good to avoid using for loops with arrays/vectors/strings/etc.

You can add the <algorithm> header to your code
 #include <algorithm> 
which will allow you to replace this block of code...
	for (i=0;i<=str.length()-1;i++)  //The loop reverses the elements in the array.
	{
		temp=num[i];
		num[i]=str.at((str.length()-1)-i);
		str.at((str.length()-1)-i)=temp;		
 	} 
with this:
std::reverse(num, num + str.length()); 
Much cleaner, more readable, and less error-prone.


Suggestion - prefer a vector instead of an array
Arrays are very "C"-like, and should be avoided. a vector is a "C++ array", and is often better for solving 'array-like' problems.

you'lll need to #include <vector> in your code. To replace the array with the vector you won't even need MAX_DIGITS any more.
e.g.
#include <vector>

class bigNumber
{
public:
    void readBig();
    void printBig();
private:
    std::vector<int> num;  // "vector of int"

};


You can hugely simplify your readBig function too:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int convert_to_int(char c)
{
    return c - '0';
}

void bigNumber::readBig()
{
    std::string str;
    std::cin >> str;

    num = std::vector<int>(str.size());  // "num is a vector with length of str.size()"

    // Copy the digits in reverse order, and convert to int by subtracting '0' all at the same time!
    std::transform(str.rbegin(), str.rend(), num.begin(), convert_to_int);  
}
Hopefully you're able to see this being far neater, and less error-prone than using an array directly with all those for-loops.

A vector has similar capabilities to std::string - so it allows you to use the array subscript syntax [ ], or the .at() and the .size() functions. (num.size() would replace MAX_DIGITS in your printBig function).

A vector has no fixed size, so you aren't just limited to MAX_DIGITS - you could have a number which has 7, or 400, or 10093 digits without changing anything in your program.

This post has been edited by Bench: 15 February 2016 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 miguellz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-January 16

Re: Classes and Objects

Posted 15 February 2016 - 10:20 PM

Thanks for the reply. I have to use the for loops because it's for school. I understand what you're saying about the reversing function so I changed it to this
for (i=0;i<=MAX_DIGITS/2-1;i++)  //The loop reverses the elements in the array.
	{
		temp=num[i];
		num[i]=num[MAX_DIGITS-i];
		num[MAX_DIGITS-i]=temp;		
 	} 



Now when I change my loop that initializes the array to 'MAX_DIGITS-1' instead of 'str.length()-1' the program exits after I plug in the first number.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Classes and Objects

Posted 16 February 2016 - 01:25 AM

Could you post your updated code? I assume you've changed more than just the code to reverse your number.
Was This Post Helpful? 0
  • +
  • -

#5 miguellz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-January 16

Re: Classes and Objects

Posted 17 February 2016 - 11:42 PM

View PostBench, on 16 February 2016 - 08:25 AM, said:

Could you post your updated code? I assume you've changed more than just the code to reverse your number.


I made it work. I'll post it if you still want to see it. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1