hello

i have a problem to solve for a commandline-programm:
there are several types of inputs to get and these can look like:
keyword1 <double> value1 <double> value2
keyword2 <double> value1
keyword3 <double> value1
...

I just can't figure out how to do it!

thanks!

see any syntax will be always in this way only . all u have to do is to read what type of arguments a particular function needs . suppose lets take ur example , u say : -
keyword1 <double> value1 <double> value2 this means this keyword "keyword1" will require a DOUBLE data type value i.e. <double> <<<<-- this is showing the data type u have to pass a value of this DOUBLE data-type only , and <double>value2 means again the second value is of DOUBLE data type ! got it ?

here is an example:
input:
add 3 4
output:
7

sqrt 9
3

store 4
stores value in some variable

so the first is the operater followed by a number of doubles to process.

This is an example to how to get the arguments from the command line:

#include <iostream>

int main ( int argc, char *argv[]  )
{
    std::cout << "Number of arguments: " << argc << std::endl;

    for ( short i = 0; i < argc; ++i )

        std::cout << argv[i] << std::endl;

    return 0;
}

You have all the arguments in the array, all you need to do is to parse it, and call the corresponding function depending on what was passed to the program.

Input:
./DevC++/cmdprg This is not a test (I compiled it under the name of 'cmdprg' )

Output:
Number of arguments: 6
./DevC++/cmdprg
This
is
not
a
test

Hope this helps.

This is an example to how to get the arguments from the command line:

#include <iostream>

int main ( int argc, char *argv[]  )
{
    std::cout << "Number of arguments: " << argc << std::endl;

    for ( short i = 0; i < argc; ++i )

        std::cout << argv[i] << std::endl;

    return 0;
}

You have all the arguments in the array, all you have to do is to parse it, and call the corresponding function depending on what was passed to the program.

Input:
./DevC++/cmdprg This is not a test

Output:
Number of arguments: 6
./DevC++/cmdprg
This
is
not
a
test
Hope this helps.

thanks, thats almost is what i need to do, but there should be one executable running and asking me for what i want to do.
That means, i start my program and type the sequence "keyword value value..." and than it should process the values like the user specified.
otherwise i would always have to insert the hole programm name and restart the programm.
is there possibility (im shure there is) to do so?

thanks

t

You could make a while loop and tie the execution to that. That way the user could constantly feed the program with newer instructions, until the escape keyword is given.

#include <iostream>
#include <string>

int main ( int argc, char *argv[]  )
{
    std::cout << "Not going to quit until you say 'exit'!\n";
    std::string word;

    do
    {
        std::cout << "#: ";
        std::cin >> word;
    }
    while ( word != "exit" );

    return 0;
}

Is this something you had in mind?

yes i thought about something like that.
the main problem is how to extract keyword and values from "word".

if (keyword=="add")
      {
        result=value1+value2;
      }
      if ....

the switching of the Keywords is not the big problem, but the getting of the values, since i dont know how many it will be.

Then stringstream is what you need: http://www.cplusplus.com/reference/iostream/stringstream/stringstream/

Also with the use of find , or by walking through the string, you can get the number of white spaces, which will tell you how many arguments were passed.

But be careful how much freedom give you to the user. It can really complicate things up for you. For example if you write a function in C++ called 'addition', you need to decide how many arguments will the function take, and what will it return type, right? But if you let the user, to give as many arguments as he wants, you need to figure out a way to handle arbitrary number of parameters.

For example the user input can be:
add 2 3 5 6 1 23 5 612312 3123

or
add 2.1 20.41 2 // these are mixed type, floating point and integer
add 0
0 12 add
multiply 2.4 1
multiply 0 0
divide 12.1 +0

Your program has to be 'smart' enough to know what to do, with the given line. The really tricky part is to validate each command, the rest once you've extracted the parameters will be easy.

so here is how i got it to work for 2 inputs:

string input, str;
double value1, value1;
if (input.find("add")!=string::npos) {
			str=input.substr(4, input.length());
			char * pEnd;
			value1 = strtod (str.c_str(),&pEnd);
			value2 = strtod (pEnd,NULL);
			cout << value 1 + value2;
		}

for more then 2 i should work with an array or so..

thanks for the help

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.