It must either happen in the 'for loop', or in the final cout statement, but where? Im not seeing the actual code that does the conversion.
string str; cout<<"Enter string : "; cin>> str; for(int a = 0; a!=str.length(); ++a) cout<<int(str[a])<<", ";
string str; cout<<"Enter string : "; cin>> str; for(int a = 0; a!=str.length(); ++a) cout<<int(str[a])<<", ";
|
|
|
std::cout << (char)'a'; //01100001 = 'a' in memory std::cout << (int)'a'; //01100001 -> 97 in decimalOne will output the ASCII character 'a', the other the ASCII codepoint, as you are forcing implicit conversion to an int. Your code uses casting with a function, int('a').
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str[20];
int result[20] = {0};
cin.getline(str, 20);
for(unsigned int a = 0; a != strlen(str); a++)
result[a] = (int)str[a]; //assign to array
for(unsigned int a = 0; a != strlen(str); a++)
cout << result[a] << ", "; //print array
}
int array[] = {'s', 't', 'r', 'i', 'n', 'g'};
If you output this array you will get ASCII numbers for corresponding letters. Is that what you meant?
#define class struct // All is public.
int intArray[] = {'s', 't', 'r', 'i', 'n', 'g'};
for (int i = 0; i < 5; ++i)
std::cout << (char)intArray[i]; // this will output letters instead of numbers, because we casted them to char
#define class struct // All is public.
0 members, 1 guests, 0 anonymous users