Just wondering what people think about the code below. It could be improved with a few pointers, was this way the best way to solve the problem? Is my coding sytle ok? Thanks in advace. The original question was:
The question does not ask to validate input, and was not coded to do so.Quote:
Write a program that asks the user to enter a character. The program will classify the character if it has any notable characteristics and it will output what those characteristics are. For example, entering a ‘w’ will output a message “w is a lower case character” but entering ‘W’ will produce the message “W is an upper case character”. If you enter a number (say 2) the message will be “2 is a number”. If you enter ‘a’ the message will be “a is a lower case letter and is a vowel” whereas ‘A’ will produce “A is an upper case character and a vowel”. If you enter a punctuation mark such as a . , “ ‘ : ; etc., the message will indicate that you have entered a punctuation character. E.g. if you enter ‘:’ the message is “: is a punctuation character”. If you enter an arithmetic operator such as ‘+’, the output will be “+ is an arithmetic operator”. If the character is any other non-characteristic value, output nothing.
To enable your application to keep running and allowing you to continually enter characters, read up on the do while loop. In this case, the application will run until you press the escape key then return.
Code:#include <iostream.h>
#include <conio.h>
enum MessageIds {UPR_CHAR, LWR_CHAR, NUM, VOWEL_LWR, VOWEL_UPR, PUNCT, ARITH};
void Messages(MessageIds, int, char);
bool StringSearch(const char String[], char Temp, int &Pos);
int main(void)
{
const int START_ALPHA_UPR = 65, END_ALPHA_UPR = 90, START_ALPHA_LWR = 97,
END_ALPHA_LWR = 122, NUM_LWR = 48, NUM_UPR = 57,
VOWEL_LWR_POS = 4, ESC = 27;
const char VOWELS[] = "aeiouAEIOU\0", PUNCTUATION[] = ".,?';:!\0",
ARITHMETIC[] = "-+*/\0";
MessageIds MsgIds;
char Character;
int AsciiVal, Index;
do
{
clrscr();
cout << "Please enter a character (Esc to quit): ";
cin >> Character;
AsciiVal = Character;
if(StringSearch(VOWELS, AsciiVal, Index) && Index <= VOWEL_LWR_POS)
Messages(MsgIds, VOWEL_LWR, AsciiVal); //needs to be first case
else
{
if(Index != 10)
Messages(MsgIds, VOWEL_UPR, AsciiVal);
else
{
if(AsciiVal >= START_ALPHA_UPR && AsciiVal <= END_ALPHA_UPR)
Messages(MsgIds, UPR_CHAR, AsciiVal);
else
{
if(AsciiVal >= START_ALPHA_LWR && AsciiVal <= END_ALPHA_LWR)
Messages(MsgIds, LWR_CHAR, AsciiVal);
else
{
if(AsciiVal >= NUM_LWR && AsciiVal <= NUM_UPR)
Messages(MsgIds, NUM, AsciiVal);
else
{
if(StringSearch(PUNCTUATION, AsciiVal, Index))
Messages(MsgIds, PUNCT, AsciiVal);
else
{
if(StringSearch(ARITHMETIC, AsciiVal, Index))
Messages(MsgIds, ARITH, AsciiVal);
}
}
}
}
}
}
}while(AsciiVal != ESC);
cout << "\nTada........";
getchar();
return 0;
}
void Messages(MessageIds MsgIds, int MsgCode, char Character)
{
switch(MsgCode)
{
case UPR_CHAR:
cout << "\n" << Character << " is a upper case character.";
getchar();
break;
case LWR_CHAR:
cout << "\n" << Character << " is a lower case character.";
getchar();
break;
case NUM:
cout << "\n" << Character << " is a number.";
getchar();
break;
case VOWEL_LWR:
cout << "\n" << Character << " is a lower case letter and a vowel.";
getchar();
break;
case VOWEL_UPR:
cout << "\n" << Character << " is a upper case letter and a vowel.";
getchar();
break;
case PUNCT:
cout << "\n" << Character << " is a punctuation character.";
getchar();
break;
case ARITH:
cout << "\n" << Character << " is a arithmetic operator.";
getchar();
break;
}
}
bool StringSearch(const char String[], char Temp, int &Pos)
{
bool MatchFound = false;
int i = 0;
while(String[i] != '\0' && !MatchFound)
{
if(String[i] == Temp)
{
MatchFound = true;
Pos = i;
}
i++;
}
Pos = i;
return MatchFound;
}