// checkNonAscii.cpp
//
// Checks whether an entered string
// contains at least one non-ASCII character.
#include <iostream>
#include <string>
using namespace std;
/*
* Prompts the user to enter a string.
* Displays an indication of whether the string
* contains any non-ASCII characters, or whether, on
* the other hand, it contains ASCII characters only.
*
* 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()
{
// Prompt user for a string, read it, and echo it:
cout << "Type something. Anything:>";
string text; // to be entered by the user
getline( cin, text ); // input entire line, including spaces
cout << "You typed: " << text << endl;
// Determine whether text contains a non-ASCII character:
bool nonAsciiFound = false;
for ( int i = 0; i < text.length() && !nonAsciiFound; i++ ) {
char x = text[i];
if ( x < 0 ) // i.e. if the leading bit is a 1
nonAsciiFound = true;
} // for i
// Output result:
cout << "Your line of text ";
if ( nonAsciiFound )
cout << "contains at least one non-ASCII character." << endl;
else
cout << "contains ASCII characters only." << endl;
return 0;
} // function main