// charsInString1.cpp
//
// Displays ASCII code values of characters
// in a string entered by the user.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
cout << "Enter a string between 1 and 15 characters long:>";
string word; // to be entered by the user
cin >> word; // input string without white space
cout << endl; // skip a line in output
// Check whether string length is within range:
if ( word.length() > 15 )
{
cout << "You entered a string of length "
<< word.length()
<< ", should be between 1 and 15." << endl;
return 1;
} // if
// Print table headings:
cout << "Position Character ASCII value" << endl;
cout << "-------- --------- -----------" << endl << endl;
// Print table body:
for ( int i = 0; i < word.length(); i++ )
{
// Display character's position in word:
cout << setw(5) << i;
// Display the character itself:
cout << " " << word[i];
// Display the character's ASCII value
cout << setw(14) << (int) word[i] << endl;
} // for i
return 0;
} // function main