jeudi 11 février 2021

Editing an initialized struct member using fstream C++

I am stuck in the code where I have initialized the struct member but I don't know how can I edit or delete it in a txt file. (Is this possible?) Since I am making a "pc part shop" about processors and motherboards.Any tips are appreciated. Also sorry if my code is not clean, I am beginner.

    #include<iostream>
    #include<string>
    #include<fstream>
    
    const int size = 5;
    
    struct Computer
    {
        string processor[size];
        string motherboard[size];
    };
    
    struct Prices
    {
        double proc_prices[size];
        double mboard_prices[size];
    };
    
    void display_proc(struct Computer comp, struct Prices price);
    
    int main(){
        int chosen_proc, chosen_moth;
        Computer computer = {
            {"1. Asus Prime A320M-K AMD AM4 uATX MotherBoard with LED lighting, DDR4 3200MHz, 32Gb/s M.2, HDMI, SATA 6Gb/s, USB 3.0","2. MSI H410M-A PRO LGA 1200 Supports 10th Gen Intel Core and Pentium Gold / Celeron processors"}, // processsor array init
            {"1. Intel Pentium Dual Core G2030 3.0Ghz 3MB Cache LGA1155 22nm Processor", "2. Intel Core i3-7100 Processor (3M Cache, 3.90 GHz)"} // motherboard array init 
        };
        Prices all_prices = {
            {2499, 5999},
            {2999, 3999}
        };
        system("Color 0A"); // this is just a design only i'm testing it
        
        ofstream proc1("processor.txt", ios::app);
        ofstream mother1("motherboard.txt", ios::app);

    cout <<"Choose your Processor!" << endl;
    if(proc1.is_open()){  
        display_proc(computer, all_prices);
        proc1.close();//file close  
    }  
    else{  
        cout<<"Error in file opening"<<endl;  
    } 
    cout <<"Enter a number: ";
    cin >> chosen_proc;


        }
void display_proc(struct Computer comp, struct Prices price)
{
    for(int i = 0; i < size; i++)
    {
        cout << comp.processor[i] << "  "  << endl
         << "Price: Php"<< price.proc_prices[i] << endl;
         
    }
}

Aucun commentaire:

Enregistrer un commentaire