array entry

You are to create an application which stores information about people, along with a ‘task list’. The application’s functionality can be described as follows:

•The user should be able to store peoples’ names along with several pieces of information about each person (age, telephone number, favourite football team etc.).



Should I use char to store the information with as I need to be able to search it later or should I use string? Also as I need to be able to search by age too how can I use age into the same array?wouldnt I need int for 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	#include <iostream> 
	#include <string>
	#include <math.h> 
	#include <fstream>
	using namespace std;

	int main()
	{
		int i;
		string information[10][7];
	
		//This bit should check if theres anything stored currently.
		cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
		cin >> i;
		i--;

		//input
		for (int j=0;j<7;j++)
		{
		switch(j+1)
		{case 1: cout << "\nFirst Name: "; break;
		case 2: cout << "\nLast Name: "; break;
		case 3: cout << "\nAge: "; break;
		case 4: cout << "\nEmail: "; break;
		case 5: cout << "\nDoor Number: "; break;
		case 6: cout << "\nRoad Name: "; break;
		case 7: cout << "\nPost Code: "; break;
		default:;}		

		cin >> information[i][j];}

		// output
		for (int j=0;j<7;j++)
		{	
		switch(j+1)
		{case 1: cout << "\nFirst Name: "; break;
		case 2: cout << "\nLast Name: "; break;
		case 3: cout << "\nAge: "; break;
		case 4: cout << "\nEmail: "; break;
		case 5: cout << "\nDoor Number: "; break;
		case 6: cout << "\nRoad Name: "; break;
		case 7: cout << "\nPost Code: "; break;
		default:;}		

		cout << information[i][j];}


		system("PAUSE");



	return 0;
	}
Last edited on
One way would be to create a struct or class containing the various fields relating to a single person. Think of the complete thing as a new user-defined data type.

Then you could create an array of these structs. Or a vector, which can grow dynamically as required.
I have updated it to show what I have so far. I need to be able to search up the records too using their age or last name for example.? any idea how to do this . Yes I have too many questions lol
closed account (D80DSL3A)
Have you considered Chervils suggestion yet?
Have you learned about structures or classes?

If you can't use a structure for containing the elements of a "person", then parallel arrays may be the used.

How to tell if a "slot" has been used before? It's new if all the string sizes are still zero.
If you are a beginner (don't know anything about struct or class) then I highly recommend you pick a tutorial about the struct rather than class, as classes are actually Object Orientated Programming which is a much more complex area (not going to explain now why, but trust me, better hang on with struct for now).
If you need help on understanding structs, either PM me, or just ask me in here. Though, it's not such big deal, and if you google it, I'm quite sure you'll get it.

Hope I could help,
~ Raul ~
Last edited on
well I understand classes(sort of) Ill search up structs now . But I think I have done this too just don't know what its called? I mean I do object orientated programming as a module so I should have done it.

If I use structs or classes would I be able to use a search algorithm to find the data? eg if I searched for someone by age or such ? Im completely lost on this task.
The whole reason for using class/struct is to organize your code more efficiently and actually make it easier to create a search algorithm to find the requested data. I say easier, given you already know how to use class/struct. If you don't know then then there's a learning curve but it's well worth it. Structs are simpler, have less syntax to learn and should fit your program better given its current size.

On the other hand you can (almost) continue with what you have. It won't scale easily but it'll get the job done. Everything currently gets stored in information[i][j] so you could add a menu that asks the user if he would like to add 'information' or return an item from 'information' and list the options. You'll need a function to replace an old entry/warn the user of an old entry first.

'(almost)': About the search feature - it would probably be easier to store integer values into an integer array and string values into a string array meaning that you'll need 2 arrays (parallel as was mentioned earlier). Then, when the user searches for a specific age, u'll be able to return the entry it was found. Similar with a person's name. So that when you print the entry 5, it'll print age,telephone, etc from the integerarray[5][all] and the stringarray[5][all].

Hope this helps
Last edited on
Topic archived. No new replies allowed.