Reputation: 801
I am a c++ beginner. I am trying to grab some inputs from console. myconsole commmands are in "action parameter" formation
Base on the input "Request" "Create" and "Destroy" then my program will decide which function to process. i know that i can use cin >> variable to do this. i made some research and come out the follow
string action;
while(true){
cin >> action;
cout << action << endl;
}
Now the problem is i can only assign input to one variable "action", how can i assign input to another variable "parameter" and note the parameters maybe in string and integer datatype.
Thanks for help.
Upvotes: 0
Views: 7905
Reputation: 477710
It might be better to read entire lines, and split those later. String streams are useful for that purpose:
#include <string>
#include <sstream>
#include <iostream>
std::string line;
while (std::getline(std::cin, line))
{
std::istringstream iss(line);
std::string token;
while (iss >> token)
{
std::cout << "Word: " << token << "\n";
// ... process tokens; e.g. store in a container
}
}
Upvotes: 0
Reputation: 133
example:
string str;
int a;
double b;
char c;
cin >> str >> a >> b >> c;
Upvotes: 1
Reputation: 111
If you want to create a full text mode user interface, have a look at the CLI toolkit: http://alexis.royer.free.fr/CLI/. This toolkit makes it easy to define command line syntaxes (with sequences keywords and parameters, but also menus and other stuff) and plug them with the final routines that should be executed at end. The parsing is done by the library provided with the toolkit.
Upvotes: 0