// checkNonAscii4B.cpp
// Tests whether an entered sequence of characters
// contains any non-ASCII characters.
// Reads up to the first whitespace character,
// imitating cin's extraction operator for C-strings.
// Uses a combination of sentinel-controlled
// and flag-controlled repetition.
// Uses a return statement to avoid
// continuing the search after a
// non-ASCII character has been found.
#include <iostream>
using namespace std;
int main()
{
// Announce purpose of program:
cout << "Enter a sequence of characters, "
<< "without white space.";
cout << "This program will tell you whether it "
<< "contains any non-ASCII characters." << endl;
// Ask user for a string, and echo it:
cout << "Enter your string:> ";
// Read and ignore leading whitespace, until
// non-whitespace is reached.
char typed;
cin.get(typed); // Read FIRST character.
while ( typed == ' ' || typed == '\t'
|| typed == '\r' || typed == '\n' )
{
cin.get(typed); // Read NEXT character.
} // while
// Read and echo non-whitespace characters,
// searching for a non-ASCII character, until
// whitespace is reached.
cout << "You typed: "; // Announce echoed input.
while ( typed != ' ' && typed != '\t'
&& typed != '\r' && typed != '\n' )
{
cout << typed; // Output current character.
if ( typed < 0 ) {
cout << endl << "Your string contains at least one "
<< "non-ASCII character." << endl;
return 0;
} // if
cin.get(typed); // Read NEXT character.
} // while
cout << endl;
// If this point has been reached, the string
// does not contain any non-ASCII characters.
// Output result:
cout << "Your string does not contain any "
<< "non-ASCII characters." << endl;
return 0;
} // function main