0

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.

1
  • 1
    Small sidenote: if(number_first == true) can be simplified to if(numer_first) and you can simplify the else if to an else without any conditions. Commented Sep 2, 2021 at 5:51

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

reference address.house_number and address.street_name in your cout lines, rather than s.house_number and s.street_name. (In fact, get rid of the StreetAddress s; line entirely, as you don't need it and its presence is what is allowing your program to compile and do the wrong thing, rather than giving you a helpful compile-time error)
Ah, I had thought that I would have to somehow initialize the structure in the function itself. Is using the address reference due to the structure being initialized in the parameters?
Yes; you could alternatively do StreetAddress s = address; then you would have two identical StreetAddress objects (address and s) and you could print data from either of them. There’s no benefit in this case to making a second copy, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.