| | |
Alphabet to Integer conversion program== Segmentation Fault.
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.
"You can't build a reputation on what you are going to do."
•
•
•
•
>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.
"You can't build a reputation on what you are going to do."
>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";
In case you were wondering, yes, I do hate you.
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?
"You can't build a reputation on what you are going to do."
![]() |
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
Views: 979 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






