使用do-while循环编写程序并使其询问用户选择是/否

时间:2018-02-25 12:53:43

标签: c++ while-loop

目前正在编写显示城市信息的程序。

我已经编制了第一阶段。用户输入城市名称并打印出其数据。在执行此操作之后,程序应该询问用户是否需要任何其他信息。如果用户输入是,它将循环相同的消息和上面相同的系统(它将再次要求用户输入城市名称并显示该城市数据)。如果用户插入NO程序将停止执行。 我不确定接近这一点。 那么这样做的最佳方法是什么?如何正确编写它以循环IF语句。

    #include <iostream>
    #include <string>

    struct CityData {
      std::string name;
      double population;
      short id;
    };

    void printCityData(CityData city) {
      std::cout << "City Name : " << city.name << std::endl;
      std::cout << "Population : " << city.population << " Milion" << std::endl;
      std::cout << "City ID : " << city.id << std::endl;
    }

    int main() {
      std::string NOP = "CityData AI"; // NOP stands for NAME OF PROGRAM
      std::cout << NOP << std::endl;

      // Create space between Name of program and user input
      std::cout << '\n';

      // CITYDATA FULL INFO
      CityData Toronto = {"Toronto", 2.1, 1};
      CityData Montreal = {"Montreal", 1.7, 2};
      CityData Ottawa = {"Ottawa", 1, 3};

      std::cout << "Enter city name or see the city list : " << std::endl;
      std::string cnl; // CN stands for City Name
      getline(std::cin, cnl);

      // Create space between user input and city info print
      std::cout << '\n';

      // City name choice - start of
      if (cnl == "Toronto" || cnl == "toronto" || cnl == "to") {
        printCityData(Toronto);
      } else if (cnl == "Montreal" || cnl == "montreal" || cnl == "mo") {
        printCityData(Montreal);
      } else if (cnl == "Ottawa" || cnl == "ottawa" || cnl == "ot") {
        printCityData(Ottawa);
      } else if (cnl == "city list" || cnl == "City List" || cnl == "City list") {
        std::cout << "Currently on list : Toronto, Montreal, Ottawa" << '\n';
      } else {
        std::cout << "City is not on the list!" << std::endl;
      }
      // City name If statement outro

      // Create space between data print and end of program
      std::cout << '\n';

      // Declare choice outside of DO-WHILE statement
      std::string choice;
      do {
        std::cout << "Any additional info needed?" << '\n';
        getline(std::cin, choice);
        if (choice == "Yes")
          std::cout << "Enter city name :" << std::endl;

      } while (choice != "No");

      system("pause");
      return 0;
    }

使用Visual Studio 2017。

1 个答案:

答案 0 :(得分:1)

interface DetectionParams {
    timeStamp: number;
    rectangle: number[];
};