I'm having a lot of trouble designing a switch statement that can take a string value I have to read from an input file and matching it up with an enum value using a switch statement so I can process the string using a function. Complicated, I know.

Here's the input example:
model<tab>Powerbook G4<tab>Apple<tab>1500
model<tab>Alienware 1000<tab>Alienware<tab>1860
update<tab>Inspiron 2500<tab>Dell<tab>2000

The program reads an input file full of computer models, brands, and prices and stores them into arrays. I did that, and it works. Now I have to read another input file, like the one above, which has commands that change the values in the arrays

model: find the model and print out the brand and price
update: find the model and update it's price to the new integer listed
range: find all models within price range
exit: stop reading the command file, print a message and stop program

Here's what I have (abbreviated version)

enum Commands { MODEL, UPDATE, RANGE, EXIT }
Commands dothis  // enum type for switch statement (really not sure if what to do here)
ifstream Process   // input file with commands
string Execute      // temporary string to hold the command to be executed

// Priming Read
getline( Process, Execute, '\t' )   // gets the command, but leaves remainder of line

 switch (dothis)
   case MODEL: void FindModel()  // reads rest of line and searches for model and reprints
   case UPDATE: void UpdatePrice() // reads model and new price, updates in array, prints
   case RANGE: void FindInRange() // finds all models in price range and prints
   case EXIT: void ExitCommand() // prints "exit command found" then stops reading file

getline( Process, Execute, '/t' ) // goes back for more

I won't know how many commands are in the file, but the exit command will always be last. I'm pretty sure that's how the switch statement should look, but I don't know how to take the string I read in and use it in the statement. Thanks in advance.

Recommended Answers

All 6 Replies

Start with std::map<std::string, Commands> table; Then add your translation information, like table["model"] = MODEL; Then you can have say

std::string command = "model";  // or as read from the file
switch ( table[command] ) {
  case MODEL: // and so on

Only problem is I can't use maps or any of that fancy jazz.

I think I might be able to use parallel arrays to store the strings and enums. How exactly would I go about that??

Only problem is I can't use maps or any of that fancy jazz.


> I can't use maps or any of that fancy jazz.
"can't" as in you haven't even tried to learn how to use them.
"can't" as in it is outside the scope of your homework assignment.

yea it's outside the scope of my assigment. Still trying to figure it all out

So write your own function which takes a string containing the model, then returns an enum.
A simple if / else if chain ought to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.