string command;
int main(){
cin >> command;
if (command == "exit"){
//code that exits the program
}
else{
cout << "Unknown command '"<<command<<"' try again";
cin >> command;
}
}
if the command entered is not exit the program asks for another command but when the next command is wrong the program closes how would i make it so that the programm keeps asking for valid comands?
std::string command;
while((std::cout << "Enter command: ") && std::getline(std::cin, command))
{
std::vector<std::string> v; //will hold the command name and its arguments
std::istringstream iss (command); //used for splitting by space
while(iss >> command) //keep looping until we run out of arguments
{
v.push_back(command); //add each part of the command to the vector
}
if(v.size() < 1) //make sure the command wasn't empty
{
continue;
}
elseif(v.front() == "exit")
{
break;
}
elseif(v.front() == "blah" && v.size() == 2)
{
std::cout << "Blah blah blah: " << v[1] << std::endl;
}
else
{
std::cout << "Unknown command." << std::endl;
}
}