943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2820
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 16th, 2008
0

Re: high digit numbers

Some remarks:
The C library is a standard part of the C++ library.
In particular, the atoi() function is declared in <cstdlib> C++ library header.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 17th, 2008
0

Re: high digit numbers

can u plz tell me how many parametres this atoi() function takes?
how do u use it?
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 17th, 2008
0

Re: high digit numbers

This is how atoi() is used, basically.
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. using namespace std;
  7.  
  8. int a = atoi("10");
  9.  
  10. cout << a << " squared is " << a * a << '\n';
  11. }
The basic use isn't enough unless you can guarantee that the string represents both an integer value and an integer value that fits in the range of int. If you can't guarantee that, atoi() doesn't do a good job of handling those kinds of errors.

One problem with atoi() is that it returns 0 if an error occurs. 0 is a valid integer so unless you want to ban 0 in your program, you can't reliably use it as an error indicator.

Another problem is that if the number can't be represented by int, the behavior of atoi() is undefined and there's no way around it without pre-validating the string.

The end result is that a robust use of atoi() looks more like this:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. using namespace std;
  4.  
  5. char *s;
  6.  
  7. // Initialize s and fill it with a string
  8.  
  9. if (IsInt(s)) {
  10. int a = atoi(s);
  11.  
  12. cout << a << '\n';
  13. }
  14. }
Where IsInt() is a function you write that validates the string to make sure the represented value fits in an int. The other ways of converting a char* to int may be more verbose, but they're also more user friendly when it comes to error handling.

Edward recommends that you only use atoi() when there's a 100% guarantee that the string represents a valid int and no error handling will ever be necessary.
Last edited by Radical Edward; Aug 17th, 2008 at 9:01 am.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 17th, 2008
0

Re: high digit numbers

thanks alot for all the replies
but i'm still not convinced that even with this function i'd be able to work with high digit numbers!
i guess it's just the c++ weakness in this case!
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 17th, 2008
1

Re: high digit numbers

Quote ...
By the looks of it, you're supposed to manually manage the numbers. Edward's first attempt would probably use strings to store the numbers and then handle the arithmetic just like it's taught in elementary school with digit by digit calculations and saving of the carry or borrow values.

That's the simple and easy to write solution, but it's not optimal in terms of speed or memory footprint.
Do as what Edward has said and you will able to solve your problem easily. You don't actually need integer to store number, you can just use string to represent the number. First of all, you create one string to store the input which is in the format you have mentioned before:
C++ Syntax (Toggle Plain Text)
  1. first_number[+ or -]second_number
Then create 2 other strings: the first string stores the first number, and the second string stores the second number. Then calculate it like what you have taught in your elementary school.

For example:
User's Input:  162823+562369
-> First String = 162823
-> Second String = 562369

Calculate it manaully like you would calculate by hand.

 1     1   1
 1 6 2 8 2 3  
                         +
 5 6 2 3 6 9
---------------
 7 2 5 1 9 2
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sending bitmap
Next Thread in C++ Forum Timeline: Introducing myself and a n00b help question. ;)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC