A program that prompts the user for keywords belonging to C++.

J

Jason Heyes

This is a revised version of a post entitled "Class to support keywords".
Please reply to this post instead of the old one.

The following program repeatedly prompts the user for C++ keywords until
'explicit' is entered. If the user fails to enter a valid keyword, the
program terminates.

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

The following two lines of input will terminate the program:

do for while explicit
if else elseif virtual instanceof

The input given by the string "public private switch" will not.

What would a class that supports the functionality of KeyWord in the above
program look like? What are the details of the implementation of such a
class? How many changes to the implementation would be required if another
set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion. Thanks.
 
C

Christian Meier

Jason Heyes said:
This is a revised version of a post entitled "Class to support keywords".
Please reply to this post instead of the old one.

The following program repeatedly prompts the user for C++ keywords until
'explicit' is entered. If the user fails to enter a valid keyword, the
program terminates.

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

The following two lines of input will terminate the program:

do for while explicit
if else elseif virtual instanceof

The input given by the string "public private switch" will not.

What would a class that supports the functionality of KeyWord in the above
program look like? What are the details of the implementation of such a
class? How many changes to the implementation would be required if another
set of keywords was to be used instead?

I would appreciate help in the form of code as well as discussion. Thanks.
You do not really need a class for that. The whole thing you need is a
container containing strings. The container has to be filled at startup.
Afterwards you can use a find function which tells you if the keyword is in
the container. If not, it is an invalid keyword...
I don't know if there is a class for this thing. But write your own one. I'm
sure there are many listings on the web the all reserved c++ keywords.

Greets Chris
 
T

Thomas Matthews

Jason said:
This is a revised version of a post entitled "Class to support keywords".
Please reply to this post instead of the old one.

The following program repeatedly prompts the user for C++ keywords until
'explicit' is entered. If the user fails to enter a valid keyword, the
program terminates.

#include <iostream>
#include "KeyWord.h"

int main()
{
KeyWord word;
while (std::cin >> word && word != "explicit");
return 0;
}

The following two lines of input will terminate the program:

do for while explicit
if else elseif virtual instanceof

The input given by the string "public private switch" will not.

What would a class that supports the functionality of KeyWord in the above
program look like?
Probably, something with a hardcoded table of strings. Could be
a hash table, a map or something else {from the Dragon Book}.
What are the details of the implementation of such a
class?
Depends on the design.
How many changes to the implementation would be required if another
set of keywords was to be used instead?
Only to the table.
I would appreciate help in the form of code as well as discussion. Thanks.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top