Alphabet to Integer conversion program== Segmentation Fault.

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

Join Date: Mar 2008
Posts: 672
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: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Alphabet to Integer conversion program== Segmentation Fault.

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

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int atoi(const char*);
  5. int single_char_convert(char);
  6. int pow(int,int);
  7. int main()
  8. {
  9. std::cout<<atoi("123");
  10. }
  11.  
  12. int atoi(const char *str)
  13. {
  14. int length=std::strlen(str)-1;
  15. int result=0;
  16. for(int x=0;x<=length;x++)
  17. {
  18. int y=length-x;
  19. char character=str[x];
  20. int converted_char=single_char_convert(character);
  21. result+=(converted_char*pow(10,y));
  22. }
  23. return result;
  24. }
  25.  
  26. int single_char_convert(char a)
  27. {
  28. switch(a)
  29. {
  30. case '0':
  31. return 0;
  32. case '1':
  33. return 1;
  34. case '2':
  35. return 2;
  36. case '3':
  37. return 3;
  38. case '4':
  39. return 4;
  40. case '5':
  41. return 5;
  42. case '6':
  43. return 6;
  44. case '7':
  45. return 7;
  46. case '8':
  47. return 8;
  48. case '9':
  49. return 9;
  50. }
  51. }
  52.  
  53. int pow(int number,int powerof)
  54. {
  55. if(powerof==1)
  56. {
  57. return number;
  58. }
  59. else
  60. {
  61. return number*pow(number,--powerof);
  62. }
  63. }



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
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,968
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 offline Offline
Posting Virtuoso

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

 
0
  #2
May 1st, 2009
>I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.

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."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
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: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #3
May 1st, 2009
>I am getting a segmentation fault when i run the follwing program.
How does your code handle N^0?

>Code tags work sometimes and code tags donot work.
Define "do not work".
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
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: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

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

 
0
  #4
May 1st, 2009
Originally Posted by tux4life View Post
>I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.

To do conversions stringstreams are often used
Well True,
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
Will a-'0';
be true for all the constants.in different character map sets?


Narue thanks for the reply

Here is the new code

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int atoi(const char*);
  5. int single_char_convert(char);
  6. int pow(int,int);
  7. int main()
  8. {
  9. std::cout<<atoi("123");
  10. }
  11.  
  12. int atoi(const char *str)
  13. {
  14. int length=std::strlen(str)-1;
  15. int result=0;
  16. for(int x=0;x<=length;x++)
  17. {
  18. int y=length-x;
  19. char character=str[x];
  20. int converted_char=single_char_convert(character);
  21. result+=(converted_char*pow(10,y));
  22. }
  23. return result;
  24. }
  25.  
  26. int single_char_convert(char a)
  27. {
  28. switch(a)
  29. {
  30. case '0':
  31. return 0;
  32. case '1':
  33. return 1;
  34. case '2':
  35. return 2;
  36. case '3':
  37. return 3;
  38. case '4':
  39. return 4;
  40. case '5':
  41. return 5;
  42. case '6':
  43. return 6;
  44. case '7':
  45. return 7;
  46. case '8':
  47. return 8;
  48. case '9':
  49. return 9;
  50. }
  51. }
  52.  
  53. int pow(int number,int powerof)
  54. {
  55. if (powerof==0)
  56. {
  57. return 1;
  58. }
  59. else
  60. {
  61. return number*pow(number,--powerof);
  62. }
  63. }
Last edited by Sky Diploma; May 1st, 2009 at 1:42 pm.
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,968
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 offline Offline
Posting Virtuoso

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

 
0
  #5
May 1st, 2009
Originally Posted by Sky Diploma View Post
Well True,
I pretty much dont know whether I can use stringstreams to solve this exercise.
You can: if you convert the C++ string back to a C-string after the conversion, but that was probably not the intention of the assignment
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."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
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: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #6
May 1st, 2009
>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:
  1. const char digits[] = "0123456789ABCDEF";
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
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 offline Offline
Posting Virtuoso

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

 
0
  #7
May 1st, 2009
Narue, you're right but I provided him with this trick because his function was returning an integer value ...
"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: 672
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: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

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

 
0
  #8
May 1st, 2009
Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
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,968
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 offline Offline
Posting Virtuoso

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

 
0
  #9
May 1st, 2009
Originally Posted by Sky Diploma View Post
Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
Take a look at this
"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,566
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: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #10
May 1st, 2009
Originally Posted by tux4life View Post
Narue, you're right but I provided him with this trick because his function was returning an integer value ...
And that makes a difference, how?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC