Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,677 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,208 people online right now. Registration is fast and FREE... Join Now!




keyword problem

 
Reply to this topicStart new topic

keyword problem

naash
post 3 Sep, 2007 - 04:10 AM
Post #1


D.I.C Head

**
Joined: 25 Aug, 2007
Posts: 77


My Contributions


Q Write a function in C++ that takes a string type data argument and returns true if the string is a C++ keyword. The function returns a false value if the given string is not a C++ keyword .Do not use any predefined C/C++ header files except "iostream.h"

I did the following ..... tell me if its right. Also tell me an easier method if available.

CODE

int key_chk(char s[])
{
int flag=0;  
enum key={int=1,char,long,float,double,class,structure,private,union};
enum key i;
if(*s==(char)i)
  {
     flag=1;
     break;
  }
if(flag==0)
  {
     cout<<"Not a keyword";
     return 0;
  }
if(flag!=0)
  {
     cout<<" keyword";
     return 1;
  }
}

User is offlineProfile CardPM

Go to the top of the page

Bench
post 3 Sep, 2007 - 05:16 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


You can start to tell whether its right or not by at least trying to compile it.

If you want a string literal, put it in-between double quotes, eg, "int", "float", "double", etc.
enums are based on integral types, so I can't see how that would help you with in this excercise.

You might find that an array of strings will help here, that you can use as a lookup table
const char* keywords[] = { "int", "float", "double" /* ... etc ... */ };

This post has been edited by Bench: 3 Sep, 2007 - 05:22 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month