structs are frustrating

Pages: 12
Hello! I am having a hard time with structs. I don't understand how to access a struct in a while loop. Can you advise on this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

struct friends
{
	string fname;
	string lname;
	string dob;
};

int main(){
	
	ifstream inFile;
	ofstream outFile;
	
	outFile << fixed << showpoint;
	outFile << setprecision(2);
	
if(!inFile)
{
	cout << "Unable to open file. Please check the file." << endl;
}

int i = 0;


while (!inFile.eof())
{
	inFile >> fname >> lname >> dob;
	
}

}
A struct defines a new kind of object.

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.

Ahh okay. I fixed that portion. Now it's throwing me an error.

Error:
 
31	9	[Error] no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'friends') 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

struct friends
{
	string fname;
	string lname;
	string dob;
};

int main(){
	
	ifstream inFile;
	
friends first;
friends last;
friends birth;	
if(!inFile)
{
	cout << "Unable to open file. Please check the file." << endl;
}

int i = 0;

while (!inFile.eof())
{
	inFile >> first >> last >> birth;
	cout << first << " " << last << "      " << birth << endl;
	
}

system("Pause");
return 0;
}


Did I use the syntax correctly so far?
Your program logic looks very wrong.

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.

1
2
friend first_friend;
inFile >> first_friend.fname >> first_friend.lname>> first_friend.dob;

Last edited on
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.
I have a file with a list of 15 people.


Going to need to create 15 friend objects, then. I suggest a vector of them.

1
2
3
4
5
vector<friend> fifteen_friend_objects;
for (int i = 0; i < 15; ++i)
{
  inFile >> fifteen_friend_objects[i].fname >> fifteen_friend_objects[i].lname>> fifteen_friend_objects[i].dob;
}
Sorry I don't know waht a vector is and I can't use an array to do this.
@Moschops,

friend is a reserved word.
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.

1
2
3
4
5
friends f;
while (infile >> f.first >> f.last >> f.birth)  // removed use of eof()
{	cout << f.first << " " << f.last << "      " << f.birth << endl;
	
}

Last edited on
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?
Last edited on
Line 2 reads the respective fields into the struct.
Okay I guess I'm not understanding how that stores the data from the file into the struct to output it.
Does anyone have any more advice on this? I don't quite get why it doesn't output anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

struct friends
{
	string fname;
	string lname;
	string dob;
};

int main(){
	
	ifstream inFile;
	
	inFile.open("friends.txt");
	
while(!inFile)
{
	cout << "Unable to open file. Please check the file." << endl;
}

friends f;
int i = 0;
while (!inFile.eof())
{	
	cout << f.fname << " " << f.lname << "      " << f.dob << endl;
	i++;
}



system("Pause");
return 0;
}
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?
Last edited on
My fault about that. I re-wrote the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

struct friends
{
	string fname;
	string lname;
	string month;
	string day;
	string year;
};

int main(){
	
	ifstream inFile;
	inFile.open("friends.txt");
	
	string month = "";
	
if(!inFile)
{
	cout << "Unable to open file. Please check the file." << endl;
}
friends f;
int index = 1;

cout << "    My friends and their birthdays" << endl;

cin.ignore(2, '/');
cin.ignore(5, '/');
while(!inFile.eof())
{
	inFile >> f.fname >> f.lname >> f.month >> f.day >> f.year;
	
	
	switch(month)
	{
	case '01':
		string month = 	"January";
		break;
	case '02':
		string month = "February";
		break;
	case '03':
		string month = "March";
		break;
	case '04':
		string month = "April";
		break;
	case '05':
		string month = "May";
		break;
	case '06':
		string month = "June";
		break;
	case '07':
		string month = "July"
		break;
	case '08':
		string month = "August";
		break;
	case '09':
		string month = "Spetember";
		break;
	case '10':
		string month = "Ocober";
		break;
	case '11':
		string month = "November";
		break
	case '12':
		string month = "December";
		break;
	default:
		cout << "Invalid date of birth format. Please enter as 11/11/1234. " << endl;
		
	}
	cout << index << "." << f.fname << " " << f.lname << setw(30) << setfill(' ') << f.month << f.day << f.year << endl;
	index++;
	
system("Pause");
return 0;
}


It works. I was overcomplicating it as usual. Now to use a switch statement to convert the date of birth to this format:

September 1, 1986

Can I get some advisement on this?
Last edited on
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 the error says:

[Warning] multi-character character constant [-Wmultichar]

I'm assuming this means that I'm trying to convert, or switch an into to a string which is illegal correct?
Is that with the code you posted? Or have you made changes?

What line of the code does that occur on?
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?

Here is my code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

struct friends
{
	string fname;
	string lname;
	string dob;
};

int main(){
	
	ifstream inFile;
	inFile.open("friends.txt");
		
if(!inFile)
{
	cout << "Unable to open file. Please check the file." << endl;
}
friends f;
int index = 1;
int month;
int day;
int year;
while(!inFile.eof())
{
	inFile >> f.fname >> f.lname >> f.dob;
	
	string name = f.fname + ' ' + f.lname;
	cout << index << "." << name << setw(30) << setfill(' ') << f.dob << endl;
	index++;
}

system("Pause");
return 0;
}
Last edited on
Pages: 12