Saturday, 15 June 2013

arrays - C++ getting rid of leading zeros -



arrays - C++ getting rid of leading zeros -

i'm trying array3[arraysize] print out no leading zeros. how can this? inputs 6 123456 7 1234567

#include <iostream> #include <cmath> using namespace std; const int arraysize = 20; void initnumber(char array[arraysize]); int addarrays(char array1[arraysize], char array2[arraysize],char arrayfinal[arraysize]); void outputnumber(char array[arraysize]); int main() { int i=0; char array1[arraysize], array2[arraysize]; char array3[arraysize]; bool number = false; cout << "please come in first number" << endl; initnumber(array1); cout << endl << "please come in sec number" << endl; initnumber(array2); addarrays(array1,array2,array3); outputnumber(array3); int sum;

this problem area. seems print out รก00000000000008308642 instead of 8308642. loop improve fit while or loop.

{ if(array3[arraysize-i] != '0') // heeeeeeeeeeeeeeeeelp number = false; else number = true; sum = i++; } while(number == true); for(sum; sum <= arraysize; sum++){ // outputs terms cout << array3[arraysize-sum]; } homecoming 0; } void initnumber(char array[]){ int numberofdigits, numbercount; int i; cout << "how many digits in number? "; cin >> numberofdigits; numbercount = numberofdigits; cout << "please come in digits in number to the lowest degree important first: "; for(i = 0; < numberofdigits; i++){ // inputs terms cin >> array[i];} for(numbercount; numbercount < arraysize; numbercount++){ array[numbercount] = '0'; // inputs zeros other terms } } int addarrays(char array1[],char array2[],char arrayfinal[]){ int array1int, array2int, totalint, error =0, = 0; char totalchar; for(arraysize; arraysize-i >= 0; i++){ array1int = array1[i] - '0'; array2int = array2[i] - '0'; totalint = array1int + array2int + error; error = 0; if(totalint > 9){ error = totalint/10; arrayfinal[i] = totalint%10; } else{ arrayfinal[i] = totalint; } } cout << endl; homecoming totalint; } void outputnumber(char array3[]){ bool number; int i=0; for(arraysize; arraysize - > 0; i++){ array3[i] = array3[i] + '0'; } }

in first iteration of loop (i == 0) go out of array bounds accessing array3[arraysize-i], array3[arraysize]. should start i = 1, , add together && <= arraysize loop status right behavior if digits zeros.

demo

c++ arrays

No comments:

Post a Comment