Единственная проблема состоит в том, что когда я спрашиваю пользователя, на какую комнату он хочет перейти, если он вводит пустой ввод, то должно выводиться сообщение «Вы должны выбрать комнату».
blockquote >Использование
std::getline
, а затем извлечение числа из строки с помощьюstd::istringstream
- лучшая стратегия для этого.std::string line; std::cout << "Choose an existing room. "; while ( std::getline(std::cin, line) ) { // Try to get the room_change using istringstream. std::istringstream str(line); if ( str >> room_change ) { // Successfully read the room. break; } // Problem reading room_change. // Try again. std::cout << "Choose an existing room. "; }
class Foo
{
public string Bar { get; set; }
}
class Program
{
static void Main()
{
Type type = typeof(Foo); // possibly from a string
IList list = (IList) Activator.CreateInstance(
typeof(List<>).MakeGenericType(type));
object obj = Activator.CreateInstance(type);
type.GetProperty("Bar").SetValue(obj, "abc", null);
list.Add(obj);
}
}
Вот пример взятия List< > введите и превращение его в List< string>.
список var = typeof (List< >).MakeGenericType (typeof (строка));