// classifyChar1A.cpp
//
// Classifies the characters in an
// entered string of 1 to 15 characters,
// using logical operators.
// Uses interactive input.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
/*
* Outputs, to a text file classification.txt,
* a table classifying the characters in an
* entered string.
*
* The characters are classified as ASCII
* letters, ASCII digits, ASCII space,
* ASCII punctuation marks, ASCII control
* characters, or non-ASCII characters.
*
* Global variables:
* cin - an istream object for interactive input.
* cin is an external variable, declared
* in library header file <iostream>.
* cout - an ostream object for output to the terminal.
* cout is an external variable, declared
* in library header file <iostream>.
*/
int main()
{
cout << "Enter a string between 1 and 15 characters long:>";
string text; // to be entered by the user
getline( cin, text ); // input entire line, including spaces
cout << endl;
// Check whether string length is within range:
if ( text.length() == 0 || text.length() > 15 ) {
cout << "You entered a string of length "
<< text.length()
<< ", should be between 1 and 15." << endl;
return 1;
} // if
// Prepare to write to output file classification.txt:
ofstream outFile;
char* outputFilename = "classification.txt"; // C-string
outFile.open(outputFilename);
if ( !outFile ) {
cout << "Could not open file "
<< outputFilename << "." << endl;
return 1;
} // if
// Print table headings:
outFile << "Position Character Classification" << endl;
outFile << "-------- --------- --------------" << endl << endl;
// Print table body:
for ( int i = 0; i < text.length(); i++ ) {
// Display character's position in string text:
outFile << setw(5) << i;
// Display the character itself:
char x = text[i]; // character at position i in string text
outFile << " " << x << " ";
// Classify the character:
if ( x < 0 )
outFile << "non-ASCII character" << endl;
else if ( x == ' ' )
outFile << "ASCII space" << endl;
else if ( x >= '0' && x <= '9' )
outFile << "ASCII digit" << endl;
else if ( (x >= 'A' && x <= 'Z')
|| (x >= 'a' && x <= 'z') )
outFile << "ASCII letter" << endl;
else if ( x < 32 || x == 127 )
outFile << "ASCII control character" << endl;
else
outFile << "ASCII punctuation mark" << endl;
} // for i
cout << "File " << outputFilename
<< " has been created." << endl;
return 0;
} // function main