Alphabet to Integer conversion program== Segmentation Fault.
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
#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.
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
>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".
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
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
#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);
}
}
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
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 :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
>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:
const char digits[] = "0123456789ABCDEF";
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Narue, you're right but I provided him with this trick because his function was returning an integer value ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Hey can you explain me of converting a string of char containing a hex notation of a number to a int?
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
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 :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>And that makes a difference, how?
I'm sorry but I don't get that question :(
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
#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);
}
}
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
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')) :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
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 :P
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 ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
isxdigit and isdigit should be preferred. There's no isodigit, so your suggestions aren't completely pointless. :icon_rolleyes:
Thanks for mentioning, I didn't know that :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243