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

Discussion in 'C++' started by 1111111111, Jul 8, 2004.

  1. 1111111111

    1111111111 Guest

    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";
    }
     
    1111111111, Jul 8, 2004
    #1
    1. Advertising

  2. "1111111111" <> wrote in message
    news:...
    > 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.
    --
    Gary
     
    Gary Labowitz, Jul 8, 2004
    #2
    1. Advertising

  3. * 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.

    --
    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?
     
    Alf P. Steinbach, Jul 8, 2004
    #3
  4. 1111111111

    1111111111 Guest

    >
    > 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?????
     
    1111111111, Jul 9, 2004
    #4
  5. "1111111111" <> wrote in message
    news:...
    > >
    > > 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
    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
     
    John Harrison, Jul 9, 2004
    #5
  6. 1111111111

    osmium Guest

    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.

    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.
     
    osmium, Jul 9, 2004
    #6
  7. 1111111111

    Arijit Guest

    (Alf P. Steinbach) wrote in message news:<>...

    <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=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
     
    Arijit, Jul 9, 2004
    #7
  8. Arijit wrote:
    > (Alf P. Steinbach) wrote in message news:<>...
    >
    > <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=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
     
    Victor Bazarov, Jul 9, 2004
    #8
  9. 1111111111

    Arijit Guest

    (1111111111) wrote in message news:<>...
    > >
    > > 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.
    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.
     
    Arijit, Jul 9, 2004
    #9
  10. 1111111111

    JKop Guest


    > 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.
     
    JKop, Jul 10, 2004
    #10
  11. 1111111111

    Old Wolf Guest

    Victor Bazarov <> wrote:
    > Arijit wrote:
    > > (Alf P. Steinbach) wrote:
    > >>
    > >>Better use 'for( ;; )' or else you risk a compilation warning.
    > >>Also, if you absolutely must use 'while', then 'while( true )'.


    > > why should while(1) produce a warning and not for(;;) ?


    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 !
     
    Old Wolf, Jul 11, 2004
    #11
  12. "Old Wolf" <> wrote in message
    news:...
    > Victor Bazarov <> wrote:
    > > 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.


    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?
     
    Aguilar, James, Jul 12, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    0
    Views:
    390
  2. shapper
    Replies:
    0
    Views:
    358
    shapper
    Dec 18, 2007
  3. Replies:
    2
    Views:
    161
    rantingrick
    Aug 24, 2011
  4. MrHelpMe

    Could someone help with my ASP code below

    MrHelpMe, Feb 17, 2008, in forum: ASP General
    Replies:
    2
    Views:
    196
    s_m_b
    Feb 22, 2008
  5. Caduceus
    Replies:
    5
    Views:
    147
    Tad McClellan
    Jan 23, 2007
Loading...

Share This Page