I'm a bit lost. I am trying to take an input file and use the data inside as a command as well as the data. So my input file would look like this:

B 127.2 Hi 183.2 Someone 23.8 Lost

Where "B" would call the specific function it is attached to and input the data next to it. I have tried finding information about this but am only finding information about argc/argv. I'm not sure if there is a specific way of doing this or if I should just take the input of the first character, put it in a switch or an else if statement, and then input the data left on the line. Thanks!

Recommended Answers

All 6 Replies

First you need to make a list of commands and associated functions that are to process them. Then you have several options, probably the simplest is to use a switch statement for each command

char command = 'B';

switch(command)
{
   case 'A':
      // do something
      break;
    case 'B':
      // do something
      break;
    // etc. 
}

I can see that makes perfect sense. I guess I just get a bit lost with trying to input the data from one input file into seperate input variables. Like making the first letter the command variable and then being able to ignore that letter and take the rest of the line as the data variable.

If you have the entire line in a char array then just do this:

char line[255] = "B 127.2 Hi 183.2 Someone 23.8 Lost"


switch(line[0])
{

}

Perfect! Thank you so much.

All right, so i've run into another problem I did not think of last night. What if i'm trying to do this with multiple similiar lines of input like so.

B 127.2 Hi 183.2 Someone 23.8 Lost
H 172.1 Me 1827.2 goodbye 2918.2 game
G 1723.1 Soda 17.2 Hello 28.2 games

I was thinking if maybe I put the switch in a while statement and make the variable the getline of the input while not empty? I just want to be able to run one line at a time to function calls but be able to do multiple lines the same way after the first one completes.

while(!getline.eof){
    switch....
    }

Maybe like so?

somewhat

ifstream in("filename.ext");
std::string line;

while( getline(in,line) )
{
   switch(line[0])
   {

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