Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:24:20+00:00 2026-05-31T13:24:20+00:00

I made a MegaInt class that could handle very large numbers and overloaded a

  • 0

I made a MegaInt class that could handle very large numbers and overloaded a few operators. Unfortunately I’ve gotten stuck and my code just crashes.

Each MegaInt ojb has a vector value and an bool sign. Each position of a number is placed in the vector (i.e. 4325 is vector value = {5,2,3,4}) and whether its sign (+ or -) is 1 or 0.

here’s a bit of the code…

        #include <vector>
        #include <string>
        using namespace std;

        class MegaInt{
            friend class main;
            private:
                vector<int> value;
                bool sign; //true is pos, false is neg

            public:
                MegaInt(vector <int> x, bool y);
                MegaInt(string s);
                MegaInt(const MegaInt& m);
                MegaInt & operator*=(const MegaInt &rhs);

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

                MegaInt::MegaInt(string s){
                    int pos = s.length()-1;

                    while (pos >= 0){
                        if(pos == 0 && s[pos] == '-'){
                            sign = false;
                            break;
                        }
                        else if (pos == 0 && s[pos] == '+'){
                            sign = true;
                            break;
                        }
                        else{
                            sign = true;
                        }
                        if(s[pos] >= 48 && s[pos] <= 57)
                            value.push_back(s[pos]-48);
                        else{
                            value.clear();
                            break;
                        }
                        pos --;
                    }
                    chopoffleadingOs();
                }
                MegaInt::MegaInt(const MegaInt& m){
                    value = m.value;
                    sign = m.sign;
                }

                MegaInt operator*(const MegaInt& x, const MegaInt& y){
                    int multi = 0;
                    int temp;
                    vector<int> total;

                    for(int i = x.value.size()-1; i>=0; --i){
                        for(int j = y.value.size()-1, k = 0; j>=0; --j, ++k){
                            temp = x.value[i] * y.value[j];
                            if (total.size() <= (i + multi + 1))
                                                     total.resize(i + multi + 1 + 1);
                                                total[i + multi] += (temp % 10);
                                                temp = (temp - total[i]) / 10; 
                                                total[i + multi + 1] += temp;
                                                     }
                                                     multi++;
                                                    }

                                        reverse(total.begin(), total.end());

                return newTotal;
                }

I mainly seem to be stuck on the overloaded multiplication part. The rest I think i got.

Thanks,
Noah

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:24:20+00:00Added an answer on May 31, 2026 at 1:24 pm

    One likely the problem is here:

    total[i+multi]+=(8%10);
    

    You use the total vector, but haven’t allocated memory for it. This can be done through the resize function:

    total.resize(MAX_SIZE);
    

    A good way to find unexpected crashes is to use the debugger. If you run the program in a debugger, it will halt when there is a problem so you can examine variables and see both where the crash is and what may have caused it.

    Edit:

    If you don’t know the size of the total vector beforehand, you have to resize it dynamically:

    if (total.size() <= (i + multi + 1))
        total.resize(i + multi + 1 + 1);  // An extra +1 because vectors are zero-indexed
    
    total[i+multi]+=(8%10);
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a code that translate strings to match each word from the array
I made a UIView using Interface Builder with a top bar that has Cancel
Made a fiddle for this: http://jsfiddle.net/terjeto/MN4FJ/ My problem is that dragleave fires when you
I made a custom DatePicker that extends the android DatePicker UI component. I needed
I made the mistake of calling authorize during a valid session. I was very
I made this app that takes time entries from fogbugz and posts them to
I made a function that overwrite the the :hover of some elements on a
I made a game in VB .Net that uses tcp and sends messages back

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.