# I need assistance with my C++ calculator code...there has to be an easier way

I am writing code for a big integer calculator...this seems to be a hot topic.  The code below is what I have started with.  I am getting stumped when it comes to how to get this program to output a large number without eliminating the extra zeros. ex...100000000 + 100000000.  This program is only a rough draft (not too user friendly).   Essetially it takes input in type char - changes it to sets of 3 int. then adds the sets of 3 int then outputs into someform...this is where I have the problem.

ps...the extra cout's are for debugging only. I have not removed them because I have not figured out
how to make this thing work yet.
/*
*
*
*******************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;
int charToInt(char a);

int main()
{
cout << "Enter a really big number:  ";
char num;
vector<char> bigNum;
vector<char> bigNum1;
vector<int> bigIntX;
vector<int> bigIntY;
ostringstream ostr;
int sum = 0, y = 0, x = 0,
bigSum = 0, rem = 0,
repeat = 1;

for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum.push_back(num);
}

int find = bigNum.size() - 1;

for (int n = 0; n <= find ; n++)
{
char numb = bigNum[find - n];

x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));

if (repeat == 3)
{
if ( cin.eof())break;
bigIntX.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}

if (n == find && repeat != 3 && sum > 0)
bigIntX.push_back(sum);

cout << repeat;
repeat++;

}

cout << "\nEnter another big number:  ";

for(;;)
{
cin >> num;
if (num == '+' ) break;
bigNum1.push_back(num);
}

int find2 = bigNum1.size() - 1;
sum = 0;
x = 0;
repeat = 1;

for (int m = 0; m <= find2 ; m++)
{
char numb = bigNum1[find2 - m];

x = x * 10;
if (x < 1)
x++;
sum += x * (charToInt(numb));

if (repeat == 3)
{
if ( cin.eof())break;
bigIntY.push_back(sum);
sum = 0;
x = 0;
repeat = 0;
}

if (m == find2 && repeat != 3 && sum > 0)
bigIntY.push_back(sum);

cout << repeat << " " << bigIntY.size() << "bsize ";
repeat++;
}
int sizeBig = 0;

if (bigIntX.size() >= bigIntY.size())
sizeBig = bigIntX.size() - 1;
if (bigIntY.size() > bigIntX.size())
sizeBig = bigIntY.size() - 1;
cout << " " << sizeBig << "szbig";

for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}

if (bigSum < 1){
else if (bigSum < 10){
cout << "not supposed to come out here";
else if (bigSum < 100){
else
cout << bigSum << "\n";

}

for (int k = sizeBig; k >= 0 ; k--)

}

/****************************************************************************************

****************************************************************************************/
int charToInt(char a)
{
switch (a)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case '0':
return 0;
default:
cerr << "\nThere was an error in the number entered.";
}
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>> this is where I have the problem.

What's the problem exactly ?
0
Author Commented:
I cannot get the output right....when I input a number with alot of zeros the program either times out or it puts out a much smaller number ex. 100000000000+ 1000000000000  ends up being 2000 or <vector out of range>..
0
Author Commented:
when the vectors add in the code:
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;

bigSum == 0 -- ends up bieng zero when I need it to be 000
know what i mean?
2 - 000 - 000 - 000 is what I need
2 - 0 - 0 - 0 is what I get
I am not sure how to do this...
I will eventually make a class with all of this stuff, but I need to work out the bugs first..
0
Commented:
>> bigSum == 0 -- ends up bieng zero when I need it to be 000

bigSum is an int. You can't store 000 in an int.

You can solve that when you output though :

#include <iomanip>     // <--- this include is needed for setfill and setw

cout << setfill('0') << setw(3) << bigSum;

will show 000 if bigSum is 0.

Reference pages :

http://www.cplusplus.com/reference/iostream/manipulators/setfill.html
http://www.cplusplus.com/reference/iostream/manipulators/setw.html
0
Author Commented:
that did something but not what I was looking for...now I am getting 002200000, it looks like somekind of progress... here is what I changed in the code (is this correct?):

for (int i = 0; i <= sizeBig ; i++)
{
bigSum = bigIntX[i] + bigIntY[i] + rem;
rem = 0;
if (bigSum > 1000){
bigSum = bigSum - 1000;
rem++;}

cout << bigSum << "\n";

}

cout << setfill('0') << setw(3) << bigSum;

for (int k = sizeBig; k >= 0 ; k--)
0
Commented:
Try :

for (int k = sizeBig-1; k >= 0 ; k--)
cout << setfill('0') << setw(3) << bigAnswer[k];

Note that you made your code unnecessarily complicated. For example the charToInt function could simply have been :

int charToInt(char a) {
if ((a < '0') || (a > '9')) {
cerr << "\nThere was an error in the number entered.";
return -1;
}
return a - '0';
}

Also, why do you group the digits per three ? There's no need for that really. More than half of your code is because of that ...
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
ok...I put it in the wrong place.  I think it will work here:

for (int k = sizeBig; k >= 0 ; k--)
cout << setfill('0') << setw(3)<< bigAnswer[k];

but for example when I add 1000 + 1000 I get 002000... how do I fix that problem?
0
Commented:
>> but for example when I add 1000 + 1000 I get 002000... how do I fix that problem?

See my last post ;)
0
Author Commented:
I am using this format because this is the way I was requested to do it....I think it is a little ridiculous myself...I am just doing as requested.

thank you for the charToInt simplification....I will try it out
0
Author Commented:
Thank You!!!..I am sure there will be more where that came from...
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.