simply a pain

Hi,

I was so excited I had this figured out. NOPE!
Here we go should be simple.

1
2
3
4
5
6
7
// Loop to set location of index
	for( int i = 0; i < size; i++)
	{
		if( tele[i].lname.length() == 0 )
		index = i;
	}
	

This is suppose to go through an array and see how long the last name's length is if it is == to 0 then the index at tele[i] = index;
The issue is if I make it > 0 after the .length then it will overight the last element if it is 0 it will do the first element.

All I want from the code is to just
| 2 | 5 | 3 | 3 | empty(1) | empty(2) |

go through the array above( they are strings in reality) and see oh hey the length is is not 0 so lets go to the next one. Until it gets to empty(1) then it says oh hey this length is 0 so assign i to index.
OK so what is the deal here?
Also someone told me that tecnically arrayys still have crap in them although there is not say a 3, or a 2 in the array. That empty(1) has some of the computer bull*hit in it or something? I"m not sure.
Thanks
Last edited on
if it is == to 0 then the index at tele[i] = index;
???
What?

The issue is if I make it > 0 after the .length then it will overight the last element if it is 0 it will do the first element.
I have no idea what you just said.

With your current code, at the end of the loop index will have either the same value it had before the loop (if none of the strings were empty), or the position of the last element in the array that meets your criteria (because every new element that meets it overwrites the value of index).
With your example above, index == 5 at the end of the loop, and tele[index].lname == empty(2).
OK let me explain it again sorry.

0 | 1 | 2 | 3 | empty(1) | empty(2) |

I just want a way to add to the array. I want a way so that I can add to it with out overwriting the any of the index's with a value (0, 1,2,3). Thats what I was trying to do above. I just was trying to look at the given index (i) and and see if something was there. If it was empty I want to assign the index i to a variable and then use it as the location so when the user enters information it will be stored in the correct empty spot.

This is the idea. But like I said I'm having issues it is not being added correctly either it adds to the wrong begining and overwrites the first information at that location or it does it to the end.

Take a look>


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
//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	int index = 0;
	
	// Loop to set location of index
	for( int i = 0; i < size; i++)
	{
		if( tele[i].lname.length() > 0)
		index = i;
	}
	
	//Message for option A function
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	//To get first name
	cout << "Please enter the first name. " << endl;
	cin >> tele[index].fname;
	//To get last name
	cout << "Please enter the last name. " << endl;
	cin >> tele[index].lname;
	//To get address
	cout << "Please enter the street address. " << endl;
	cin.get();
	getline(cin,tele[index].streetAdd); 
	//To get city,state,zip
	cout << "Please enter the city, state and zip code." << endl;
	getline(cin,tele[index].cityStateZ); 
	//To get phone
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	cin >> tele[index].phone; 
	
	    
if A[i] = i, this works:


1
2
3
4
5
6
7
8
9
10
int A[3];
A[0] = 0;
for (int i=0; i<3; ++i)
{
	if(A[i] != i)
	{
		int j = i;
		break;
	}
}
Last edited on
But the thing is it has to be general I don't know what it would be position 3 maybe I call the function again now it needs to be added to position 4 ...How would I account for that?
Thanks for the idea
what do you save in this array?
from your example it looked like A[i] = i.
so the array element with the index i contains the index i
I read from a file. That has this in it .

Bill Smith
9763 N. Adrian Hwy
Ann Arbor Mi,48719
517-260-9871
Lauren Wilt
8752 W. Munger Rd.
Tecumseh Mi, 48719
517-423-9826
Cory Tilton
9853 N. Territorial
Detroit Mi, 89915
313-987-5600
Walter Freeman
8752 W. Miles Rd.
Adrian Mi, 48719
517-423-7989
Jack Cowen
9853 Gabor ave.
Detroit Mi, 89915
313-987-2121



Each set of fname, lname, address, (city,state,zip), phone is at a index I used a structure.

Then my function is supposed to get this information and add another one to the list. Not over-wright the the index that is alreadly there. So the size should be able to handle this I made it big enough so that there is room because we havent learned vectors. Do you get what I'm saying now?
Thanks dude
post your whole code please, i dont really get the struct and array thing
So, all it does not is if I enter an new thing.
Llinas
Rodolfo
123. Bulls*it street
Ann Arbor MI, 29292
333-333-3333
It will save all this info over Bill smith or maybe the last one. But doesn't simpily add to the array.
post your whole code please
OK, Please over look the mess it is a work in progress so I have comments on things that I want to add and so forth.

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 10;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};

// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{	
	
	int i = 0;
	while(!fin.eof())
   {
		fin >> tele[i].fname;
		fin >> tele[i].lname;
		fin.get();
		getline(fin,tele[i].streetAdd, '\n');
		getline(fin,tele[i].cityStateZ, '\n');
		getline(fin,tele[i].phone, '\n');
	i++;
   }

	size = i;

}

//Function to display Menu
void displayMenu()
{
	cout << "Phone Directory Program " << endl;
	cout << endl;
	cout << "Options" << endl;
	cout << "[A]dd and entry to the phone directory. " << endl;
	cout << "[D]elete an entry from the phone directory. " << endl;
	cout << "[U]pdate an entry from the phone directory. " << endl;
	cout << "[L]ist the entire phone directory. " << endl;
	cout << "[E]xit the menu" << endl;
}

//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	int index = 0;
	
cout <<	endl;
	// Loop to set location of index
	for( int i = 0; i < size; i++)
	{
		if( tele[i].lname.length() == 0)
		index  = i + 1;
	}
	
	//Message for option A function
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	//To get first name
	cout << "Please enter the last name. " << endl;
	cin >> tele[index].lname;
	//To get last name
	cout << "Please enter the first name. " << endl;
	cin >> tele[index].fname;
	//To get address
	cout << "Please enter the street address. " << endl;
	cin.get();
	getline(cin,tele[index].streetAdd); 
	//To get city,state,zip
	cout << "Please enter the city, state and zip code." << endl;
	getline(cin,tele[index].cityStateZ); 
	//To get phone
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	cin >> tele[index].phone; 
	
	    
	cout << endl;
	cout << endl;

for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
	}

return size;




}

//Option D function 
void optionD( infoType tele[], int& size )
{	
	int index = 0;
	bool found = false;
	string dPhone;
	
	cout << "You have selected option D " << endl;
	cout << "Please enter the phone number of the record to be deleted." << endl;
	cout << "(Example, 555-555-5555)" << endl;

	cin >> dPhone;
	
	while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
			cout << "The entry to be deleted is " << endl;
			cout << tele[index].lname << endl;
			cout << tele[index].fname << endl;
			cout << tele[index].streetAdd << endl;
			cout << tele[index].cityStateZ << endl;
			cout << tele[index].phone << endl;
			cout << endl;
		// To "remove" element.
			--size;
			tele[index] = tele[size];
	
			found = true;
	     }
			else
			index++;
  // Then entry has been removed message? if not found message return to menu?
	}
	
}

//Function to update directory
void optionU( infoType tele[], int&size )
{	
	//Variables
	int index = 0;
	string lname = "";
	string newLname = "";
	string newFname = "";
	string newAdd = "";
	string newCSZ = "";
	string newPhone = "";
	int choice = 0;
	bool found = false;
	
	//Option U function message
	cout << "You have selected option U." << endl;
	// To get record.
	cout << "Please enter the last name of record to be updated." << endl;
	//Get last name
	cin >> lname;
	//convert first letter to upper
	lname[0] = toupper(lname[0]);
	//Find record
	while( index < size && !found )
	{
		if(tele[index].lname.compare(lname)== 0)
		{
			cout << "The entry to be updated is " << endl;
			cout << tele[index].lname << endl;
			cout << tele[index].fname << endl;
			cout << tele[index].streetAdd << endl;
			cout << tele[index].cityStateZ << endl;
			cout << tele[index].phone << endl;
			cout << endl;
		found = true;
		}
		else if(!found)
		index++;
  //an if statement and return to menu if not found?
	}
	//User prompt
	cout << "Please select from one of the following" << endl;
	cout << "1. Update last name." << endl;
	cout << "2. Update first name." << endl;
	cout << "3. Update street Address." << endl;
	cout << "4. Update city, state, and zip code." << endl;
	cout << "5. Update phone number." << endl;

	
	//Note put entire update inside loop incase user wants to update more then once

	// YOU NEED GET FOR WHOLE LINES OF INPUT!
  
	// To get selection	
	cin >> choice;
	switch(choice)
	{
	case 1: cout << "Please type the new last name. " << endl;
		cin >> newLname;
		tele[index].lname = newLname;
		break;
	case 2: cout << "Please type the new first name. " << endl;
		cin >> newFname;
		tele[index].fname = newFname;
		break;
	case 3: cout << "Please type the new  street address" << endl;
		cin.get();
		getline(cin,newAdd, '\n'); 
		tele[index].streetAdd = newAdd;
		break;
	case 4: cout << "Please type the new  city, state, and zip code." << endl;
		cin.get();
		getline(cin,newCSZ); 
		tele[index].cityStateZ = newCSZ;
		break;
	case 5: cout << "Please type the new phone number."<< endl;
		cout << "(Example, 555-555-5555) " << endl;
		cin >> newPhone;
		tele[index].phone = newPhone;
		break;
	default: cout << "Invalid selection. " << endl;

	
	}

	cout << tele[index].lname << endl;
	cout << tele[index].fname << endl;
	cout << tele[index].streetAdd << endl;
	cout << tele[index].cityStateZ << endl;
	cout << tele[index].phone << endl;
}

int main()
{		//Note use enum in main have functions return something and assign a value to enum(see hangman)
	ifstream fin("input.txt");
	
    int newSize = 100;
    infoType tele[SIZE];
		
   
	readData(fin, tele, newSize );
	optionU(tele,newSize );
	newSize = optionA(fin, tele,newSize);
	//optionD(tele,newSize);
	
	
 //displayMenu();
 
  system("pause");
 
 return 0;

}

Thanks
change line 55 to
int index = size;
and delete lines 58-63

or just use size instead of index in your optionA function

and dont forget to increase size in your optionA function
Last edited on
Didn't work dude. This is what I changed it to and when I did output to see what was going on I the array with nothing added.This what I changed it to.

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
//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
	int index = size;
	
	//Message for option A function
	cout << "----You have selected option A---- " << endl;
	cout << endl;
	//To get first name
	cout << "Please enter the last name. " << endl;
	cin >> tele[index].lname;
	//To get last name
	cout << "Please enter the first name. " << endl;
	cin >> tele[index].fname;
	//To get address
	cout << "Please enter the street address. " << endl;
	cin.get();
	getline(cin,tele[index].streetAdd); 
	//To get city,state,zip
	cout << "Please enter the city, state and zip code." << endl;
	getline(cin,tele[index].cityStateZ); 
	//To get phone
	cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
	cin >> tele[index].phone; 
	
	    
	cout << endl;
	cout << endl;

for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
	}

return size ++;

Maybe I did it wrong?
first of all you can make that function void and just write
size++; without return

second. it worked for me.. but it only works with your struct, the text file is not edited. you have to check it with a debugger in order to see that or add some printing function.
Last edited on
OK I'm not saying that it didn't I'm saying its not working for me :)
I made it void .

I'm sorry I don't understand you
but it only works with your struct, the text file is not edited. you have to check it with a debugger in order to see that or add some printing function.

I don't know this.

Wait wait...OK back as I was writing this I was like this makes no sense. Then I said duh the size it incremented after I was trying to display something.

So another dumb question is I don't need that loop before I add to the array because it will automatically add to the open spot at the end?
Thank yous sir!!
OK one more someone helped me with the chunk of code here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while( index < size && !found )
	{
		if(tele[index].phone.compare(dPhone)== 0)
		{
			cout << "The entry to be deleted is " << endl;
			cout << tele[index].lname << endl;
			cout << tele[index].fname << endl;
			cout << tele[index].streetAdd << endl;
			cout << tele[index].cityStateZ << endl;
			cout << tele[index].phone << endl;
			cout << endl;
		// To "remove" element.
			--size;
			tele[index] = tele[size];
	
			found = true;
	     }
			else
			index++;



After the remove element I'm trying to understand that little section .
So you decrement the size then you assign everything to tele[index] but since the size is decremented you are now missing one. But my question is how does it know which one is to be deleted. I know I find it above the index but how does
tele[index] = tele[size]; accomplish it? Maybe thats a dumb question but I'm willing to risk it. Thanks again



1)
you dont need that loop, since you are already using newSize (implemented in your main()) to save the index of the first empty element. this will work, as long as you increase newSize if you add something and decrease it if you remove something.

2) the code you posted:
in this part you replace the element index with the element size.
index is the element you want to delete, you find it in your if statement.
size is the the first empty element, so the next element after the last entry in your struct.

what you do now is, you find the entry you want to replace and save its index in the variable index.
then you decrement size with "size--;" in order to get the last element.
(remeber size was pointing at the element after the last one, so size--;)
in the next step you overwrite the element index with the element size, so now you have the information of the element size 2times in your struct. but since you decremented size, this doesnt matter. size is pointing at the last element now, which is just being handled like the next empty element anyways. so the next time you add something, this element simply gets overwritten.


if you want to delete the element index and also keep the order in your struct, you need to do the following:
- find the element you want to delete and save its index
- replace element[index] with element[index+1] in a for-loop starting at index until you reach the element[size]
- decrease size
Last edited on
Thanks Darkmaster.


Topic archived. No new replies allowed.