CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    2

    adding two user input arrays

    For this program I have to be able to input two positive arrays up to 20 digits long and add them together, I think I have the input part right I am just having difficulty adding the two arrays together and outputting the new sum array...any help or advice would be great, here is what i have so far:


    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()

    {
    int count = 0, number;
    char first[20], second[20], sum[20];
    cout << "Enter the augend: ";
    cin >> noskipws >> first[0];
    cout << endl;
    while( first[count] != '\n')
    {
    count++ ;
    cin >> first[count];
    }
    for ( number = count-1; number >= 0; number--)
    cout << first[number];
    cout << endl;

    count=0;
    cout << "Enter the adden: ";
    cin >> noskipws >> second[0];
    cout << endl;
    while( second[count] != '\n')
    {
    count++;
    cin >> second[count];
    }
    for(number = count-1; number >= 0; number--)
    cout << second[number];
    cout << endl;


    number=0;
    for(number=0;number<20;number++)
    {
    sum[number] = first[number] + second[number];
    }
    cout << sum[number];
    cout << endl;

    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: adding two user input arrays

    Stick some code tags on that to preserve indenting.

  3. #3
    Join Date
    Mar 2009
    Posts
    2

    Re: adding two user input arrays

    For this program I have to be able to input two positive arrays up to 20 digits long and add them together, I think I have the input part right I am just having difficulty adding the two arrays together and outputting the new sum array...any help or advice would be great, here is what i have so far:


    Quote Originally Posted by theflash View Post
    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main()

    {
    int count = 0, number;
    char first[20], second[20], sum[20];
    cout << "Enter the augend: ";
    cin >> noskipws >> first[0];
    cout << endl;
    while( first[count] != '\n')
    {
    count++ ;
    cin >> first[count];
    }
    for ( number = count-1; number >= 0; number--)
    cout << first[number];
    cout << endl;

    count=0;
    cout << "Enter the adden: ";
    cin >> noskipws >> second[0];
    cout << endl;
    while( second[count] != '\n')
    {
    count++;
    cin >> second[count];
    }
    for(number = count-1; number >= 0; number--)
    cout << second[number];
    cout << endl;


    number=0;
    for(number=0;number<20;number++)
    {
    sum[number] = first[number] + second[number];
    }
    cout << sum[number];
    cout << endl;

    return 0;
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: adding two user input arrays

    Two areas that stick out as problematic:

    1) The inputting of the array values... why are you comparing the input to '\n'????

    2) The output of the array sums... you are only outputting sum[20], which is beyond the size of the array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured