I am working on a struct. The two requirements are for the user to be able to input an entry and then view it. I have two questions, when entering the data shouldn't I be able to enter spaces when entering the address i.e. 654 smith st and store that as address1? and why am i getting a memory addres for my print function?
it displays

,
-654649840

#include <string>
#include <iostream>

using namespace std;

struct playerType
{
    string first;
    string last;
    string address1;
    string address2;
    string city;
    string state;
    string zip;
    int jersey;
}; //end struct

void readIn(playerType player)
{
    cin >> player.first >> player.last;
    cin    >> player.address1 >> player.address2;
    cin    >> player.city >> player.state;
    cin    >> player.zip >> player.jersey;
}

void printPlayer(playerType player)
{
    cout << endl << endl << player.first << " " << player.last 
        << endl << player.address1 << endl << player.address2
        << endl << player.city << ", " << player.state
        << " " << player.zip << endl << player.jersey << endl;
}


int main()
{
    playerType player;

    cout << "Enter all of the player's information in order shown\n"
        "pressing Enter after each entry. First name, last name,\n"
        "address line 1, address line 2, city, state, zip, and jersey number: ";

    readIn(player);
    printPlayer();

    
} //end main

Dani AI

Generated

Two separate issues in the posted code explain both symptoms you saw. The extraction operator (>>) stops at whitespace, so an address like 654 smith st will be split across multiple fields. Also, readIn takes the struct by value, so it fills a copy and the player in main stays uninitialized — printing uninitialized fields produces garbage (undefined behavior). Finally, the posted main calls printPlayer() with no argument; that will not compile — the call must pass the player object.

A reliable pattern is to read whole lines for text fields and convert the jersey number from a line. Example (uses different function names to avoid repeating the original snippets):

// read full lines so addresses can contain spaces
void populate(playerType &out) {
    std::string tmp;
    std::getline(std::cin, out.first);
    std::getline(std::cin, out.last);
    std::getline(std::cin, out.address1);
    std::getline(std::cin, out.address2);
    std::getline(std::cin, out.city);
    std::getline(std::cin, out.state);
    std::getline(std::cin, out.zip);
    std::getline(std::cin, tmp);
    try { out.jersey = std::stoi(tmp); } catch (...) { out.jersey = 0; }
}

void display(const playerType &p) {
    std::cout << p.first << ' ' << p.last << '\n'
              << p.address1 << '\n' << p.address2 << '\n'
              << p.city << ", " << p.state << ' ' << p.zip << '\n'
              << p.jersey << '\n';
}

Troubleshooting tips: do not mix >> and getline without consuming the leftover newline (std::cin.ignore() or std::ws). Initialize your struct in main (playerType player{};) to avoid UB if something goes wrong. Keep ZIP as a string (preserves leading zeros). As suggested, pass the struct by reference so the input routine modifies the original; prefer a non-const reference for input and a const reference for printing. Compile with warnings enabled and use sanitizers (AddressSanitizer) or Valgrind to catch uninitialized reads.

1. To read your data with space use cin.getline( ... )
2. To use the player-data in you functions you need to pass it as reference or as a pointer for example:

void printPlayer(playerType &player) 
...
printPlayer(player);

or

void printPlayer(playerType *player) 
...
printPlayer(&player);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.