I need help with a program i am writing. It takes in a line from the user and then decides wat to do. there are to many options to spesify. some of the options are:

    set TARGET 453.212.556.22 

This command will differ becuase the host will never be the same.

    set FUNCTION create:tcp

This function will also differ becuase there are allot of things that can be created like: reverse tcp connection, vnc session, webcam session.

    use NETWORK 10.0.0.2

That will be the defualt gateway, there is also going to be allot of them.

    >: download "filename" "destination"

This is going to download a file from the computer to the other computer.

See my problem isnt to write these functions. The problem is i cant seem to make my "input string" into keywords so that it can choose the write action. I cant really go and type:
set TARGET 0.0.0.1
All the way up to newest ip address, that would take ages and the program would be to big.

So i need to make my program read the firs few words:
set TARGET
and then get the next word:
10.0.0.0

One might say that i am trying to make a terminal (for linux users) or a cmd (for microsoft user).
it should act just like a cmd / terminal: getting the user input. deviding it into keywords so that it can choose wat to do.

You cant tell me that microsoft's or linux's terminal has each and every ipadress + any other possible name of a program or directory in its database. They must have used keywords.

Here is my code:

NOTE: this code istn my actual code, this is just to demonstrate what i am saying:

    #include <iostream>
    #include <someotherheaders>

    using namespace std;

    int vncsession();
    {
    //connects to other computer using vnc
    }

    int downloadfile(string filnamelocation, string filenamedest)
    {
    // downloads a file from target to destination
    }

    int main()
    {
        string input;

        cout<<">: ";

        getline(cin, input);

        if(line == "use FUNCTION ")
            vncsession();
        else if (line == "download ")
            downloadfile(keyword1, keyword2);
    }

So my question actually is: How am i going to make my program brake that line into keywords and then do what it must. I have tried using an enum before but it didnt work.

Recommended Answers

All 2 Replies

Well if you want to parse a string the best way i can think of is to you a stringstream. You get the input from the user using getline like you have and then you would put the string into the stringstream. After that you would output all of the differnt parts into som sort of contianer. After that you just go through the continaer and handle accordinginly. Here is a little sample of how to do that.

// to break a up string
#include <vector> // for vector
#include <sstream> // for stringstream
#include <iostream>
#include <string>

int main()
{
    std::stringstring ss;
    std::string input, temp;
    std::vector<string> comand;
    std::cout << ">: ";
    std::getline(cin, input);
    ss << input;
    // brak up line and put each part into the vector
    while( ss >> temp)
    {
        comand.push_back(temp);
    }
    // display vector
    for(size_t i = 0; i < comand.size(); i++)
        std::cout << comand[i] << std::endl;
    std::cin.get(); // pause program
    return 0;
}

See man, that is exactly what i needed, thanks so much for it. i have been looking all over the web for this and couldnt find anything. ill do it like that.

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.