Data structures and functions?

Hi guys! Would just like to get some help with my code here. The code below was originally written in C and I just tried to port it using C++ (which I am a total noob in) and I keep getting the following errors:

1>test.cpp(33): error C2660: 'insertData' : function does not take 1 arguments
1>test.cpp(38): error C2660: 'deleteData' : function does not take 1 arguments
1>test.cpp(94): error C2065: 'lastCount' : undeclared identifier

The thing is, those first two functions (insertData, deleteData) were fed an argument (x) and I'm not too sure as to why lastCount was made undeclared when in fact it is a member of the List data structure (and as far as I know, can be accessed globally). Hope you guys can help. In the mean time, I'll try to read up some more articles about this and hopefully get to solve it myself too.

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
#include "stdafx.h"
#define MAX 5

typedef struct List {
	int L[MAX];
	int lastCount;
	} List;
	
List L;

int menu();
void insertData();
void deleteData();
void displayData();
int isEmpty();
int isFull();
int locateData();
void makeNull();
void save();
void load();
void exit();

int main(){
	int x, opt;
	load();
	
	while(opt!=4) {
		opt = menu();
		switch(opt){
			case 1:
				printf("Insert data");
				scanf("%d", &x);
				insertData(x);
				break;
			case 2:
				printf("Delete data");
				scanf("%d", &x);
				deleteData(x);
				break;
			case 3:
				displayData();
				break;
			case 4:
				save();
				return 0;
			default:
				printf("Invalid option.");
				break;
		}

		return 0;
	}
}

int menu(){
	int opt;
	int i;
	char *n[] = {"Insert data", "Delete data", "Display data", "Display data", "Exit"};
	
	printf("Menu");
	
	for(i=0; i<=4; i++){
		printf("%d - %s", i+1, n[i]);
	}
	
	printf("Input option: ");
	scanf("%d", &opt);
	return(opt);
}

void makeNull(){
	L.lastCount = -1;
}

int isEmpty(){
	return(L.lastCount == MAX-1);
}

int locateData(int x){
	int i;
	
	for(i=0; i<=L.lastCount; i++){
		return(i);
	}
	
	return (-1);
}

void insertData(int x){
	if(isFull()){
		printf("List full.");
	} else {
		L.lastCount++;
		L.L[lastCount] = x;
	}
}

void deleteData(int x){
	int i, p;
	if(isEmpty()){
		printf("List empty.");
	} else {
		p = locateData(x);
		if(p=0){
			printf("Not found.");
		} else {
			for(i=p; i<=L.lastCount; i++){
				L.L[i] = L.L[i++];
			}
			L.lastCount--;
		}
	}
}

void displayData(){
	int i;
	
	for(i=0; i<=L.lastCount; i++){
		printf("%d", L.L[i]);
	}
	
}

void save(){
	int i;
	FILE *fp;
	fp = fopen("file.dbf", "w+");
	
	for(i=0; i<=L.lastCount; i++){
		fprintf(fp, "%d \n", L.L[i]);
	}
	
	fclose(fp);
}

void load(){
	int y;
	FILE *fp;
	makeNull();
	fp = fopen("file.dbf", "r+");
	
	while(!feof(fp)){
		fscanf(fp, "%d \n", &y);
		insertData(y);
	}
	
	fclose(fp);
}
Last edited on
When you prototyped the functions you didn't specify any parameters.

lines 12, 13, and 17 should be
1
2
3
void insertData(int);
void deleteData(int);
void locateData(int);

respectively.
When prototyping a function you must specify the parameter types the function takes. You may specify a name for the parameters (for clarity), but it is ignored by the compiler and it can be different in the function definition.

EDIT:
and line 94 should be
L.L[L.lastCount]
which will resolve that third compiler error.
Last edited on
Thanks. Solved this problem for the most part but I got an error in the file stream now. Oh boy.
Topic archived. No new replies allowed.