Question

In: Computer Science

Why does my code print nothing in cout? I think my class functions are incorrect but...

Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why.

bigint.h:

#include <string>
#include <vector>

class BigInt {
private:
   int m_Input, m_Temp;
   std::string m_String = "";
   std::vector<int> m_BigInt;
public:
   BigInt(std::string s)   // convert string to BigInt
   {
       m_Input = std::stoi(s);
       while (m_Input != 0)
       {
           m_Temp = m_Input % 10;
           m_BigInt.push_back(m_Temp);
           m_Input %= 10;
       }

   }
   std::string to_string() // get string representation
   {
       for (auto digit : m_BigInt)
       {
           m_String += std::to_string(digit);
       }
       return m_String;
   }


   void add(BigInt b) // add another BigInt to this one
   {
       for (int digit = 0; digit != b.getVect().size(); digit++)
       {
           this->getVect()[digit] += b.getVect()[digit];
       }
   }


   std::vector<int> getVect() // get BigInt vector
   {
       return m_BigInt;
   }
};

test1.cpp:

#include "bigint.h"
#include
using namespace std;

int main() {
   BigInt a("13");
   BigInt b("42");
   b.add(a);    // increase b by a
   cout << b.to_string() << endl; // prints 55
   b.add(a);    // increase b by a
   cout << b.to_string() << endl; // prints 68
}

Solutions

Expert Solution

In case of any query do comment. Please rate answer as well. Thanks

Note: there were few problems in the program, but still you require to change add function to handle carry in addition. Other thing was when you were trying to find a digit to put it in bigInt vector, it was running infinitely as you were not dividing the digit by 10 to get next digit. This is corrected and also when you convert vector to string you have to use reverse iterator because you are pushing digits to the back.

Code:

#include <string>
#include <vector>

class BigInt {
private:
   int m_Input, m_Temp;
   std::string m_String = "";
   std::vector<int> m_BigInt;
public:
   BigInt(std::string s)   // convert string to BigInt
   {
       m_Input = std::stoi(s);
       while (m_Input != 0)
       {
           m_Temp = m_Input % 10;
           m_BigInt.push_back(m_Temp);           
           m_Input /= 10;
       }

   }
   std::string to_string() // get string representation
   {
       //clear the m_String every time when your print
       m_String ="";
       //you need to reverse iterate the vector
       for (auto it = m_BigInt.rbegin(); it != m_BigInt.rend(); it++) 
       {
           m_String += std::to_string(*it);
       }
       return m_String;
   }


   void add(BigInt b) // add another BigInt to this one
   {
       for (int digit = 0; digit != b.getVect().size(); digit++)
       {
           m_BigInt[digit] += b.getVect()[digit];
       }
   }


   std::vector<int> getVect() // get BigInt vector
   {
       return m_BigInt;
   }
};

=============difference in code=======

output:


Related Solutions

(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
this is my matlab code for class, my professor commented "why is the eps an input...
this is my matlab code for class, my professor commented "why is the eps an input when it is set inside the function and not specified as a variable? how do i fix? function[] = () %Declare Global Variables global KS; global KC; KC = 0; KS = 0; End = 0; while (End == 0) choice = questdlg('Choose a function', ... 'Fuction Menu', ... 'A','B','B'); switch choice; case 'A' Program = 'Start'; while strcmp(Program,'Start'); Choice = menu('Enter the Trigonometric...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I can't get the number of days to print. Here is my code: public static void...
I can't get the number of days to print. Here is my code: public static void main(String[] args) { // Prompt the user to enter year Scanner scanner = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2016): "); int year = scanner.nextInt(); for(int i = 1; i <= 12; i++) printMonth(year, i); } /** Print the calendar for a month in a year */ static void printMonth(int year, int month) { // Print the...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
I entered in this code for my programming fundamentals class for this question To encourage good...
I entered in this code for my programming fundamentals class for this question To encourage good grades, Hermosa High School has decided to award each student a bookstore credit that is 10 times the student’s grade point average. In other words, a student with a 3.2 grade point average receives a $32.0 credit. Create an application that prompts a student for a name and grade point average, and then passes the values to a method (computeDiscount) that displays a descriptive...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT