Reputation: 15
I'm new to working on structures and came across this problem:
In Germany, the house number is displayed after the street name; for example, Bahnhofstraße 1. Change the print_address function from this section so that it can print addresses in either format.
#include <iostream> #include <string> using namespace std; struct StreetAddress { int house_number; string street_name; }; void print_address(StreetAddress address, bool number_first); int main() { StreetAddress white_house; white_house.house_number = 1600; white_house.street_name = "Pennsylvania Avenue"; print_address(white_house, true); cout << endl << "Expected: 1600 Pennsylvania Avenue" << endl; StreetAddress train_station; train_station.house_number = 1; train_station.street_name = "Bahnhofstraße"; print_address(train_station, false); cout << endl << "Expected: Bahnhofstraße 1" << endl; return 0; } void print_address(StreetAddress address, bool number_first) { //code here }
Currently in the code here
section, I have this:
void print_address(StreetAddress address, bool number_first)
{
StreetAddress s;
if(number_first == true)
cout << s.house_number << " " << s.street_name;
else if (number_first == false)
cout << s.street_name << " " << s.house_number;
}
I'm getting numbers instead of what I want. What's the problem here? I'm failing to see the issue.
Upvotes: 0
Views: 548
Reputation: 73061
Your print_address
function is declaring a local variable StreetAddress s
and leaving it uninitialized, and then trying to print out the uninitialized "data" it contains.
I think what you intended was to print out the fields in the address
argument, instead.
Upvotes: 3