| | |
Alphabet to Integer conversion program== Segmentation Fault.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am getting a segmentation fault when i run the follwing program.
This program takes in a const char* and returns the integer format.
I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.
Here is the code
Code tags work sometimes and code tags donot work.
This program takes in a const char* and returns the integer format.
I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.
Here is the code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> int atoi(const char*); int single_char_convert(char); int pow(int,int); int main() { std::cout<<atoi("123"); } int atoi(const char *str) { int length=std::strlen(str)-1; int result=0; for(int x=0;x<=length;x++) { int y=length-x; char character=str[x]; int converted_char=single_char_convert(character); result+=(converted_char*pow(10,y)); } return result; } int single_char_convert(char a) { switch(a) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; } } int pow(int number,int powerof) { if(powerof==1) { return number; } else { return number*pow(number,--powerof); } }
Code tags work sometimes and code tags donot work.
Last edited by Sky Diploma; May 1st, 2009 at 1:13 pm. Reason: Code tag isnt taking a copy paste maybe
>I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.
To do conversions between numbers and strings
Lines 28-50 can be replaced by:
To do conversions between numbers and strings
stringstreams are often used 
Lines 28-50 can be replaced by:
return a-'0'; Last edited by tux4life; May 1st, 2009 at 1:26 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
>I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.
To do conversionsstringstreamsare often used
I pretty much dont know whether I can use stringstreams to solve this exercise.
•
•
•
•
Originally Posted by The C++ Programming Language Edition 3 by Bjarne Stroustrup has this Assignment in 6.6.16
Write a function atoi(const char*) that takes a string containing digits and returns the corresponding ints. for example
atoi("123")
returns 123
Modify atoi () to handle C++ octal and hexadecimal notations in addition to decimal numbers and also character constant notations
be true for all the constants.in different character map sets?
Narue thanks for the reply
Here is the new code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> int atoi(const char*); int single_char_convert(char); int pow(int,int); int main() { std::cout<<atoi("123"); } int atoi(const char *str) { int length=std::strlen(str)-1; int result=0; for(int x=0;x<=length;x++) { int y=length-x; char character=str[x]; int converted_char=single_char_convert(character); result+=(converted_char*pow(10,y)); } return result; } int single_char_convert(char a) { switch(a) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; } } int pow(int number,int powerof) { if (powerof==0) { return 1; } else { return number*pow(number,--powerof); } }
Last edited by Sky Diploma; May 1st, 2009 at 1:42 pm.
Last edited by tux4life; May 1st, 2009 at 1:43 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
>Will a-'0';
>be true for all the constants.in different character map sets?
Digits are required by the C++ standard to be contiguous, but if you need to convert to hexadecimal, you can't portably use that trick. Try an array instead, using the index as your value:
>be true for all the constants.in different character map sets?
Digits are required by the C++ standard to be contiguous, but if you need to convert to hexadecimal, you can't portably use that trick. Try an array instead, using the index as your value:
C++ Syntax (Toggle Plain Text)
const char digits[] = "0123456789ABCDEF";
I'm here to prove you wrong.
Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
•
•
•
•
Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ init and read serial port status from loopback plug?
- Next Thread: Need help with c++ problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






