Velocity Reviews > C++ > Could Someone Help me on this CODE.... THANKS!!

# Could Someone Help me on this CODE.... THANKS!!

1111111111
Guest
Posts: n/a

 07-08-2004
Here is what I have so far... User is to enter 2 integers. One digit
at a time. After that I have to print out what the user entered, and
then add the two together. Example:

Enter First Integer (one digit at a time) //-1 ends it
1
2
3
-1
Enter Second Intger (one digit at a time) //-1 ends it
2
3
4
5
6
-1
You entered:
123
&
23456
The sum of these numbers is 23579

//I AM LOST TRYING TO ADD THE TWO TOGETHER....

HERE IS MY CODE.. THANKS FOR HELPING!!!

#include <iostream>
using namespace std;
void display(int x[], int n);

void main() {

int arr[100],num;
int i=0, cnt;
cout << "enter the digits of your first integer (one at a time)"
<<endl;
while(1) {
cin >> num;
if (num !=-1) {
arr[i]=num;
i++;
}
else {
break;

}
}

cnt=i;

int arr1[100],num2;
int j=0, cnt1;
cout << "enter the digits of your second integer (one at a
time)"<<endl;
while(1) {
cin >> num2;
if (num2 !=-1) {
arr1[j]=num2;
j++;
}
else {
break;
}
}
cnt1=j;

//display the results
cout << "you entered the numbers: "<<endl;
display(arr, cnt);
cout<<"and"<<endl;
display(arr1, cnt1);

}

void display(int x[], int n) {
int i;
for (i=0; i<n; i++) {
cout << x[i];
}
cout << "\n";
}

Gary Labowitz
Guest
Posts: n/a

 07-08-2004
"1111111111" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Here is what I have so far... User is to enter 2 integers. One digit
> at a time. After that I have to print out what the user entered, and
> then add the two together. Example:
>
> Enter First Integer (one digit at a time) //-1 ends it
> 1
> 2
> 3
> -1
> Enter Second Intger (one digit at a time) //-1 ends it
> 2
> 3
> 4
> 5
> 6
> -1

Some quibbles. You didn't enter an integer, you entered three integers.
Entering 1 then 2 then 3 would require you to scale the integers you have
and add them together to get a single integer. (1 * 100 + 2 * 10 + 3 = 123).
You would need an algorithm to do that to get the final integer with your
Of course, why not allow the user to enter the integer 123 as a single
entry? It's not clear what the assignment is, so it may have called for the
"one-digit-at-a-time" entry, but we don't know.

> You entered:
> 123
> &
> 23456
> The sum of these numbers is 23579
>
> //I AM LOST TRYING TO ADD THE TWO TOGETHER....
>
> HERE IS MY CODE.. THANKS FOR HELPING!!!
>
> #include <iostream>
> using namespace std;
> void display(int x[], int n);
>
> void main() {

int main( ) {

>
> int arr[100],num;
> int i=0, cnt;
> cout << "enter the digits of your first integer (one at a time)"
> <<endl;
> while(1) {
> cin >> num;
> if (num !=-1) {
> arr[i]=num;
> i++;
> }
> else {
> break;
>
>
> }
> }
>
> cnt=i;
>
> int arr1[100],num2;
> int j=0, cnt1;
> cout << "enter the digits of your second integer (one at a
> time)"<<endl;
> while(1) {
> cin >> num2;
> if (num2 !=-1) {
> arr1[j]=num2;
> j++;
> }
> else {
> break;
> }
> }
> cnt1=j;
>
> //display the results
> cout << "you entered the numbers: "<<endl;
> display(arr, cnt);
> cout<<"and"<<endl;
> display(arr1, cnt1);
>
> }
>
> void display(int x[], int n) {
> int i;
> for (i=0; i<n; i++) {
> cout << x[i];
> }
> cout << "\n";
> }

Well, you got the digits into arrays and printed out the whole array one
digit at a time. It makes it look like a single integer, but it isn't. And
that's why you have a problem adding the numbers together -- you havn't got
two numbers yet.
I'm afraid the whole design at this point is making the assignment too
cumbersome (although it still can be done by multiplying and adding the
digits). The best bet at this point is to rethink how an entire integer can
several digits that are entered before the user hits Enter?
Think it over and try again. But you did an interesting job using arrays for
this.
--
Gary

Alf P. Steinbach
Guest
Posts: n/a

 07-08-2004
* 1111111111:
> Here is what I have so far... User is to enter 2 integers. One digit
> at a time. After that I have to print out what the user entered, and
> then add the two together. Example:
>
> Enter First Integer (one digit at a time) //-1 ends it
> 1
> 2
> 3
> -1
> Enter Second Intger (one digit at a time) //-1 ends it
> 2
> 3
> 4
> 5
> 6
> -1
> You entered:
> 123
> &
> 23456
> The sum of these numbers is 23579
>
> //I AM LOST TRYING TO ADD THE TWO TOGETHER....
>
> HERE IS MY CODE.. THANKS FOR HELPING!!!
>
> #include <iostream>
> using namespace std;
> void display(int x[], int n);
>
>
> void main() {

'main' must have return type 'int'.

If your compiler doesn't issue a warning or error it's broken.

The Visual C++ compiler is one example.

> int arr[100],num;
> int i=0, cnt;
> cout << "enter the digits of your first integer (one at a time)"

You mean, "one per line".

> <<endl;
> while(1) {

Better use 'for( ;; )' or else you risk a compilation warning.
Also, if you absolutely must use 'while', then 'while( true )'.

> cin >> num;
> if (num !=-1) {
> arr[i]=num;
> i++;

Use ++i, not i++.

> }
> else {
> break;

Why not simply 'if( num == -1 ) { break; }'?

Also, more serious, here you risk a _buffer overflow_ if the
user enters more than one hundred digits.

You should check for that, and that necessitates using a symbolic
constant for the array size.

>
> }
> }
>
> cnt=i;
>
> int arr1[100],num2;
> int j=0, cnt1;
> cout << "enter the digits of your second integer (one at a
> time)"<<endl;
> while(1) {
> cin >> num2;
> if (num2 !=-1) {
> arr1[j]=num2;
> j++;
> }
> else {
> break;
> }
> }
> cnt1=j;

Note that this is virtually the same code as before.

So this should be made into a function.

Also, not that array plus a count of elements is essentially what
a std::vector gives you, so do use a std::vector instead.

> //display the results
> cout << "you entered the numbers: "<<endl;
> display(arr, cnt);
> cout<<"and"<<endl;
> display(arr1, cnt1);

Now, how to "add together" the numbers depends on whether you can
assume a limited range (so you can convert to integer and use the

If not, the use a loop that starts at the least significant digit,
adds those, and from the result computes the 0...9 range digit
and carry (if any). Next iteration of the loop, add the carry and
the next least significant digits. And so on.

> }
>
> void display(int x[], int n) {
> int i;
> for (i=0; i<n; i++) {
> cout << x[i];
> }
> cout << "\n";
> }

I'd structure that a little bit differently, as a function returning
a string, and with no newline char in it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

1111111111
Guest
Posts: n/a

 07-09-2004
>
> Now, how to "add together" the numbers depends on whether you can
> assume a limited range (so you can convert to integer and use the
>

How would I go about doing this?????

John Harrison
Guest
Posts: n/a

 07-09-2004

"1111111111" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> >
> > Now, how to "add together" the numbers depends on whether you can
> > assume a limited range (so you can convert to integer and use the
> > built-in addition) or not.
> >

>
> How would I go about doing this?????

If you can do it on a piece of paper it would be a start. You can use a

When you add numbers on paper you have to make sure that the numbers line
up, so that you add ones to ones, tens to tens, hundreds to hundreds. If any
if the additions give you a number of 10 or more then you have to carry one
into the next column.

Does any of this sound familiar? It used to be taught to eight year olds but
maybe now that we have calculators etc. perhaps it isn't.

Implementing the above is actually a fairly simple for loop, about half a
dozen lines of code tops.

john

osmium
Guest
Posts: n/a

 07-09-2004
1111111111 writes:

> > Now, how to "add together" the numbers depends on whether you can
> > assume a limited range (so you can convert to integer and use the
> > built-in addition) or not.
> >

>
> How would I go about doing this?????

People are guessing (necessarily) as to what your assignment is and what you
have coded so far. I suggest posting both of these to clarify what the
situation is. Are there any constraints on the user? Is he allowed to
start typing random gibberish? Only digits? How many digits? Be sure that
you yourself understand the difference between a digit and a number.

positive numbers and the answer is negative the range for an int (or long)
has been exceeded.

Arijit
Guest
Posts: n/a

 07-09-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alf P. Steinbach) wrote in message news:<(E-Mail Removed)>...

<snip>
> > <<endl;
> > while(1) {

>
> Better use 'for( ;; )' or else you risk a compilation warning.
> Also, if you absolutely must use 'while', then 'while( true )'.
>
>
> > cin >> num;
> > if (num !=-1) {
> > arr[i]=num;
> > i++;

>
> Use ++i, not i++.

</snip>

i++ appears on a line by itself, so whats the difference
between writing i++ and ++i ? Also, why should while(1)
produce a warning and not for(; ?

-Arijit

Victor Bazarov
Guest
Posts: n/a

 07-09-2004
Arijit wrote:
> (E-Mail Removed) (Alf P. Steinbach) wrote in message news:<(E-Mail Removed)>...
>
> <snip>
>
>>><<endl;
>>> while(1) {

>>
>>Better use 'for( ;; )' or else you risk a compilation warning.
>>Also, if you absolutely must use 'while', then 'while( true )'.
>>
>>
>>
>>> cin >> num;
>>> if (num !=-1) {
>>> arr[i]=num;
>>> i++;

>>
>>Use ++i, not i++.

>
> </snip>
>
> i++ appears on a line by itself, so whats the difference
> between writing i++ and ++i ? Also, why should while(1)
> produce a warning and not for(; ?

It's just a matter of keeping good habits. 'while (true)' is better
than 'while (1)' simply because 1 (an int) doesn't have to be converted
to 'bool'. Writing ++i instead of i++ is better because you're not
using the return value (the original value of 'i' before the increment)
anyway, so why bother getting it? Yes, for integral types it most likely
doesn't matter, but if you get into the habit of writing ++i instead of
i++, you will never run into trouble where it does matter.

As to "should produce a warning", it probably should not, but just like
with other "should" type of things in life, they rarely do what they
should (or what we think they should), and a well-known compiler does
give a warning when it probably shouldn't...

Just my \$0.02...

V

Arijit
Guest
Posts: n/a

 07-09-2004
(E-Mail Removed) (1111111111) wrote in message news:<(E-Mail Removed). com>...
> >
> > Now, how to "add together" the numbers depends on whether you can
> > assume a limited range (so you can convert to integer and use the
> > built-in addition) or not.
> >

>
> How would I go about doing this?????

My guess as to your assignment is you are trying to add two
arbitrarily large integers. In that case your program will
not work for more than 100 digit long numbers.
as needed.

const int MAX = 100;
int main()
{
int arr1[MAX],arr2[MAX],arr3[MAX];
int i=0,j=0;

do
{ cin >> arr1[i]; }
while(arr1[i++]>=0);

do
{ cin >> arr2[j]; }
while(arr2[j++]>=0);

i-=2;
j-=2;

int k = (i>j) ? i : j;
arr3[++k] = 0;

for(;i>=0 && j>=0;--i,--j,--k)
{
arr3[k] += arr1[i] + arr2[j];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}

if(i<0)
for(;j>=0;--j,--k)
{
arr3[k] += arr2[j];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}
else
for(;i>=0;--i,--k)
{
arr3[k] += arr1[i];
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}

return 0;
}

You should improve on my rough attempt by using vectors.

JKop
Guest
Posts: n/a

 07-10-2004

> i++ appears on a line by itself, so whats the difference
> between writing i++ and ++i ? Also, why should while(1)
> produce a warning and not for(; ?

++i:

i += 1;

i++:

int temp = i;
return temp;
++i;

Turning the latter into the former would be an optimization. If you going to
depend on things like that, you may aswell throw the inline keyword out the
window too.