Create command in C + +?

Feb 28, 2012 at 11:47pm
Hello I am new to the area of C + + programming, develop applications in console mode I would like to create a custom command resembling the cmd windows

for example to create a command called "help" that displays information:
1 - sum
2-subtract
and also put in the script eg if I do not type the correct command nehum returned as "invalid command!"

someone could pass a code sample?

compiler I use Dev-C + + 4.9.9.2

Feb 28, 2012 at 11:55pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::string line;
while(std::getline(cin, line))
{
    std::istringstream command (line); //I just had to do it...command line
    std::string cmd;
    command >> cmd;
    if(cmd == "help" || cmd == "?")
    {
        cout << "Commands:" << endl
             << "help - display this help" << endl
             << "doA - does thing A" << endl
             << "doB(int) - does thing B with an int" << endl;
    }
    else if(cmd == "doA") doA();
    else if(cmd == "doB")
    {
        int x;
        command >> x;
        doB(x);
    }
    else cout << "Invalid Command" << endl;
}
Feb 29, 2012 at 10:32pm
not able to put it I can put the file ready for dowload for example?
Mar 1, 2012 at 12:25am
What do you mean?
Mar 1, 2012 at 12:34am
a development with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
    system("TITLE centro de comando");
    cout<<"$";
 string line;
 string line2;
 string line3;
string command;
string argument;
string empty;
stringstream str_stream(line);

getline(cin, line);
//HELP
if(line == "HELP")
{
cout<<"HELPING.......OK!"<<endl;
main();
}
else
{
cout<<"ERROR!"<<endl;
main();
}
//infomações
if(line2 == "INFO")
{
cout<<"© Centro de Comando 2012 , desenvolvido por BRUNOPESCAROLLI"<<endl;
main();
}
else
{
cout<<"ERROR!"<<endl;
main();
}
//versão
if(line3 == "VER")
{
cout<<"VERSAO 2.0.0.1 beta!"<<endl;
main();
}
else
{
cout<<"ERROR!"<<endl;
main();
}

system("PAUSE > NULL");
  return 0;
}


but he 's with the following problem: he only just read the command' help ', and others who created the' VER ',' INFO 'can not read, as I do for him to read other commands?
Last edited on Mar 1, 2012 at 12:35am
Mar 1, 2012 at 12:42am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdlib> //why do you need this?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
    system("TITLE centro de comando"); //never do this, instead use: SetConsoleTitle("centro de comando");
    cout<<"$";
 string line;
 string line2; //why do you need this?
 string line3; //why do you need this?
string command;
string argument; //why do you need this now?
string empty; //why do you need this?
stringstream str_stream(line); //but...line is empty, so what is the point?

getline(cin, line); //so, you get input to line after making the string stream?
//HELP
if(line == "HELP") //this should be fine because you inputted to line and are comparing to line
{
cout<<"HELPING.......OK!"<<endl;
main(); //never do this
}
else //what if they entered one of the other commands?
{
cout<<"ERROR!"<<endl;
main(); //never do this
}
//infomações
if(line2 == "INFO") //but...the input is in "line", not "line2"
{
cout<<"© Centro de Comando 2012 , desenvolvido por BRUNOPESCAROLLI"<<endl;
main(); //never do this
}
else //what if they entered one of the other commands?
{
cout<<"ERROR!"<<endl;
main(); //never do this
}
//versão
if(line3 == "VER") //but...the input is in "line", not "line3"
{
cout<<"VERSAO 2.0.0.1 beta!"<<endl;
main(); //never do this
}
else //what if they entered one of the other commands?
{
cout<<"ERROR!"<<endl;
main(); //never do this
}

system("PAUSE > NULL"); //never do this, instead use: cin.ignore(unsigned(-1), '\n');
  return 0;
}
Topic archived. No new replies allowed.