This is what
function arguments are for.
You need to pass the answer to the cities() function as an argument and let it compare it for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
using namespace std;
void cities(string answer){
string city1,city2,city3,city4,city5;
city1 ="London";
city2 = "Las Vegas";
city3 = "New York";
city4 = "Berlin";
city5 = "Paris";
if (answer == city1)
cout << "The city was " << city1 << "!" << endl;
else if (answer == city2)
cout << "The city was " << city2 << "!" << endl;
else if (answer == city3)
cout << "The city was " << city3 << "!" << endl;
else if (answer == city4)
cout << "The city was " << city4 << "!" << endl;
else if (answer == city5)
cout << "The city was " << city5 << "!" << endl;
else
cout << "What city is that?" << endl;
}
string question(){
string answer;
cout << "What is your favorite City?" << endl;
getline(cin, answer, '\n');
return answer;
}
int main() {
string answer = question();
// compare answer string here to list in cities function?
cities(answer);
return 0;
}
|
The problem with this is that there are now many similar if's doing the same thing!
This is where
arrays and
loops come to rescue!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <string>
using namespace std;
void cities(string answer){
int citycount = 5;
string city[citycount] = {"London", "Las Vegas", "New York", "Berlin", "Paris"};
bool foundcity = false;
for (int i=0; !foundcity && i<citycount; ++i) {
if (answer == city[i]) {
cout << "The city was " << city[i] << "!" << endl;
foundcity = true;
}
}
if (!foundcity)
cout << "What city is that?" << endl;
}
string question(){
string answer;
cout << "What is your favorite city?" << endl;
getline(cin, answer, '\n');
return answer;
}
int main() {
string answer = question();
// compare answer string here to list in cities function?
cities(answer);
return 0;
}
|
A drastic improvement. If we now added more cities we would just edit the citycount and the city array and that's it. In the previous case, we would've needed to add more and more if statements!
To make it more C++-like though, there are a few more things you need to consider.
First, when you pass strings around (e.g. cities() receiving the answer) they get
copied in their entirety! This is horribly inefficient and mitigate this we're going to use
references (&)!
But now that we're using references, we need to make sure not to change the arguments we pass since we might damage the original string! This means we need to put
const all over the place (well, at least at places we don't expect to change anything.)
Finally, in many tutorials you're going to find using namespace std; but this is actually not a good practice at all! That's why we're going to prefix cout, endl, getline, etc with
std::
Finally, we get this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <string>
void cities(const std::string& answer){
const int citycount = 5;
const std::string city[citycount] = {"London", "Las Vegas", "New York", "Berlin", "Paris"};
bool foundcity = false;
for (int i=0; !foundcity && i<citycount; ++i) {
if (answer == city[i]) {
std::cout << "The city was " << city[i] << "!" << std::endl;
foundcity = true;
}
}
if (!foundcity)
std::cout << "What city is that?" << std::endl;
}
std::string question(){
std::string answer;
std::cout << "What is your favorite city?" << std::endl;
std::getline(std::cin, answer, '\n');
return answer; //no worries about references here since the answer will get reused in main().
}
int main() {
const std::string answer = question();
// compare answer string here to list in cities function?
cities(answer);
return 0;
}
|
Which is how an average C++ code looks like.