943,701 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1649
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 1st, 2009
0

Alphabet to Integer conversion program== Segmentation Fault.

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 1st, 2009
0

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

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 1st, 2009
3

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

>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".
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 1st, 2009
0

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

Click to Expand / Collapse  Quote originally posted by tux4life ...
>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.

Quote 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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 1st, 2009
0

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

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 1st, 2009
3

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

>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:
C++ Syntax (Toggle Plain Text)
  1. const char digits[] = "0123456789ABCDEF";
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 1st, 2009
0

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

Narue, you're right but I provided him with this trick because his function was returning an integer value ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 1st, 2009
0

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

Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 1st, 2009
0

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

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
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 1st, 2009
2

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

Click to Expand / Collapse  Quote originally posted by tux4life ...
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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ init and read serial port status from loopback plug?
Next Thread in C++ Forum Timeline: Need help with c++ problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC