PDA

View Full Version : strings



zaac
July 15th, 2009, 08:49 AM
hey,
ive been stuck on this for a while, it should be easy,but i just cant get it.

what im trying to do is take a single digit from a string of digits and convert it into an int. heres my code so far



#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
string str = "73167176";
int i = 4;
int num1;
istringstream buffer(&str[i]);
buffer >> num1;
cout << num1 << endl;
}


the problem with this is that i get the number 7176 instead of 7.

if i pass in str[i] instead of &str[i] i get en error "invalid conversion from ‘char’ to ‘std::_Ios_Openmode’"

how do i fix this problem?, or point me in the right direction

thanks, zac

abhilashm86
July 15th, 2009, 09:04 AM
you need to output str instead of num1.............
check this


#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
string str = "73167176";
int i = 4;
int num1;
istringstream buffer(&str[i]);
buffer >> num1;
cout << str[i] << endl;
}

zaac
July 15th, 2009, 09:14 AM
but str is a string and i need an int

abhilashm86
July 15th, 2009, 09:31 AM
but str is a string and i need an int

here you are reading whole string from value pointed by integer i, so read just a single character, then you'll able to print intermediate values.........

bhagabhi
July 15th, 2009, 09:59 AM
change your line


istringstream buffer(&str[i]);


to


istringstream buffer(str.substr(4,1));

zaac
July 15th, 2009, 10:03 AM
here you are reading whole string from value pointed by integer i, so read just a single character, then you'll able to print intermediate values.........

im trying to read a single character and convert it to an int, but when i but it into the buffer it automatically takes multiple characters.

i need it as an int so it can be used in calculations, printing it is just to test to make sure it only takes one character

zac

zaac
July 15th, 2009, 10:24 AM
thanks bhagabhi

MindSz
July 15th, 2009, 05:06 PM
You can also get a character and type-cast it as an int and subtract 0x30 from it. That should give you the integer value.

Here's a simple program:


#include <stdio.h>
int main(){

char c = '7';
int n = (int) c;

n -= 0x30;
printf("%d\n", n);
return 0;
}

c0mput3r_n3rD
July 15th, 2009, 05:39 PM
You can also get a character and type-cast it as an int and subtract 0x30 from it. That should give you the integer value.

Here's a simple program:


#include <stdio.h>
int main(){

char c = '7';
int n = (int) c;

n -= 0x30;
printf("%d\n", n);
return 0;
}

+1
I find type casting to be much easier, and more readable.
You did it in c though and he's doing c++, it ends up being the same either way though.