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:
#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?
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?