如何让用户在cmd上的一行中输入两个命令? [英] How do I let the user enter two commands in one line on the cmd ?

查看:278
本文介绍了如何让用户在cmd上的一行中输入两个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在尝试编写一个代码,让用户在cmd屏幕上输入一些命令。

该项目是关于添加数据的节点到链表。数据由用户输入add输入,后跟一个数字,但必须使用set - 节点的编号 - 变量的名称 - 其值输入节点的其他信息,



我试过了,但我不能让他们像这样在一行:

>添加0324782012

(创建一个数字= 0324782012的新节点)

>使用C ++设置0324782012标题数据结构

(将节点0324782012的标题设置为Data Structur

es使用C ++)。



Node有一个int号和一个字符串标题。



你可以帮忙吗?



我尝试过:



  int  main(){
Node * node = NULL;
string line,line2;
while (getline(cin,line)){

istringstream command(line);
string cmd;
命令>> CMD;

if (cmd == add
{
int x;
命令>> X;
add(& node,x);
cout<< 编号为<<< node->的节点已添加数字<< << endl;
}
其他 如果(cmd == set
{
Node * p = node;
int x;
命令>> X;
while (p!= NULL){

if ( p-> number == x){

while (getline(cin,line2)){
istringstream command_in(line2 );
string cmd_in;
command_in>> cmd_in;

if (cmd_in == title){
string y;
command_in>> y;
title(& p,y);
break ;
}
} break ;}
p = p-> next;

}
else cout<< 无效命令<< ENDL;
}

}

解决方案

这很简单:你的电话< yourprogram> 添加0324782012设置0324782012标题数据结构使用C ++



你得到argc == 3你可以得到

 argv [ 0 ],其中  yourprogram  
argv [ 1 ],其中 添加0324782012
argv [ 2 ],其中 set 0324782012 title使用C ++的数据结构





分隔符是空白而你需要注意正确的报价。



要了解如何获取参数,你可以使用这个简单的例子:



  int  main( int  argc, char  * argv [])
{
cout<< argc =<< argc<< ENDL;

for int i = 0 ; i< argc; i ++)
cout<< argv [<< i<< ] =<< argv [i]<< ENDL;

return 0 ;
}





输出:



 argc = 3 
argv [0] = x:\ myprogram.exe
argv [1] =添加0324782012
argv [2] =设置0324782012标题数据结构使用C ++


Hi,
I've been trying to write a code that lets the user enter some commands on the cmd screen.
The project is about adding nodes with data to a linked list . the data is entered by the user typin' "add" and followed by a number, but the other info of the node must be entered by using "set - the number of the node - name of the variable - its value ",

I've tried, but i couldn't let them be in one line like this:
> add 0324782012
(create a new node with number = 0324782012)
> set 0324782012 title Data Structures Using C++
(set the title of node 0324782012 to "Data Structur
es Using C++") .

The Node has an int number and a string title .

could you please help ?

What I have tried:

int main(){
	Node* node =NULL;	 
	string line,line2;
	while(getline(cin, line)){	

		istringstream command (line);
		string cmd;
		command >> cmd;

			if(cmd == "add")
			{
				int x;
				command >> x;
				add(&node,x);
				cout<<"The node with number "<<node->number<<" has been added."<<endl;
			}
			else if(cmd == "set")
			{
				Node* p=node;
				int x;
				command >> x;
				while (p!=NULL){
					
					if (p->number==x){

						while ( getline(cin, line2) ){
							istringstream command_in (line2);
							string cmd_in;
							command_in>>cmd_in;

						if(cmd_in == "title"){
							string y;
							command_in  >> y ;
							title(&p,y);
							break;
						}
                            }break;}
                p=p->next;
				
				}
else cout << "Invalid Command" << endl;
	}

}

解决方案

This is simple: your call <yourprogram> "add 0324782012" "set 0324782012 title Data Structures Using C++"

You get argc == 3 and you can get

argv[0] with "yourprogram" 
argv[1] with "add 0324782012" 
argv[2] with "set 0324782012 title Data Structures Using C++"



The separator is the blank and you need to take care of correct quoting.

To see how to get the parameter you can use this simple example:

int main(int argc, char* argv[]) 
{
	cout << "argc = " << argc << endl;

	for (int i = 0; i < argc; i++)
		cout << "argv[" << i << "] = " << argv[i] << endl;

	return 0;
}



The output:

argc = 3
argv[0] = x:\myprogram.exe
argv[1] = add 0324782012
argv[2] = set 0324782012 title Data Structures Using C++


这篇关于如何让用户在cmd上的一行中输入两个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆