Alphabet to Integer conversion program== Segmentation Fault.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #11
May 1st, 2009
>And that makes a difference, how?
I'm sorry but I don't get that question
Last edited by tux4life; May 1st, 2009 at 2:15 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #12
May 1st, 2009
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.

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int atoi(const char*);
  5.  
  6. // Definitions of Required Functions
  7. int single_char_convert(char);
  8. int pow(int,int);
  9. int convert(const char*,int,int);
  10.  
  11. // DEFINITOINS END
  12. int main()
  13. {
  14. std::cout<<atoi("123")<<" DECIMAL \n";
  15. std::cout<<atoi("0112")<< " Octal == 74\n";
  16. std::cout<<atoi("0x1128")<<" HEXADECIMAL == 4392\n";
  17.  
  18. }
  19.  
  20.  
  21. int atoi(const char *str)
  22. {
  23. if (str[0]=='0'&&(std::tolower(str[1])=='x'))
  24. {
  25. return convert(str,16,2); //Hexadecimal
  26. }
  27. else if(str[0]=='0')
  28. {
  29. return convert (str,8,1); //Octal
  30. }
  31. else{
  32. return convert(str,10,0); //Decimal
  33. }
  34. }
  35.  
  36. int convert(const char *str,int value,int start)
  37. {
  38. int length=std::strlen(str)-1;
  39. int result=0;
  40. for(int x=start;x<=length;x++)
  41. {
  42. int y=length-x;
  43. char character=tolower(str[x]);
  44. int converted_char=single_char_convert(character);
  45. if(converted_char==-1)
  46. {
  47. std::cerr<<"\nInvalid Input,at "<< character <<" is an error\n";
  48. return -1;
  49. }
  50. result+=(converted_char*pow(value,y));
  51. }
  52. return result;
  53. }
  54.  
  55.  
  56. int single_char_convert(char a)
  57. {
  58. char index[]="0123456789abcdef";
  59. for(int count=0;count<16;count++)
  60. {
  61. if(index[count]==a)
  62. {
  63. return count;
  64. }
  65. }
  66. std::cerr<<"Error Invalid Character Input";
  67. return -1;
  68. }
  69.  
  70. int pow(int number,int powerof)
  71. {
  72.  
  73. if (powerof==0)
  74. {
  75. return 1;
  76. }
  77. else if(powerof<0)
  78. {
  79. std::cerr<<"ERROR ERROR Wrong Power "<< powerof <<" which is less than 0";
  80. }
  81. else
  82. {
  83. return number*pow(number,--powerof);
  84. }
  85. }
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #13
May 1st, 2009
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #14
May 1st, 2009
Originally Posted by tux4life View Post
To check whether a character contains a valid hexadecimal token (0-9, a-f) you could also simply make use of the following if-statement : if(('0'<=c && c<='9') || ('a'<=c && c<='f'))
This reminds me that the characters a-f arent allowed to be used in the OCTAL AND DECIMAL SYSTEMS.
So I guess I should write code to stop that from Happening.

Which way do you suggest.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #15
May 1st, 2009
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'))

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."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,855
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
2
  #16
May 1st, 2009
Originally Posted by tux4life View Post
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'))
isxdigit and isdigit should be preferred. There's no isodigit, so your suggestions aren't completely pointless.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: Alphabet to Integer conversion program== Segmentation Fault.

 
0
  #17
May 1st, 2009
Originally Posted by Narue View Post
isxdigit and isdigit should be preferred. There's no isodigit, so your suggestions aren't completely pointless.
Thanks for mentioning, I didn't know that
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 920 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC