Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

C++
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;
	}

}
Posted
Updated 24-Oct-16 4:52am
v2
Comments
[no name] 24-Oct-16 10:51am    
Have a look at command line arguments here http://www.cprogramming.com/tutorial/lesson14.html
Richard MacCutchan 24-Oct-16 11:49am    
Why not just tell the user that multiple commands must be separated by some control character (semi-colon would be a good choice)? All your program then needs to do is split the input round those characters and process each substring as a command.

1 solution

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

You get argc == 3 and you can get
C++
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:

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;
}


The output:

argc = 3
argv[0] = x:\myprogram.exe
argv[1] = add 0324782012
argv[2] = set 0324782012 title Data Structures Using C++
 
Share this answer
 
v4
Comments
Leo Chapiro 24-Oct-16 10:57am    
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;
}
Leo Chapiro 24-Oct-16 11:04am    
I just wanted to show you the way how to "let user enter two command in one line", that's all. HTH

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900