1

Currently writing program that shows info about cities.

I already programmed first phase. User inputs city name and it prints out its data. Thing is,after this was executed,program should ask a user for any additional info is needed. If user inputs Yes it will loop same message and same system as above ( it will again ask user to input city name and display that city data). If user inserts NO program will stop executing. I am not sure about approach to this. So what is the best approach doing this? How to write it properly to loop IF statement.

    #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;
    }

Using Visual Studio 2017.

AndreasDEV
  • 55
  • 1
  • 9
  • What is the question? Your best bet at this point are these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Feb 25 '18 at 12:55
  • What about moving the `std::string choice; do{` to the line after `CityData Ottawa = { "Ottawa", 1, 3 };` – Quimby Feb 25 '18 at 12:59
  • @Quimby just did it,see no difference. – AndreasDEV Feb 25 '18 at 13:02
  • You're trying to ask for a city name and print city information in a loop. But most of your code is not inside a loop. Just move the start of your do loop to near the beginning of the code and you won't be too far away – john Feb 25 '18 at 13:05
  • @john already did that amigo. Thing is i am not sure how to mix IF statements. I need 2 if statements, one for city names and second for choice Yes/No. Nest them? – AndreasDEV Feb 25 '18 at 13:09
  • Your logic for finding out the city to print the data can be moved out of the main function to a separate function. Also, take the input from the user in the loop. You have asked the user for the input city outside loop and another time you have asked user to input data but the code is not written for that. – vkrishna17 Feb 25 '18 at 13:12

1 Answers1

1
#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};
  // Declare choice outside of DO-WHILE statement
  std::string choice;
  do {
    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';

    std::cout << "Any additional info needed?" << '\n';
    getline(std::cin, choice);
  } while (choice ==
           "Yes"); // This acts as the second if statement in original code

  system("pause");
  return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
Quimby
  • 17,735
  • 4
  • 35
  • 55