Hi there, i have an assignment, and i need to create a monthly expenditure. I have managed to get alot done, a fully functioning menu and all the functions. Im stuck when it comes to deleting an entry
I have been using structured arrays to store data, ive posted all my code, i thought it be easier for help, sorry for the massive post.
What my tutor suggested was to use a counter, and then save the counter also in a text file, so when i reopen the console, the counter doesnt reset to 0
Is there a simpler way to be able to delete one of these entries? each entry has an ID
I don't understand how a counter written to a file is going to help you. It looks like you are trying to redo what a std::vector already provides (you are copying every entry forward if something in the middle is deleted). Since you have an array, you can't really "erase" an entry but you could simply clear it. Does it matter if there is an unused entry in the middle? It seems to me that this program should allow for dynamic expansion of the array anyhow. You are currently limited to 100. I would recommend using a std::deque personally. if you have a std::deque then you can build the array of entries dynamically and simply call its member functions to add or delete entries. It will manage the memory for you and keep track of the size of the array. If you aren't ready to get into those classes yet, consider maintaining the array such that you can simply clear the entry and leave it in the middle. You could make it work that way as well, for starters. For this program to be useful, eventually it needs to be more dynamic.
By the way, thanks for posting neat code with tags and proper indenting. The indenting is a bit off in spots but the format of the post is better than many others.
This statement is wrong. You forgot the [5]. cout<<"Invalid input, please select again [1] [2] [3] [4]";
This is also wrong. The program may exit prematurely because you aren't including all of the possibilities in the do..while conditions. }while(userChoice != 1 || userChoice != 2);
The file i/o, in general, is still incoherent. I'm not sure what your plans are for that. Are you planning on reading the data back in to initialize the program? Reading the counter from a file isn't helpful at all unless you are planning on reading all of the previously entered records. Personally, I would output each record of the array to a binary file prior to exiting. In fact that would be an advantage of using vector instead of deque. Vector might be slightly less efficient if you are inserting and deleting lots of records in the middle but would maintain records in a contiguous block of memory that could easily be written to a binary file and easily read back in at the end. In that case you could develop a structure that contains a header (including the count of records) and a body where the body is the array of records. When the program starts, you read the file header first to determine how many records are present. Then you validate the file size based on how many records are present. Finally, you read the data straight into the array.
You seem very skilled in programming, to be honest, you have lost me on some stuff lol, i understand bits of what you are telling me, my tutor at uni mentioned dynamic memory allocation, but we havent been taught anything on the subject.
Let me try again. When your program initializes it reads a counter. It also builds the 100 element entries array which is not initialized. What good is it to read in a counter that only tells you how many things were in the array during the last execution of the program? The array is currently full of garbage. In order to make use of a counter you need to be able to re-initialize the array from a file as well. The counter by itself is useless. In fact it'll cause problems because you might think that there are 20 records when in reality none of them are even initialized.