I have 3 classes called Starter,Pizza and Dessert that takes variable number of string inputs whenever an object is created like,
//pizza takes 2 inputs
Pizza p("margarita","large size");
//starter takes 3 inputs
Starter s("ribs","bbq sauce","small size");
But I want to create a new object using a function add() that takes a string and matches it with the class to create a new object. Eg
add(string type)
{
if(type == "Pizza")
{
Pizza *p = new Pizza();
}
else if(type == "Starter ")
{
Starter *p = new Starter ();
}
}
Now my question is, how can I give the inputs to the classes in a user friendly way? By user friendly I was thinking that the user can write all the inputs of a class in one line and not using cin to take every single input.
Say we are getting Pizza, then what I dont want,
cout<<"What type of pizza";
cin>>*input* <<endl;
cout<<"What size";
cin>>*input* <<endl;
I want to write all the inputs in one line like,
input "margarita","large"
Credit is to @MuratKarakus. just extending his answer to support this type of input "margarita","large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
is >> size;
--------update
The code bellow is to support input like "1/2 margarita 1/2 bbq delux", "large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ' ', '-'); // this'll replace all space with '-'
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
std::replace( meal.begin(), meal.end(), '-', ' '); // this'll replace all '-' with space
is >> size;