Using File Handling (insert, add delete) in an initialized struct

Hello, there. I am having trouble with my project. Long story short its a pc shop where you can buy processors, motherboards, etc. I have finished in the customer part but I am having trouble in the admin part where you can add, delete an item since I have a fixed array struct member. What can I do about this problem?

http://cpp.sh/9bckz Here is the code. It is quite long to post it in here.

Last edited on
Hello WeebTriestoCode,

I tried to compile your code, but get many errors.

To start with:
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
#include<iostream>
#include<iomanip>
#include<string>
#include<windows.h>
//#include<cstring>
//#include<conio.h>

#include<algorithm>
#include<fstream>

using namespace std;

const int size = 5;

struct Computer
{
    string processor[size];
    string motherboard[size];
    string gpu[size];
    string ram[size];
    string cpu_coolers[size];
    string pc_case[size];
    string power_supplies[size];
    string pre_builtPC[size];
};

For lines 17 - 24 which "size" are you trying to use??

I can not say for sure, but I have not seen any code that would require <cstring>. And for <conio.h> this can be used, but should be avoided as not everyone has it.

salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.



There are some other errors that need fixed.

Andy
Hello WeebTriestoCode,


I am having trouble in the admin part where you can add, delete an item since I have a fixed array struct member.

What can I do about this problem?


Provide more information.

What do you mean by "add" and "delete"??

What object do you want to work with for the "add" and "delete"?

Would this be the object defined in "main" or something different?

Then have a read at this: http://xyproblem.info/

Andy
can you use a <vector> instead of an array, and is this what you were trying to ask?
vectors wear a few hats but one of them is 'variable length array'
you can add, delete an item since I have a fixed array struct member.

There's your problem. You can't have it both ways. You need a variable length structure like a vector if you want to add new items.

Also, you have a lot of duplicate code. Instead of 8 display_xyz() functions, there should be just one with a parameter that is the collection of items to display. The same is true of the insert_data_xyz() functions.

Because you also open 9 files, I assume that the data is supposed to be in the files, not hard-coded in the program.

Also, you have separate arrays for item names and item prices. Since everything has a price, it seems to me that all of your data for components should be based on a structure like:
1
2
3
4
struct Item {
    string name;
    double price;
};

As an aside, this brings up an important point: when writing a program, it's usually a good idea to start by figuring out how to represent the data. Then you can work on the code that manipulates that data.

I don't want to suggest anything further without knowing exactly what you're trying to accomplish. Please post the assignment so we can see what you're trying to do. The code just shows us how you're trying to do it.
Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
void insert_data_power(ofstream &write) {
	Computer comp;
	Prices all_prices;
	for(int i = 5; i < 6; i++){
		cout << "\n\tEnter name of processor: ";
		getline(cin, comp.power_supplies[i]);
		cout << "\n\tEnter price of processor : ";
		cin >> all_prices.power_prices[i];
	}
	write << comp.power_supplies << endl;
	write << all_prices.power_prices<< endl;
}


First what is the purpose of that loop?

What is the point of all of those magic numbers (5 and 6)?

How many times do you think that that loop will actually execute?

What is the sizes of your arrays?

What are the valid index values of those arrays?

What happens if you try to access an array outside the bounds of the array?

What are you trying to print with those last two lines?

Hello WeebTriestoCode,

Not to say that I have fixed all the problems because I am not sure what is needed, but I have worked with the menus for awhile and came up with this:






                        <><><><><><><><><><><><><><><><><><><><><><><><><><><>
                        ||                                                  ||
                        || A CS127-8L PROJECT:                              ||
                        ||                                                  ||
                        ||                PC CUSTOMIZATION                  ||
                        ||                     AND                          ||
                        ||                   MANAGEMENT                     ||
                        ||                                                  ||
                        || Developed by: -----------                        ||
                        ||                 and                              ||
                        ||               ----------------                   ||
                        ||                                                  ||
                        ||                                                  ||
                        ||                                                  ||
                        <><><><><><><><><><><><><><><><><><><><><><><><><><><>


 PC Customization and Management System
 --------------------------------------

   1. Admin
   2. Customer
   3. Exit
    Select option: 1


       ADMIN INTERFACE
  ------------------------
  1. Add a PC Component
  2. Delete a PC Component
  3. Edit a PC Component
  4. Go Back <-
    Your option: 1


   What component would you like to add?
    1. PC case
    2. Motherboard
    3. Power Supply
    4. Processor
    5. GPU
    6. CPU Cooler
    7. Ram
    8. Pre-built PC
    9. Exit
      Enter Choice:


The blank lines between the parts means that the screen was cleared B4 the output.

Just an idea especially the last menu. It does give you an idea of what needs to be done.

Andy
Topic archived. No new replies allowed.