passing array to a function

i have tried all i could but i still can't make it run

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
 #include<iostream>
#include<conio.h>
#include<stdio.h>
#include<cstring>
using namespace std;
struct info
{
	char name[20];
	int number;
};
void addcontact(info ,int );

void menue();
void main ()
{
	
	menue();

}
void menue()
{
	const int num=10;
	info contactlist[num];
	
	cout<<"_______________________\n";
    cout<<"     PHONE BOOK        \n";
	cout<<"_______________________\n";
	cout<<"1-Add Contact\n";
	cout<<"2-Edit Contact\n";
	cout<<"3-Delete Contact\n";
	cout<<"4-Show All Contacts\n";
	cout<<"5-Exit\n";
	int option;
	cin>>option;
	
	if(option==1)
	{
	addcontact(contactlist,num);
	}		
		
}
	void addcontact(info a[],int i)
	{
		 i=0;
		
		system("CLS");
		cout<<"Welcome to Add contact section\n";
		cout<<"Enter name\n";
		cin>>a[i].name;
		cout<<"Enter number\n";
		cin>>a[i].number;
		cout<<"Contact Added\n";
		i=i+1;
		if(i==9)
		{
			cout<<"Contact limit reached\n";
		}
		
		menue();
	
	}
	



	
 
Line 11 prototype void addcontact(info ,int ); does not match line 42 void addcontact(info a[],int i). Prototype says you are passing in a info struct but in line 42 your function is waiting for an array of info structs.
Line 42: You're passing i by value. Since you're updating it at line 53, you need to pass it by reference.

mobotus changing code made it work ty AbstractionAnon i needed to do that without using reference but thank you again
But still AbstractionAnon how would it be the function call and function argument for the following example be like if it was done through reference call i will try with both methods
i have made the complete projects but there are still 2 errors which i cant resolve they are linkers errors for which i dont know for which particular line the code has probelm with herez da 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<cstring>

using namespace std;
struct info
{
	char name[20];
	int number;
};
void addcontact(info a[],int );
void editcontact(info a[], int);
void deletecontact(info a[],int);
void showallcontact(info a[],int);
void exit();

void menue();
void main ()
{
	
	menue();

}
void menue()
{
	const int num=10;
	info contactlist[num];
	
	cout<<"_______________________\n";
    cout<<"     PHONE BOOK        \n";
	cout<<"_______________________\n";
	cout<<"1-Add Contact\n";
	cout<<"2-Edit Contact\n";
	cout<<"3-Delete Contact\n";
	cout<<"4-Show All Contacts\n";
	cout<<"5-Exit\n";
	int option;
	cin>>option;
	
	if(option==1)
	{
	addcontact(contactlist ,num);
	}
	else if(option==2)
		
	editcontact(contactlist,num);
	else if(option==3)
		
	deletecontact(contactlist,num);
			
		
	else if(option==4)
		
	showallcontact(contactlist,num);
	
		
	else if(option==5)
		
	exit();
		
	else
		cout<<"Invalide option selected\n";
			
		
}
	void addcontact(info a[],int i)
	{
		 i=0;
		
		system("CLS");
		cout<<"Welcome to Add contact section\n";
		cout<<"Enter name\n";
		cin>>a[i].name;
		cout<<"Enter number\n";
		cin>>a[i].number;
		cout<<"Contact Added\n";
		i=i+1;
		if(i==9)
		{
			cout<<"Contact limit reached\n";
		}
		
		menue();

	}
	void editcontact(info a[],int i)
	{
		int flag=0;
		int k;
		char name[20];
		system("CLS");
		cout<<"Welcome to Edit contact section\n";
		cout<<"Enter name to edit\n";
		cin>>name;
		for(k=0;k<10;k++)
		{
			if(strcmp(name,a[k].name)==0);
			{
				flag=1;
				break;
			}
		}
		if (flag==1)
		{
			cout<<"Enter a new name\n";
			cin>>a[k].name;
			cout<<"Contact Edited\n";
			menue();
		}
			else if(flag!=1)
			{
				cout<<"No record found\n";
				menue();

			}
	
	}
	void deletecontact(info a[],int i)
	{
		

char name[20];

cout<<"Enter name of contact to be deleted"<<endl;

cin>>name;

for(int j=0;j<10;j++)

{
if(strcmp(name,a[j].name)==0)
{
strcpy(a[j].name," ");
cout<<"contact deleted\n"<<endl;
menue();
}
}
}
void showallcontacts(info a[],int i)
{
	int l;
	cout<<"details\n";
	for (l=0;l<10;l++)
	{
		cout<<a[l].name<<endl;
		cout<<a[l].number<<endl;
	 
	}
	 menue();
}	
void exit()
{
	exit(0);

}
Line 15 and 50 you have a function you named showallcontact but on line 140 you renamed it to showallcontacts.

Last edited on
Yes thank you it works now , there seems to be some logical errors though :'(
It does all the function except showing of all contacts can you remove the logic error
Lines 41-63 beg for a switch statement.

Line 84: addcontact calls menue. You're creating a recursive situation since addcontact was called by menue.

If you don't want to pass the second argument by reference to addcontact, then addcontact should be an int function and return the number of contacts.

Line 98: Remove the ; at the end of the line. That terminates the if statement.

Line 144: What if you have less than 10 contacts? You pass in the number of contacts (i), but then don't use that number.

Line 150: Recursive call to menue again.

Line 152: Not a good idea to call your function exit. While C++ allows functions with the same name and different signatures, this is confusing and prone to error.
Topic archived. No new replies allowed.