Program on link list to structs due in 30/45 min and stuck

I suppose to write a program using link list to link structs of an address book. I keep getting errors on the pointer dereferencers that refer to the pointer inside the functions.

Error says: Must have pointer type (but I'm using it as a formal parameter and the actual parameter is a pointer.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include "stdafx.h"
#include <cstdlib>
#include <iostream> 
#include <cstring>
#include <string>	
#include <fstream>	
using namespace std;

//constant number of friends
const int numberOfFriends = 100;

//struct
struct addressType
{
	string  firstName;
	string  lastName;
	string  address;
	string  phone;
	addressType *link;
};

//function prototype
void addressFunction(addressType& first, int friends);
void write_addressBook(addressType& second, int fullIndex);

int _tmain(int argc, _TCHAR* argv[]){
	
	addressType *head, *temp, *newNode;
	head = nullptr;
	temp = nullptr;
	newNode = nullptr;
	newNode = new addressType;

	//variables 
	char addAddress;
	int friendCounter = 0;
	int exitLoop = 0;
	cout << "Welcome to Address Book.\n " << endl;

	do{
		cout << "Do you want to add a friend? (Y or N): ";
		cin >> addAddress;
		cout << endl;

		if (addAddress == 'N' || addAddress == 'n'){
			cout << "Thank you for using Address Book" << endl;
			exitLoop = 1;
		}
		else{

			addressFunction( *newNode, friendCounter);
			friendCounter++;
		}
	} while ((exitLoop == 0) && (friendCounter < numberOfFriends));

	if (friendCounter != 0){

		write_addressBook(*newNode, friendCounter);

		cout << "Thank you for choosing Address Book.";
	}
	system("pause");
	return 0;
}

//Function definition 
void addressFunction(addressType& first, int friends){
	
	cin.ignore(100, '\n');

	cout << "Please enter the first name of the person you";
	cout << " wish to add to your Address Book: " << endl;
	getline(cin, first->firstName);
	cout << endl;

	cout << "Please enter the last name of the person ";
	cout << "you wish to add to your Address Book : " << endl;
	getline(cin, first->lastName);
	cout << endl;

	cout << "Please enter " << first->firstName << " " << first->lastName << "'s address.";
	getline(cin, first->address);
	cout << endl;

	cout << "Please enter " << first->firstName << " " << first->lastName << "'s phone number.";
	getline(cin, first->phone);
	cout << endl;

}

void write_addressBook(addressType second, int fullIndex)
{
	ofstream outfile;
	string fileName;

	cout << "Please enter a file name to store your address book: ";
	cin >> fileName;
	cout << endl;

	outfile.open(fileName.c_str());

	if (outfile.is_open()) {
		for (int i = 0; i < fullIndex; i++){
			outfile << second->firstName << "," << second->lastName << endl;
			outfile << second->address << endl;
			outfile << second->phone << endl << endl;
		}
		outfile.close();
	}
	else{
		cout << fileName << " did not open.\n\n";
	}
}
> but I'm using it as a formal parameter and the actual parameter is a pointer.
void addressFunction(addressType& first, int friends){
`first' is not a pointer.


> Error says: Must have pointer type
It also says the line number, ¿why are you withholding information?
Sorry the error was showing up every time I hovered over the variable for that is attached to the dereferencing operator. Also my bad on the wrong syntax for what I thought was a pointer in the formal parameter. Only been at this for a little more than 6 moths. I thought it was a reference to the pointers address.

I reloaded the program and those errors went away. Now...Stuff like:
1
2
3
(74): error C2819: type 'addressType' does not have an overloaded member 'operator ->'

(74): error C2232: '->addressType::firstName' : left operand has 'struct' type, use '.'

I hovered over the variable attached to the dereferencing
Also, I changed formal parametesr to (addressType *first)...
In your program , i don't see you use the link list. In order to debug program, I modify the program somewhere. First , i use the list to memory the new node. Second , i add some code to delete the memory you do the dynamic memory allocation in the while cycle.
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <cstdlib>
#include <iostream> 
#include <cstring>
#include <string>	
#include <fstream>	
using namespace std;


//constant number of friends
const int numberOfFriends = 100;

//struct
struct addressType
{
	string  firstName;
	string  lastName;
	string  address;
	string  phone;
	addressType *link;
};

//function prototype
void addressFunction(addressType& first, int friends);
//void write_addressBook(addressType& second, int fullIndex);
void WriteAddressBook(addressType* head);//the argument is the head of list;
void InsertList(addressType*& head,addressType& newnode);

int main(int argc, char* argv[]){
	
	addressType *head, *newNode;
	head = nullptr;
	newNode = nullptr;

	//variables 
	char addAddress;
	int friendCounter = 0;
	int exitLoop = 0;
	cout << "Welcome to Address Book.\n " << endl;

	do{
		cout << "Do you want to add a friend? (Y or N): ";
		cin >> addAddress;
		cout << endl;

		if (addAddress == 'N' || addAddress == 'n'){
			cout << "Thank you for using Address Book" << endl;
			exitLoop = 1;
		}
		else{
			newNode=new addressType;
			addressFunction(*newNode, friendCounter);
			InsertList(head,*newNode);//insert the new node in the list
			friendCounter++;
		}
	} while ((exitLoop == 0) && (friendCounter < numberOfFriends));

	if (friendCounter != 0){

		//write_addressBook(*newNode, friendCounter);
		WriteAddressBook(head);
		cout << "Thank you for choosing Address Book.";
	}
	for(addressType* temp=head;temp!=nullptr;)//free memory
	{
	    head=head->link;
	    delete temp;
	    temp=head;
	}
	system("pause");
	return 0;
}

//Function definition 
void addressFunction(addressType& first, int friends){
	
	cin.ignore(100, '\n');

	cout << "Please enter the first name of the person you";
	cout << " wish to add to your Address Book: " << endl;
	getline(cin, first.firstName);//first's data type is struct not struct pointer
	cout << endl;

	cout << "Please enter the last name of the person ";
	cout << "you wish to add to your Address Book : " << endl;
	getline(cin, first.lastName);
	cout << endl;

	cout << "Please enter " << first.firstName << " " << first.lastName << "'s address.";
	getline(cin, first.address);
	cout << endl;

	cout << "Please enter " << first.firstName << " " << first.lastName << "'s phone number.";
	getline(cin, first.phone);
	cout << endl;

}

//void write_addressBook(addressType &second, int fullIndex)//omit &,should use reference  
void WriteAddressBook(addressType* head)
{
	ofstream outfile;
	string fileName;

	cout << "Please enter a file name to store your address book: ";
	cin >> fileName;
	cout << endl;

	outfile.open(fileName.c_str());

	if (outfile.is_open()) {
		/*for (int i = 0; i < fullIndex; i++){
			outfile << second->firstName << "," << second->lastName << endl;
			outfile << second->address << endl;
			outfile << second->phone << endl << endl;
		}*/
		for(;head!=nullptr;head=head->link)
		{
		    outfile <<"name:\t"<< head->firstName << "," << head->lastName << endl;
		    outfile <<"address:\t"<< head->address << endl;
		    outfile << "phone:\t"<<head->phone << endl << endl;
		}
		outfile.close();
	}
	else{
		cout << fileName << " did not open.\n\n";
	}
}

void InsertList(addressType*& head,addressType& node)
{
    node.link=head;
    head=&node;
}
Topic archived. No new replies allowed.