defining struct strings

I have been building a program that will write structures to files, but I am having problems inputting values to the strings inside my struct.
Here are the relevant parts of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
struct character{
    string name;
    int lvl,xp,xp_to_lvl,hp_current,hp_max,mp_current,mp_max;
    int str,dex,con,INT,cha,wis;
    string char_class;
    int classdesig;
};
int main()
{
 char ch;
        cout << "Enter character's name: ";
        ch = _getch();
        while(ch != 13){//character 13 is enter
            character.name.push_back(ch);
            cout <<ch;
            ch = _getch();
        }
}

When I try to compile it, it says:
Error: unqualified-id before '.' token


I can't understand why it won't let me assign values to the strings based on this input, or even by assigning what I bring in through the input code to an intermediate string and then to the struct string.

I know that the input to the intermediate string without the struct string works fine, so it is something with the struct.

Could someone explain what is off in my syntax that is ruining my struct?

Thanks in advance.
Last edited on
Try while(ch != 13);? Also, you could try:

1
2
3
char ch
cout << "Enter character's name:";
cin >> ch

Last edited on
No, you definitely don't want a semicolon after your while loop. >_>

The problem is that you haven't defined an instance of your struct. In the same way that you are making std::string name, where name *is* an std::string, you need to make an instance of your class.

You wouldn't do std::string.push_back(), would you? So why would you do character.name?
Right, so should I make it write to an intermediate string and then make the struct string take the same value as the intermediate string?
Nope :)

You need to instantiate your struct:
1
2
character myCharacter;
myCharacter.name = "kaseron";
Topic archived. No new replies allowed.