| | |
high digit numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
This is how atoi() is used, basically.
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:
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.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> int main() { using namespace std; int a = atoi("10"); cout << a << " squared is " << a * a << '\n'; }
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)
int main() { using namespace std; char *s; // Initialize s and fill it with a string if (IsInt(s)) { int a = atoi(s); cout << a << '\n'; } }
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.
If at first you don't succeed, keep on sucking until you do succeed.
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
first_number[+ or -]second_number
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
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
- beginner in mips, stuck on program please help! (Assembly)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- array help (C)
- Game ON (Geeks' Lounge)
- Psycho Hose Beast IP question (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: sending bitmap
- Next Thread: Drawing Text in OpenGL
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






