Sorting? Or Searching

Pages: 1... 567
And, by posting the code I can finally see exactly what it is you're trying to do.
This makes helping much easier.

It appears to me that when this program runs, it should:
1) Read all data for Details objects into an array of Details objects from existing file "test.txt".

2) Allow viewing of this data and some search features. All this works with the Details objects loaded from the file.

3) Allow more Details objects to be added by the user. All old Details data plus the newly entered Details should be saved to "test.txt".

About the current problems:
You are calling SaveData() each time a new Details data is entered.
It would be easier to keep adding more Details to the existing array until the user has finished entering Details data. You would then write all of the Details data at once to an empty file.
This requires learning about one more new item.
std::vector is used when an array size is indeterminate, such as here because we don't know in advance how many new Details data the user will be entering. Basic usage of std::vector is easy and may be the key to simplifying your file I/O task.

You could do it instead by appending each new Details data to the existing file (by opening it in this mode), but then newly entered Details wouldn't be stored in memory (data is in the file only), so the user couldn't view or edit newly entered Details, nor would they be included in any searches.

So, I think that storing your Details in a std::vector (instead of an array) is the way to go here. std::vector grows in size automatically to accommodate the addition of more elements. It's really quite slick.

EDIT: I wonder what the thread length record is here in the beginners forum?
Last edited on
im not sure if I can use std vector ( we haven't learnt them) however do they come under the following headers?


#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>

if so I can use it?

and yes lol this thread is definetly very long and Im sure we have gone off topic a lot too :D

also what is meant by appending?
Last edited on
I guess you can't use std::vector then.
also what is meant by appending?

A file can be opened for appending (add data) instead of for overwriting existing data. I believe that Chervil mentioned something about this several pages ago.
yes he sent me a link which ive gone through .

How should I proceed then without using vectors
You could:

1) Declare a static array of Details (like you are doing) but make it large enough to contain some "maximum allowed" number of Details (including the existing ones in test.txt).

2) That's about it. Dynamic allocation would be cumbersome (due to possible repeated resizing) and many here would advise not doing dynamic allocation with raw (dumb) pointers (my beloved method) at all... EVER.
It is involved to do it properly.

Without vectors I think it will be hard to develop this program to do everything you are trying to do in it.
I think we covered append already, but one more word, it just means add onto the end. Think of the appendix of the book, it is something added to the end of the book. In the case of the file, when new data is added to the of the file like this, it means the existing content remains intact, the file just grows larger so as to hold the old+new data.

----------------------------------------------------------

Dynamic allocation (and possible repeated resizing) is in effect doing the same task as a vector, but is more cumbersome, and less straightforward.

On the other hand, a dynamically allocated array can usually be bigger than a statically allocated one. But at this stage I don't think it should be a concern. What I'd suggest is allocating a generously sized array, say 100 or 1000 elements. It will be necessary to use a counter to keep track of exactly how many elements are currently in use.

There are various aspects of the program which currently use a fixed number such as 10, but really should allow for a variable size.

Take one example, function LoadData(). This is called from main() with a size of 10 specified. But I would think the data file itself determines how many elements there are. The function should keep reading from the file and storing in the array until there is no more data (or until it reaches the absolute limit of say 1000). During this process, a count must be maintained, so the number of array elements used is known.

Similarly when writing the output to the file, the entire contents of the array can be written out (based upon the count which holds the number of elements actually used). If this method is adopted, there is no need to use append after all. The reason being, we hold the entire contents of the old file in the array, as well as any new elements we may add. Therefore it is safe to overwrite the existing file.

Though for safety, during program development, you may want to keep the input and output files separate, until you are sure things are working. Or at the very least, make a backup of the input file before running the program. I do encourage caution when working with files, as it may take a long time to key in all the data, and an error in the program could wipe it all out in an instant.
Yes I had planned to make my current array of 10 much larger to like at least 1000+

Wanted to work with smaller amounts to make sure it worked.

but the problem im having currently is even though say I have 3arrays with data inside I enter 2 more so I should have a total of 5?


But what happens is it loads the old 3 pieces of data andi can view them but when I input it does skip the first 3 but when it saves to file it overrites all 3 with 2 if you understand my explanation
I think at present your program has some tasks which should be separate, but they are interwoven.

In particular, function SaveData(). This is doing two separate jobs:
1. adding new items to the array
2. writing the output file.

I think you need separate functions for that (you can re-use some of the existing code).
One function will only ask the user to input some data, and store it into the array.
The other function will only write out the entire contents of the array to the file.

You could put a check into the program so that, if the user has simply browsed or searched through the data, but made no changes, then there is no need to create an output file.

On the other hand, if the user has added (or modified) any data, the program could automatically call the function to create the new version of the file, just before the program is shut down.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void SaveData(Details Data[],int size){
	ofstream DataSave("test.txt");
	
			for(int i=0;i<size;i++){
				DataSave <<  Data[i].FirstName<<"\t";	
				DataSave <<  Data[i].LastName<<"\t";
				DataSave <<  Data[i].Age<<"\t";
				DataSave <<  Data[i].Email<<"\t";
				DataSave <<  Data[i].DoorNumber<<"\t";
				DataSave <<  Data[i].RoadName<<"\t";
				DataSave <<  Data[i].PostCode<<"\t";
				DataSave <<"\n";
			}
				DataSave.close();


}


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
void DataEntry(Details Data[],int size){
int x;
int i;
i=0;
		
cout << "How Many Entries Would You Like to add";
cin >> x;
for(int j=0;j<x;j++){
	if ( Data[i].Age == 0){
		cout << "First Name: ";
		getline(cin, Data[i].FirstName);
		cin.ignore(256,'\n'); 
		cout << "Last Name: ";
		getline(cin,Data[i].LastName);
		cout << "Age(Please Enter an Integer(Number)): ";
		cin >>  Data[i].Age;
		cin.ignore(256,'\n'); 
		cout << "Email: ";
		getline(cin,  Data[i].Email);
		cout << "Door Number(Please Enter an Integer(Number)): ";		
		cin >>  Data[i].DoorNumber;	
		cin.ignore(256,'\n'); 
		cout << "Road Name: ";
		getline(cin,Data[i].RoadName);
		cout << "Post Code(NOTE:Please use Lower Case): ";
		getline(cin,Data[i].PostCode);				
		}		
	else{
		i++;
		j--;
		cout << "FULL Moving on to Next Array";
	}
}	
}


like that right?
IT WORKS NOW !!!!


also could I use pointers to the size of the array as declared

eg I declare data[10]


instead of having to change all the 10s that come after for the function calls (size) it will just be 10 for all . and if I change to 100 it will be a 100 for all
That's roughly the right idea, but it needs some refinement.
Savedata() is about right - assuming a suitable value of size is used when calling the function.

The other function receives size as an input parameter, but doesn't do anything with it. I would think there need to be two values, one the current size (how many elements of the array are actually in use) and the other being the maximum permitted size, that is how large is the array that was allocated.

Then do something like this at line 9
for(int j=size;j<x+size;j++){
that will ensure the new data input by the user will go in the next empty slot in the array.

In addition, before exiting from the function, the value of size will need to be updated to indicate how many elements are now used.

You could pass size by reference, or return it as the return value of the function.

The by-reference version would look like this:
void DataEntry(Details Data[],int & size, const int maxSize) {

I'm just sketching out ideas here, you are free to reject them and do things a different way.
Last edited on
I agree that it's better not to have the value '10' at many separate places in your program. One way would be to declare a constant,
 
const int maxSize = 100;
and use it where required, data[maxSize]

yeah im going to use size to make sure user doesn't inputanything larger than my array and if he does input something else.

Ive made max size work in my array thanks..


Ive got tonnes of other features to implement now. eg age sort etc. This I should be ok on as I can just build on what I have now thanks for all the help.

You will probably see me spamming this forum again coz of some measly error.

"In addition, before exiting from the function, the value of size will need to be updated to indicate how many elements are now used."

Why and how do I do this? is it necessary?

I mean its running fine at the moment? or do you mean so I know that all elements are full?
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
  int x = array[top];
      int i = top - 1;
      int j = bottom + 1;
      int temp;
      do
      {
            do      
            {
                   j - -;
            }while (x >array[j]);

           do  
          {
                  i++;
           } while (x <array[i]);

           if (i < j)
          { 
                  temp = array[i];    
                  array[i] = array[j];
                  array[j] = temp;
          }
      }while (i < j);     
      return j;

}


this is a quicksort partitioning program that ive been trying to understand however. To me it seems that I and j are pointing at non existing elements?

considering in my eyes

Top is meant to be array [0] and bottom[last aray]

I think I have definetly viewed this in the wrong way probably didn't understand this.



this


// top = subscript of beginning of array
// bottom = subscript of end of array
Topic archived. No new replies allowed.
Pages: 1... 567