| | |
Alphabet to Integer conversion program== Segmentation Fault.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have written all conversions and this what I have ended up with . I would like to see if there are any more changes or improvements that can be made to the code.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> int atoi(const char*); // Definitions of Required Functions int single_char_convert(char); int pow(int,int); int convert(const char*,int,int); // DEFINITOINS END int main() { std::cout<<atoi("123")<<" DECIMAL \n"; std::cout<<atoi("0112")<< " Octal == 74\n"; std::cout<<atoi("0x1128")<<" HEXADECIMAL == 4392\n"; } int atoi(const char *str) { if (str[0]=='0'&&(std::tolower(str[1])=='x')) { return convert(str,16,2); //Hexadecimal } else if(str[0]=='0') { return convert (str,8,1); //Octal } else{ return convert(str,10,0); //Decimal } } int convert(const char *str,int value,int start) { int length=std::strlen(str)-1; int result=0; for(int x=start;x<=length;x++) { int y=length-x; char character=tolower(str[x]); int converted_char=single_char_convert(character); if(converted_char==-1) { std::cerr<<"\nInvalid Input,at "<< character <<" is an error\n"; return -1; } result+=(converted_char*pow(value,y)); } return result; } int single_char_convert(char a) { char index[]="0123456789abcdef"; for(int count=0;count<16;count++) { if(index[count]==a) { return count; } } std::cerr<<"Error Invalid Character Input"; return -1; } int pow(int number,int powerof) { if (powerof==0) { return 1; } else if(powerof<0) { std::cerr<<"ERROR ERROR Wrong Power "<< powerof <<" which is less than 0"; } else { return number*pow(number,--powerof); } }
To check whether a character contains a valid hexadecimal token (0-9, a-f) you could simply make use of the following
if-statement : if(('0'<=c && c<='9') || ('a'<=c && c<='f'))
Last edited by tux4life; May 1st, 2009 at 4:56 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
To check whether a character contains a valid hexadecimal token (0-9, a-f) you could also simply make use of the followingif-statement:if(('0'<=c && c<='9') || ('a'<=c && c<='f'))
So I guess I should write code to stop that from Happening.
Which way do you suggest.
You could make use of the following if-statements:
Edit:: 'c' is a character variable (
Just put them in a loop and evaluate each string character by character before doing the actual conversion
P.S.: Sorry if my explanation is bad, my native language is Dutch so my English vocabulary is a bit limited ...
- Hexadecimal:
if(('0'<=c && c<='9') || ('a'<=c && c<='f')) - Decimal:
if(('0'<=c && c<='9')) - Octal:
if(('0'<=c && c<='8'))
Edit:: 'c' is a character variable (
char ) of course 
Just put them in a loop and evaluate each string character by character before doing the actual conversion

P.S.: Sorry if my explanation is bad, my native language is Dutch so my English vocabulary is a bit limited ...
Last edited by tux4life; May 1st, 2009 at 4:56 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
You could make use of the following if-statements:
- Hexadecimal:
if(('0'<=c && c<='9') || ('a'<=c && c<='f'))- Decimal:
if(('0'<=c && c<='9'))- Octal:
if(('0'<=c && c<='8'))
New members chased away this month: 4
![]() |
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: 920 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






