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.