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])<<", ";
WendellHarper - Dec 06 2020 01:21 PM
WendellHarper - Dec 06 2020 01:14 PM
pindo - Jul 23 2020 01:33 AM
Siten0308 - Jun 20 2019 01:43 PM
johnnylo - Apr 23 2019 07:49 AM
Posted 20 September 2010 - 06:43 PM
string str; cout<<"Enter string : "; cin>> str; for(int a = 0; a!=str.length(); ++a) cout<<int(str[a])<<", ";
Posted 20 September 2010 - 08:10 PM
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').
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
Posted 20 September 2010 - 10:12 PM
Posted 21 September 2010 - 02:03 AM
Posted 21 September 2010 - 03:25 AM
#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 }
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
Posted 21 September 2010 - 03:27 AM
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?
The roots of education are bitter, but the fruit is sweet.
Posted 21 September 2010 - 03:43 AM
Posted 21 September 2010 - 04:09 AM
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
The roots of education are bitter, but the fruit is sweet.
Posted 21 September 2010 - 02:04 PM
Language Forums →
C and C++ →
Why is similar string comparison giving different result?Started by nick112, 29 May 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
converting string to floatStarted by JonElias, 14 May 2016 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
How to find location of all the times a specific letter appears in stringStarted by 4ringsa6, 02 Mar 2016 ![]() |
|
![]() |
||
Language Forums →
Java →
How do I remove a character from a String?Started by dreddooo, 01 Oct 2015 ![]() |
|
![]() |
||
Tutorial Forums →
Other Programming Tutorials →
Python Tutorials →
Python ascii art for banners in command line programsStarted by timesleep8, 15 Sep 2015 ![]() |
|
![]() |