i have to decide a program to "satisfy" my crazy private tutor .This program reads in 2 input extremely large postive integers. It will then output their sum.
However , as C++ got limit in int, so i think i need to use string variables to represent the values..then use some loops to get pairs of digits add up together..

but then what if the length of the 2 strings are different or the digits add up >= 10??......it is driving me mad.......

please stick to my suggestions and avoid showing me some higher levels code.......or else my tutor will know i am not doing it all by myself........

Recommended Answers

All 11 Replies

Well how do you add two numbers on paper?

Work right to left, and introduce the idea of 'carry'.

A few things: We only provide help, we don't give away codes. You must also try to think about it on your own.
Minimum value for a variable of type short. –32768.
Maximum value for a variable of type short. 32767.
Visit here for some other relavnt information:http://msdn2.microsoft.com/en-us/library/ms860861.aspx I have the entire program (i had to do it a while back for a project), so i'll help you along but give it a shot. See what you come up with and post it here. See you around.

of course i dun expect a full code answer...I still want to taste the fun of enjoying the problem

now this is what i have got so far..

#include<iostream>
#include<string>


using namespace std;


int main () {


string a, b;
cin>>a>>b;


int le1 = a.length();
int le2 = b.length();


int i = 0;
int j = 0;


if ( le1<le2)
while ( i < le2-le1-1 ) {
cout<<b;
i++;}


if ( le1>le2)
while ( i < le1-le2-1 ) {
cout<<a;
i++;}


while ( ----this loop will add up the digits place to place...but i dunno how to start it...
....the above two loops are for directly displaying out the "excess" part when the length of one integer is longer than the other.....)



return 0;
}

A few things. A decent start, but remember to include the header <cmath>. Another thing, are you wanting to determine the lenght of the number that the user inputs?

length is not to be fixed..that's why i have to use a loop

Take a brief look at this:

#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>

using namespace std;


int main()
{

	int num1 = 0;
	int num2 = 0;

	cout<<"Please enter two numbers to get their total"<<endl;
	cin>>num1>>num2;

	double sum = (num1 + num2);
	cout<<sum<<endl;
	return 0;
}

Also, when you're posting your code, please place them in code & # number tags as it makes it easier to read for those helping.

Thank you for ur help ..but it seems that this method the numbers have been restricted by a range..so i am afraid it is completely different from what i am going to do...anyway..Thx and i think i will juz try my best to complete this task.

well, if you change the 'int' to another number characteristic, you'll bypass some of the restrictions. What restrictions are you trying to bypass or implement?

this is not the purpose of what i am going to do to the limit set by C++ for int; i think i will stick to my string-loop method and add up the digits place to place....and perhaps think hard on how to carry value up tp the left one when previous add>=10

The string/loop approach is probably the best at this point, however, you need some mechanism to add two values together, which I think is what Zandiago was trying to point out.

I'd recommend taking a step few steps back and try answering each of the following questions one at a time building on each previous answer.

If you have a string of char representing 1 digit per char how do you access a given char(digit) within the given string? Once you have a given char from within a given string how do you convert it to a numerical value? Once you can do that for a given string, do it for two strings and add the two values together. Then how would you convert the result back to a char that you could store in a third string? Where in the third string would you store the resultant char? Then how would you determine if you need to "carry" a value and if you have to "carry" how would you determine what to "carry" and what to store where if you "remove" the "carry"? What does the need to "carry" mean in terms of the program, where would the "carry" go in the resultant string compared to the two char you took out of the two original strings and how might you accomplish this?

If you write a program to answer the first question, then expand that program to answer each successive question and merge the resultant program with what you have already, then you probably won't have too much else to do.

I've seen other approaches to this problem, but this seems like a common approach for those of us who don't earn a living doing this sort of stuff.

Thanks to all who had replied. I have completed this problem at the end..though it is quite different from my original one. (but still is about strings and loops)

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.