Solved

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

Posted on 2008-02-12
Medium Priority
273 Views
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.";
}
}
0
Question by:EE_wanna_be
• 6
• 4

LVL 53

Expert Comment

ID: 20877766
>> this is where I have the problem.

What's the problem exactly ?
0

Author Comment

ID: 20877941
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 Comment

ID: 20877998
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

LVL 53

Expert Comment

ID: 20878294
>> 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 Comment

ID: 20878935
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

LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 20879115
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

Author Comment

ID: 20879125
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

LVL 53

Expert Comment

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

See my last post ;)
0

Author Comment

ID: 20879171
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 Closing Comment

ID: 31430268
Thank You!!!..I am sure there will be more where that came from...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
###### Suggested Courses
Course of the Month7 days, 1 hour left to enroll