0

whenever i execute the program it returns "Process exited with value 3221225477" and nothing else, i know it means out of bounds or something like that but i cant see where the problem here is, everything to me looks like it should work i know for a fact it in my "pas" function because the others are copy-paste and they always have worked, here is my code:

#include<bits/stdc++.h>
using namespace std;
string longDivision(string number, int divisor) 
{ 

    string ans; 


    int idx = 0; 
    int temp = number[idx] - '0'; 
    while (temp < divisor) 
       temp = temp * 10 + (number[++idx] - '0'); 

    while (number.size() > idx) 
    { 

        ans += (temp / divisor) + '0'; 


        temp = (temp % divisor) * 10 + number[++idx] - '0'; 
    } 


    if (ans.length() == 0) 
        return "0"; 

    return ans; 
} 


string multiply(string num1, string num2) 
{ 
    int len1 = num1.size(); 
    int len2 = num2.size(); 
    if (len1 == 0 || len2 == 0) 
    return "0"; 

    vector<int> result(len1 + len2, 0); 


    int i_n1 = 0;  
    int i_n2 = 0;  

    for (int i=len1-1; i>=0; i--) 
    { 
        int carry = 0; 
        int n1 = num1[i] - '0'; 

        i_n2 = 0;  

        for (int j=len2-1; j>=0; j--) 
        { 
            int n2 = num2[j] - '0'; 

            int sum = n1*n2 + result[i_n1 + i_n2] + carry; 

            carry = sum/10; 

            result[i_n1 + i_n2] = sum % 10; 

            i_n2++; 
        } 

        if (carry > 0) 
            result[i_n1 + i_n2] += carry; 


        i_n1++; 
    } 


    int i = result.size() - 1; 
    while (i>=0 && result[i] == 0) 
    i--; 


    if (i == -1) 
    return "0"; 

    string s = ""; 

    while (i >= 0) 
        s += to_string(result[i--]); 

    return s; 
} 






    string pas(string a, int b){

string c;
c[0]='1';
for(int i=1; i<=a.size()-1; i++){
    c[i]='0';
}
string a1=a;
a1[0]=a1[0]-c[0];
string p=to_string(b);
string k69=longDivision(a1,b);
string k=multiply(k69,p);

string pasuxi=a;
int ksize=k.size()-1;
for(int i=a.size()-1; i>=a.size()-p.size(); i--){
    if(k[ksize]>a[i]){
        pasuxi[i]=a[i]-k[ksize]+10;
        a[i-1]=a[i-1]-1;
        pasuxi[i-1]=a[i-1];
            }else{
                pasuxi[i]=a[i]-k[ksize];

            }
ksize--;


}


return pasuxi;

}



int main(){

    int n,k;
    cin>>n>>k;
    string a;
    string k1=to_string(k);
    for(int i=0; i<n; i++){
        if(i<k1.size()){
            a[i]=k1[i];
        }else{
            a[i]='0';
        }



    }



    cout<<pas(a, k);










system("pause");










}
  • 4
    This is an excellent use case of your debugger. You can single step through the program, examine variables, and see exactly where it crashes. – JohnFilleau Mar 15 '20 at 14:24
  • 2
    But this in your `pas` function: `string c; c[0]='1';` is immediately undefined behavior. `c` is empty - it has no index 0. – JohnFilleau Mar 15 '20 at 14:25
  • wait i cant define strings that way? then how am i supposed to? – otar nozadze Mar 15 '20 at 14:36
  • What's your intention? To append a character to an empty string? – JohnFilleau Mar 15 '20 at 14:37
  • https://en.cppreference.com/w/cpp/string/basic_string is the reference page for `std::string`. To append a `char`, `c` to a `std::string`, `s` you can do `s += c`, `s.append(c)`, or `s.push_back(c)`. – JohnFilleau Mar 15 '20 at 14:38
  • my intention is to create a string of length of the size of a whose first character would be '1' and the others '0'-s – otar nozadze Mar 15 '20 at 14:41
  • If you know the first character will always be `1`, you can just declare it as`"1"`. `std::string s = "1";`. To append an "other" (`n`) number of `'0'`, you can append a `std::string` to the first `std::string` that's made up of `n` `0`'s: `s += std::string(n, '0');` https://en.cppreference.com/w/cpp/string/basic_string/basic_string – JohnFilleau Mar 15 '20 at 14:44
  • When you get a big and crazy number like 3221225477 the program is probably trying to tell you something. Convert it to hex (C0000005) and see if that's more familiar. – user4581301 Mar 15 '20 at 14:46
  • hmm when i use append it now says memory -limit-exceded exit code -1 – otar nozadze Mar 15 '20 at 14:50
  • in dev c++ it says "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" – otar nozadze Mar 15 '20 at 14:51
  • 1
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Asteroids With Wings Mar 15 '20 at 14:52
  • My mistake, `append` doesn't just take a `char`. I read the documentation wrong. See https://en.cppreference.com/w/cpp/string/basic_string/append for the list of allowed calls to `append`. – JohnFilleau Mar 15 '20 at 14:56
  • wow i fixed the issue there is no error codes anymore but it still doesnt output anything but that should be a easy fix – otar nozadze Mar 15 '20 at 15:01

0 Answers0