It does NOT create anything. It does NOT create an instance of that object.
Your question is akin to asking why this program cannot access the integer value x:
1 2 3 4
int main()
{
x = 5; // DOES NOT WORK
}
Why can't this code access the value x? Because that object, x, does not exist. The coder didn't create it.
Here is how the coder can fix it; by creating the object x.
1 2 3 4 5
int main()
{
int x; // Create the object...
x = 5; // THEN you can use it
}
So to use an object of type friends, in your code, first you have to create one of those objects, like this: friends a_friends_object;
Here, I have created an object of type friends. Now I can use it.
You have a friends struct. Clearly intended for representing something (i.e. one friend), and storing three pieces of information. A first name, a last name, and a DOB.
So you created three friend objects:
1 2 3
friends first;
friends last;
friends birth;
So this is a friend named "first", and a friend named "last", and a friend named "birth". This makes no sense. What are you intending to store inside the friend named "first"? Just a first name? But that friend also has a last name, and a DOB.
Then you try to store one piece of information in each of these friend objects: inFile >> first >> last >> birth;
That makes no sense. Shouldn't each friend object contain three pieces of information?
Here is how you might create ONE friend object, and store information in it.
I apologize. I have a file with a list of 15 people. All containing first name, last name, and date of birth. I need to loop through the file till the end of the file and output the contents to the screen.
Okay let me see if I can clarify this. I would need to initialize variables for each of the students and pass the information to them within a loop? I'm kind of confused at this point.
If you're going to store 15 friends from a loop, then you need to use an array (or vector).
underoathed wrote:
I need to loop through the file till the end of the file and output the contents to the screen.
That statement does not indicate that you have to store all 15 friends into memory.
If you only need to read and display each friend using a struct, than you only need a single instance of the struct.
Okay so after I tried your code it doesn't print anything to the screen. I'm assuming that would be since there are no values for f.fname, f.lname, f.dob correct? How would I go about storing the values in them?
Um... there's nothing in your code that reads anything from the file, or puts any data into the struct.
You open the file, you use it as the loop condition, but you never actually read anything from it.
EDIT:
Okay I guess I'm not understanding how that stores the data from the file into the struct to output it.
Perhaps you need to (re)read the section in your textbook about how to read from a file?
OK, it's clear from your earlier code that you know how to read from a file. Why did you remove that from your code?
Well, for starters, I assume you're getting compiler errors with that code? You should read those errors and try and understand them, because they help you work out what's wrong with your code.
Firstly, you've declared month as a string, and you can't do a switch on a string, only on integers. Presumably you intended to
Also, you're trying to repeatedly declare variables called month, within the same scope, and that's not legal.
Why are you declaring a new variable after each case label? You've already declared month at line 22.
Also, you really should use consistent indentation. It would make the flow of control much easier for both you and us to see.
Okay I am going to try a different route here. I want to break up the dob string and seperate it into month, day , year. How would I do that? Can I use cin.ignore?