I have an input file that has call to function and arguments that are passed to that function. There might be one argument or two. A simple while loop for input will not work ex-while(inputf >> function >> arg1 >> arg2) because if a function has only one argument then arg2 will read the function call for the next set of data. Any suggestions would be appreciated.

I have an input file that has call to function and arguments that are passed to that function. There might be one argument or two. A simple while loop for input will not work ex-while(inputf >> function >> arg1 >> arg2) because if a function has only one argument then arg2 will read the function call for the next set of data. Any suggestions would be appreciated.

I have an input file that has call to function

Not sure what you mean. Input files don't call functions.

arguments that are passed to that function.

Well, if the function can be passed one argument or two arguments, you'll need two functions, right? So have the function that is called when there are two arguments read in two pieces of data from the file. Have the function that is called when there is one argument read one piece of data from the file.

I'm a little puzzled, though, about the order here. What comes first? Reading the data from the file or the call to the function that takes one or two arguments? And the reading from the file gets done INSIDE of this function AFTER it is called? I'm not 100% clear on what the setup is here. You should probably post some code, even if it's incorrect, so we can get a better feel for it.

Sorry, I meant to say the first data element is a command. I have to read in the first word and based on that word, call an appropriate function that will perform some action. The next data are the parameters that have to be passed to that function. Here is an example input file:

strlen hammer
strappend doll house

So "strlen" will call the strlen function and get the length of "hammer".

I'm reading the input file using a while loop

while(inputf >> function >> arg1 >> arg2)

The problem is with the while statement. If my program were to process this,
function = 'strlen'
arg1 = 'hammer'
arg2 = 'strappend'

then
function = 'doll'
arg1 = 'house'

I have to stop input at the end of "hammer" and then go to the next line.

Sorry, I meant to say the first data element is a command. I have to read in the first word and based on that word, call an appropriate function that will perform some action. The next data are the parameters that have to be passed to that function. Here is an example input file:

strlen hammer
strappend doll house

So "strlen" will call the strlen function and get the length of "hammer".

I'm reading the input file using a while loop

while(inputf >> function >> arg1 >> arg2)

The problem is with the while statement. If my program were to process this,
function = 'strlen'
arg1 = 'hammer'
arg2 = 'strappend'

then
function = 'doll'
arg1 = 'house'

I have to stop input at the end of "hammer" and then go to the next line.

Read in the first piece of data from the file (the command), then based on that, either read in one or two arguments. There are many ways to do this. Here is one:

string command;
inputf >> command;

if (command == "strlen")
{
    // read in arg1
}
else if (command == "strappend")
{
    // read in arg1 and arg2
}
else if (...)
{
}
// etc.

But what if I need it to loop? I might have 1 to 20 commands that are possible. Would while(!inputf.eof()) work?

But what if I need it to loop? I might have 1 to 20 commands that are possible. Would while(!inputf.eof()) work?

No need to use eof if you didn't have to before. All you are doing is splitting this statement:

inputf >> function >> arg1 >> arg2;

into two pieces:

inputf >> function;
inputf >> arg1 >> arg2;

or

inputf >> function;
inputf >> arg1;

Put the top line:

inputf >> function;

in the while condition:

while (inputf >> function)

Put the other line inside the while loop.

Are you suggesting:

while (inputf >> function)
{
     inputf >> arg1 >> arg2;
     .
     .
     .
}

Isn't that going to produce the same results?

Are you suggesting:

while (inputf >> function)
{
     inputf >> arg1 >> arg2;
     .
     .
     .
}

Isn't that going to produce the same results?

That would, but see my earlier post and incorporate it:

while (inputf >> function)
{
    if (function == "strlen")
    {
       // read in arg1 
    }
    else if (function == "strappend")
    {
         // read in arg1 and arg2
    }
    else if (...)
    {
    }
    // etc.
}

I don't what I must have done, but I did that before my last post and it wouldn't work. I tried it again and works like a champ. Thanks for helping a daft newbie.

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.