Reputation: 25
I am making a bot for steam offers. And i need to get every character of a string in a variable, i tried to use vectors but it didn't work. From the string "abc" i got the vectors: 97 98 99. I don't understand what is wrong there..
int v[100], j=0; unsigned i;
string str; cin>>str;
for (i=0; i<str.length(); ++i)
{
v[i]=str.at(i);
cout<<str.at(i)<<endl;
cout<<v[i]<<endl;
}
I am new with all this thing, so, please help me a bit. What am i doing wrong here?
Upvotes: 1
Views: 73
Reputation: 466
that's because you declared v as an int, declare it as a char
char v[100];
int j=0;
unsigned i;
string str;
cin>>str;
for (i=0; i<str.length(); ++i)
{
v[i]=str.at(i);
cout<<str.at(i)<<endl;
cout<<v[i]<<endl;
}
Upvotes: 0
Reputation: 36597
97, 98, and 99 are the numeric values of characters 'a'
, 'b'
, and 'c'
in the ASCII character set. So converting them to an int
, which is what your code does in order to store their values into an array of int
, will give those values unless your host system operates with a non-compatible character set.
Upvotes: 4