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

1

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() {

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=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;
}
cout << "\n";
}
 
G

Gary Labowitz

1111111111 said:
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
"one-digit-at-a-time" entry method. (Start with a 0, and for each digit
entered multiply your integer by 10 and add the new digit.)
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( ) {
please.
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=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;
}
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
be read in using cin. Since you can read a single digit, why can't you read
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.
 
A

Alf P. Steinbach

* 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=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
built-in addition) or not.

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;
}
cout << "\n";
}


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

1111111111

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?????
 
J

John Harrison

1111111111 said:
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
similar method in your program.

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
 
O

osmium

1111111111 said:
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.

One answer to the question "how would I go about .." is: if you add two
positive numbers and the answer is negative the range for an int (or long)
has been exceeded.
 
A

Arijit

(e-mail address removed) (Alf P. Steinbach) wrote in message
<<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=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
 
V

Victor Bazarov

Arijit said:
(e-mail address removed) (Alf P. Steinbach) wrote in message
<<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=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
 
A

Arijit

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.
Here's a rough solution. Add a call to your display routine
as needed.

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

do
{ cin >> arr1; }
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 + 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;
arr3[k-1] = arr3[k] / 10;
arr3[k] %= 10;
}

return 0;
}

You should improve on my rough attempt by using vectors.
 
J

JKop

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.
 
O

Old Wolf

Some compilers would give a warning along the lines that the
test-expression in the while loop is always true.
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'.

Why should it be converted to bool? Doesn't the standard say that
while(x)
is equivalent to
while ((x) == 0)
? And the result of a == b is an int, either 0 or 1.

Even if you are right and it should be converted to bool, why
not write "while(1)" ? "1" converted to bool could never be "false".
This is a compile-time conversion, and it's probably slower to
read 4 chars from input than it is to "convert" 1 to true, which
I doubt would actually be an operation anyway !
 
A

Aguilar, James

Old Wolf said:
Why should it be converted to bool? Doesn't the standard say that
while(x)
is equivalent to
while ((x) == 0)
? And the result of a == b is an int, either 0 or 1.

Don't you mean "while((x) != 0)"?
Even if you are right and it should be converted to bool, why
not write "while(1)" ? "1" converted to bool could never be "false".
This is a compile-time conversion, and it's probably slower to
read 4 chars from input than it is to "convert" 1 to true, which
I doubt would actually be an operation anyway !

What? The difference in compile time would be negligible even on the
slowest modern machine. I think the real question is "What helps
readability?", in which case using a bool is definitely superior. However,
when it comes right down to it, it doesn't really matter. If the warning
bothers you, you can use a flag to turn it off in your makefile, right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top