C++/c++

Advertisement


Question
QUESTION: program:character recognizer

#include<iostream>
using namespace std;
int main()
{
   char x;
   cin>>x;
   
    if ((63<x<91)||(96<x<123))
   {
   cout<<"letter";
   
   switch (x)
   {
   case 33:case 34:case 39:case 44:case 46:case 58:case 59:case 63:case 96:
   cout<<"punctuation mark";
}
   
   else if (47<x<58)
   {
   cout<<"digit";
}
   
    else
   {
   cout<<"special character";
}
   
   system("PAUSE");  return 0;
}   

18 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\char recog.cpp expected primary-expression before "else"
18 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\char recog.cpp expected `;' before "else"
23 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\char recog.cpp expected primary-expression before "else"
23 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\char recog.cpp expected `;' before "else"
29 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\char recog.cpp expected `}' at end of input


what is wrong here,i cannot understand?

ANSWER: Hello

The first error at line 18 is telling you there is something wrong just before the "else" at line 18. Even if you don't know what a "primary-expression" is, you can look carefully and you will see that it is missing a closing brace before the "else".

Generally, you should fix the first error, and then recompile, because fixing the first error may remove other error messages.

There is also a problem with the way the comparisons are written. C/C++ does not let you write (63<x<91). Instead you must write (x > 63 && x < 91). In your program, you can compare x against characters instead of numbers. It will make the program easier to read. For example you can write:
if (( x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))
Notice you can use the equal sign in the comparisons.

The full program is below

#include<iostream>
using namespace std;
int main()
{
   char x;
   cin>>x;

   if (( x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))
   {
       cout<<"letter";

       switch (x)
       {
       case '!':case '"':
       case '\'': // Notice to specify quotes, you must escape them with the slash
       case ',':case '.':
       case ':':case ';':case '?':case '`':
         cout<<"punctuation mark";
       }
   }
   else if (x >= '0' && x <= '9')
   {
       cout<<"digit";
   }

   else
   {
       cout<<"special character";
   }

   system("PAUSE");  return 0;
}



---------- FOLLOW-UP ----------

QUESTION: thank you,all is working
but i m afraid the program confuses punctuation marks and special characters

Answer
Hello
There is a logic error in the program. In the program, the check for punctuation is done only when the input is a letter, and that makes no sense. The check for punctuation should be done when the input is not a letter and not a number. Then, if the input is not a punctuation character, it must be a special character. The corrected program is below

#include<iostream>
using namespace std;
int main()
{
   while(true)
   {
       char x;
       cin>>x;

       // check if it is a letter
       if (( x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))
       {
         cout<<"letter\n";
       }
       else if (x >= '0' && x <= '9') // if not a letter check if it is a digit
       {
         cout<<"digit\n";
       }
       else // if not a letter and not a digit
       {
         switch (x) // check for puctuation
         {
         case '!':case '"':
         case '\'': // Notice to specify quotes, you must escape them with the slash
         case ',':case '.':
         case ':':case ';':case '?':case '`':
         cout<<"punctuation mark\n";
         break;

         default: // if not punctuation, then it must be special.
         cout<<"special character\n";
         }
       }
   }

   system("PAUSE");  return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.