Like Tree1Likes
  • 1 Post By oogabooga

Creating References to Variables Within Structures and Using the cin.getline Function

This is a discussion on Creating References to Variables Within Structures and Using the cin.getline Function within the C++ Programming forums, part of the General Programming Boards category; Hello again all, So I've been doing a more organized re-write of a text adventure program I've been working on ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Creating References to Variables Within Structures and Using the cin.getline Function

    Hello again all, So I've been doing a more organized re-write of a text adventure program I've been working on and putting variables into structures, but I've had some serious difficulty accessing the variables within them.

    For example, when I was trying to make references to the variables within the structure:
    Code:
    struct attributes {
        int name[256];
        int race;
        int health_stat;
    };
    Which I defined individually in main as:
    Code:
    attributes player_attributes;
    I created a case switch:
    Code:
    switch (player_attributes.race)
        {
        case 1:
        player_attributes.health_stat = 1;
        int& player_attributes.health_stat_reference =player_attributes.health_stat;
        cout << "You reach up with a hoof and touch the shattered remnants of your horn.\n Instantly, a fire shoots through your skull and you writhe in pain. \nThe mare yanks your hoof away. \n \"Don't touch that!\" \n She grabs your head and looks you dead in the eye. \n\"Look, I caulked it in time, There's a chance it will heal, \n but if you poke at it, or try to throw\naround magic, that's going to be a lot less likely.\" \n\n";
        break;
        case 2:
        player_attributes.health_stat = 1;
        int& player_attributes.health_stat_reference =player_attributes.health_stat;
        cout << cout << "You try to spread your wings and cry out in pain, noticing the bandages too \n late. you feel like there are a thousand firey daggers rammed  into your wings, \nor what's left of them.you huddled yourself into a ball. \n The mare reaches out a hoof and turns your head to face her. There's a strange determination in her eyes. \"Listen to me. You, are going to fly again, you \njust need to be patient\" She looks away wistfully for a moment. You feel \nstrangely awkward. \n\n";
        break;
        case 3:
        player_attributes.health_stat = 1;
        int& player_attributes.health_stat_reference =player_attributes.health_stat;
        cout << "You shift the blanket away and scream at the top of your lungs. Your left hind leg now ends right below your hock in a carefully woven wad of bandages. Old gods... You were a soldier! Even if the General didn't banish you for the desertion how are you going to feed Mom? How could you work? How could you- The mare puts a hoof on your shoulder and looks you dead in the eye. \n\"Listen to me okay? If I have anything to say about it, you are going to WALK out of here. We don't care which side you were fighting on.\" \n\n";
        break;
        }
    In this case switch, I get a compiler error every for every line where I try to define a reference that says:
    error: expected initializer before '.' token

    None of my internet searches so far have been able to turn up why.


    Furthermore, I have another compiler error when I try to access the array "name" within the structure above using the cin.getline function, which I attempted to do as follows:

    Code:
      cout<<"Please enter your character's name (255 characters or less): ";
        cin.getline (player_attributes.name, 256);
    Despite the fact that I used basically the same syntax to get input to the string defining the character's name in another program, and that I've inserted header data such that:
    Code:
    #include <iostream>
    
    using namespace std;
    I get a compiler error stating that:
    error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> >::getline(int [256], int)'

    I really have no idea what's going on here, despite my research. Does anyone have any ideas as to what would be causing these things? I would be extremely grateful for any help that could be offered.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Problem 1: You can't add another member to a struct later on, as you are apparently are trying to do (but why?).

    Problem 2: You defined name[256] as an int array. To use it with cin.getline it has to be a char array.
    King Mir likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by oogabooga View Post
    Problem 1: You can't add another member to a struct later on, as you are apparently are trying to do (but why?).

    Problem 2: You defined name[256] as an int array. To use it with cin.getline it has to be a char array.
    Oh! Sorry, I must have completely spaced out on the int array thing. Thank you so much. But how am I adding another member to a structure later on? All I was trying to do was define a reference for something in a structure.

    Edit: I mean, I defined:
    Code:
      int& player_race_reference = player_attributes.race;
    in main and I didn't have any compiler errors there. Am I supposed to define references for variables within a structure within the structure?
    Last edited by chaucer345; 07-22-2012 at 10:07 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,218
    Check this line.

    Code:
    int& player_attributes.health_stat_reference =player_attributes.health_stat;

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by Matticus View Post
    Check this line.

    Code:
    int& player_attributes.health_stat_reference =player_attributes.health_stat;
    Oh! Oh... that makes sense. I fixed that, and after yanking the copy pasted statement to that effect out of the case switch the program compiled.
    Thank you so much for your help! I would have spent forever trying to figure that out.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I would be far better if you wouldn't use char arrays.
    Change struct to

    Code:
    struct attributes
    {
        std::string name;
        int race;
        int health_stat;
    };
    And use std::getline:
    Code:
    cout<<"Please enter your character's name: ";
    std::getline (player_attributes.name, std::cin);
    Never trust user to put a limit on the length of anything. This is completely safe and you don't have to worry about any kind of limit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    Quote Originally Posted by Elysia View Post
    Code:
    std::getline (player_attributes.name, std::cin);
    should be:

    Code:
    std::getline (std::cin, player_attributes.name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2011, 09:18 PM
  2. References and function calls
    By Niels_M in forum C++ Programming
    Replies: 11
    Last Post: 09-01-2010, 01:49 PM
  3. references to function in classes
    By Chrip in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2007, 11:39 AM
  4. function parameter references
    By Bob001 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2003, 07:45 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21