Array of structs

I'm trying to create a user-defined array using struct, create an array of contacts, and sort and search the array by both first and last name. Right now I'm trying to build the array and simply display it right now but its erroring out on me right off the bat. This is new territory for me. The only thing that is underlined in red in the code is commented with the error.



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
#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

//Structure declaration
struct Contact
{
	string firstName;
	string lastName;
	string phone;

};

//Funtion prototypes
void enterContacts(Contact contacts[]);
void printContacts(Contact contacts[]);
//void sortByFirstname(Contact contacts[]); // commented out for later build
//void sortByLastname(Contact contacts[]);
//Contact searchByFirstname(Contact contacts[], string firstname);
//Contact searchByLastname(Contact contacts[], string lastname);


int _tmain(int argc, _TCHAR* argv[])

{
    Contact contacts[5];
	
	int count;

	//input the contacts
	for (int i = 0; i < 5; i++)
	{
		cout << enterContacts << (i + 1) << ": "; 
	}

	//Display contacts
	cout << "The contacts entered are: \n"
		<< printContacts;
	_getch();
	return 0;
}

//Functions
//Prints out the array of people
void printContact(Contact contacts[])
{
	for(int i = 0; i < 5; i++)
	{
		cout << i << ":" << /* Error no operator "<<" matches these operands*/ contacts[i].firstName << " " << contacts[i].lastName <<  contacts[i].phone << "     ";
	}
	cout << endl;

}
//void sortByFirstname(Contact contacts[]); //commented out for later build
//void sortByLastname(Contact contacts[]);
//Contact searchByFirstname(Contact contacts[], string firstname);
//Contact searchByLastname(Contact contacts[], string lastname); 
You forgot #include <string>
Include header <string>
Why are you passing a void type to cout? Also that's not how you call a function.
Ok I added #include <string> which took care of the underline error. Can't believe I forgot that. I'm trying to restructure the input contacts in main() but I can't get it to work.

I've tried enterContacts(contacts); enterContact(Contact[]); enterContacts(Contact contacts[]); among a few other things. Any ideas what I'm missing here? Probably something simple again.

The goal I was trying to reach was structure the code to say
Enter first name for person 1:
Enter last name for person 1:
Enter phone for person 1:
Enter first name of person 2:

and so forth as you enter names and information into the array.
Line 40 has a problem...

Look at line 17 and line 35. What's the difference between the two ?
Also, the function declared on line 17 doesn't have a definition...a function with no definition is uneventful indeed :p
Thanks soranz

I added the function for enterContacts
1
2
3
4
5
6
7
8
9
10
//enter contacts
void enterContacts(Contact contacts[])
{
	cout << "Enter the firstname: ";
	cin >> contacts[].firstName;
	cout << "Enter the last name: ";
	cin >> contacts[].lastName;
	cout << "Enter phone number: ";
	cin >> contacts[].phone;
}


I get an error indicator under the second square bracket. "Expecting an expression." So I'm still missing something

And still no luck calling the functions in the main().
These are my current tries.
enterContacts(Contact contacts[]); printContacts(Contact contacts[]);
I have been trying to get this to work and have got the enterContacts function to work. My problem now is that I can't get the printContacts function to work properly. The printContacts will only print out the last entry from the enterContacts 5 times. I want it to print out each of the contacts from the array. Any idea how to make it work that way?

My code so far
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Structure declaration
struct Contact
{
	string firstName;
	string lastName;
	string phone;

};

//Funtion prototypes
void enterContacts(Contact&);
void printContacts(Contact contacts);
//void sortByFirstname(Contact contacts[]); // commneted out for later build
//void sortByLastname(Contact contacts[]);
//Contact searchByFirstname(Contact contacts[], string firstname);
//Contact searchByLastname(Contact contacts[], string lastname);


int _tmain(int argc, _TCHAR* argv[])

{
    Contact contacts;
	
	
	
	//input the contacts
	cout << "Enter Contacts. \n";
	
		 enterContacts(contacts); 
	

	//Display contacts
	cout << "The contacts entered are: \n";
		 printContacts(contacts);
	
	system("pause");
	return 0;
}

//Functions
//enter contacts
void enterContacts(Contact &contacts)
{
        for (int i = 0; i < 5; i++)
	{
	cout << "Enter the first name of person " << i <<": ";
	cin >> contacts.firstName;
	cout << "Enter the last name of person " << i <<": ";
	cin >> contacts.lastName;
	cout << "Enter phone number of person " << i <<": ";
	cin >> contacts.phone;
	}
}

//Prints out the array of people
void printContacts(Contact contacts)
{
	for(int i = 0; i < 5; i++)
	{
		cout << i << ":" << contacts.firstName << " " << contacts.lastName << " " << contacts.phone << "     \n";
	}
		cout << endl;
	}

}
//void sortByFirstname(Contact contacts[]); //commented out for later build
//void sortByLastname(Contact contacts[]);
//Contact searchByFirstname(Contact contacts[], string firstname);
//Contact searchByLastname(Contact contacts[], string lastname); 

Last edited on
If you want to work with arrays, it might be good to have an array in the code somewhere.
Thanks for the reply cire.
I am going off the guide lines for my project by creating a user defined array. Ideally I would have the program read and append a text file but I was told I can't do that for this project. The array structure and functions are predetermined and mandatory for me to get the grade.

Its not like I'm saying here is my homework do it for me. There is either something I am missing, over looking, or obviously don't know and I'm here trying to help myself understand it. A fresh pair of eyes can usually see something that has been overlooked and a more knowledgeable coder can help me understand.

This program has made me feel like I am completely brain dead the first couple of programs for this semester were so easy I whipped them out in class, but this one, even the simplest stuff has eluded me.
Once again, there is no array in your code. Nor are there any functions in your code which deal with arrays.

enterContacts takes a reference to one contact and stores user input in it 5 times.
printContacts takes a copy of one contact object and prints it out 5 times.
main declares one contact object.
You were actually on the right track before you restructured your code.

1
2
void enterContacts(Contact contacts[]);
void printContacts(Contact contacts[]);

Was correct but then you changed it to
1
2
void enterContacts(Contact&);
void printContacts(Contact contacts);

Now you are passing a reference to your structure.

So go back to
1
2
void enterContacts(Contact contacts[]);
void printContacts(Contact contacts[]);


Add this back to where you reference the array in the main
[5]


Next you want to make sure your functions match the Function prototypes.
You had them right at one point
enterContacts(Contact contacts[]); printContacts(Contact contacts[]);

In your printContacts (original code) you had contacts[i].firstName
You want to mimic that in your enterContacts.

You had the basics already in place but you over thought it. Once you get past this huddle and you should be good to go. You had all the answers just not the right order.
Hey thank you desago. I have just been feeling brain dead lately and after you pointed out what I needed to do it all fell into place I've already moved pasted the printing of the array and worked through my first and last name sort. Now I'm working on the search aspect.
I'll leave this thread open if I need to ask questions about the search aspect.
Topic archived. No new replies allowed.