Need some help with this program

This is a discussion on Need some help with this program within the C++ Programming forums, part of the General Programming Boards category; So, I am required to make something that adds and substracts two 16 digit numbers, each of them composed of ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Need some help with this program

    So, I am required to make something that adds and substracts two 16 digit numbers, each of them composed of two 8 digit numbers respectively.
    This needs to be done using integers only and using only If, switch or for.

    what I have till now is this
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    
    {
    
      int number1l, number1r, number2l, number2r;
      int suml, sumr, diffl, diffr;
      char ope;
    
    
      cout << "number 1:";
      cin >> number1l >> number1r;
    
      cout << "number 2:";
      cin >> number2l >>  number2r;
    
      suml = number1l + number2l;
      sumr = number2r + number2r;
    
      diffl = number1l - number2l;
      diffr = number2r - number2r;
    
      if (sumr > 99999999)
        suml++;
    
     else if (diffr < 99999999)
        {  diffl--;
      diffr+=10000000;
        }
        
      cout << setfill('0');
      cout << "sum:"  << setw(8) <<  suml << setw(8) << sumr;
      cout << "diff" <<  setw(8) <<  diffl << setw(8) << diffr;
    
      return 0;
    }

    It all goes fine but somehow in the end I end up getting wrong calculations.

    If i put 11111111 11111111 and 22222222 22222222
    i get 3333333344444444 as sum and 1111111210000000 as difference.
    What am i doing wrong here, any help would be appreciated.

    Edit: Nevermind, found out what was wrong :/
    Last edited by Brother_Sharp; 05-28-2011 at 12:58 AM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    If you look carefully you aren't actually adding the right things.

    number1r adds number2r, number1l adds number2l.

    This finds the right sum for the specific problem you mention, though I don't know what you would do for a carry.
    The same mistake is repeated for minus.
    Last edited by whiteflags; 05-28-2011 at 01:58 AM. Reason: I misunderstood

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 08:42 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21