954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read string and use in switch statement

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:
modelPowerbook G4Apple1500
modelAlienware 1000Alienware1860
updateInspiron 2500Dell2000
exit

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
                    break;
   case UPDATE: void UpdatePrice() // reads model and new price, updates in array, prints
                    break;
   case RANGE: void FindInRange() // finds all models in price range and prints
                    break;
   case EXIT: void ExitCommand() // prints "exit command found" then stops reading file
                    break;

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.

dukedoc
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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??

dukedoc
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
Only problem is I can't use maps or any of that fancy jazz.

tutorial

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

dukedoc
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You