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
    Enter Second Intger (one digit at a time) //-1 ends it
    You entered:
    The sum of these numbers is 23579



    #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)"
    while(1) {
    cin >> num;
    if (num !=-1) {
    else {



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

    //display the results
    cout << "you entered the numbers: "<<endl;
    display(arr, cnt);
    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. Advertisements

  2. 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.
    int main( ) {

    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
    Gary Labowitz, Jul 8, 2004
    1. Advertisements

  3. * 1111111111:
    '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.

    You mean, "one per line".

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

    Use ++i, not i++.

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

    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.

    I'd structure that a little bit differently, as a function returning
    a string, and with no newline char in it.
    Alf P. Steinbach, Jul 8, 2004
  4. 1111111111

    1111111111 Guest

    How would I go about doing this?????
    1111111111, Jul 9, 2004
  5. 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 Harrison, Jul 9, 2004
  6. 1111111111

    osmium Guest

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

    Arijit Guest

    (Alf P. Steinbach) wrote in message


    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, Jul 9, 2004

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

    Victor Bazarov, Jul 9, 2004
  9. 1111111111

    Arijit Guest

    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;

    { cin >> arr1; }

    { cin >> arr2[j]; }


    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;

    arr3[k] += arr2[j];
    arr3[k-1] = arr3[k] / 10;
    arr3[k] %= 10;
    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
  10. 1111111111

    JKop Guest


    i += 1;


    int temp = i;
    return temp;

    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
  11. 1111111111

    Old Wolf Guest

    Some compilers would give a warning along the lines that the
    test-expression in the while loop is always true.
    Why should it be converted to bool? Doesn't the standard say that
    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
  12. Don't you mean "while((x) != 0)"?
    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
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.