Hi there somebody please help me to create a record file using c++..
The project is similar to a record. Ex. A product(goods) record. Where you can search for a certain product using the code number of the product, add product, remove product and view all the records.
the task is develop a similar application.

Dani AI

Generated

@Ancient Dragon has the right plan: define a record type, pick a simple file format, then build add/find/delete/list one by one. Parallel arrays make it easy to get mismatched data and brace errors like noted; use a single record type instead. Also, if your product code is unique, an associative container keyed by that code makes search and delete trivial; that echoes @Red Goose’s suggestion.

Here is a compact, working pattern using a struct and a std::map<int,Product> plus CSV persistence. Get this compiling first, then put your menu on top of the functions.

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <cstdlib>

struct Product { int code; std::string name; double price; };
typedef std::map<int, Product> DB;

void add_or_update(DB& db, const Product& p) { db[p.code] = p; }
bool erase_by_code(DB& db, int code) { return db.erase(code) > 0; }
const Product* find_by_code(const DB& db, int code) {
    DB::const_iterator it = db.find(code);
    return it == db.end() ? 0 : &it->second;
}
void print_all(const DB& db) {
    for (DB::const_iterator it = db.begin(); it != db.end(); ++it) {
        const Product& p = it->second;
        std::cout << p.code << "  " << p.name << "  " << p.price << "\n";
    }
}
bool save_csv(const DB& db, const std::string& file) {
    std::ofstream out(file.c_str());
    if (!out) return false;
    for (DB::const_iterator it = db.begin(); it != db.end(); ++it) {
        const Product& p = it->second;
        out << p.code << "," << p.name << "," << p.price << "\n";
    }
    return true;
}
bool load_csv(DB& db, const std::string& file) {
    std::ifstream in(file.c_str());
    if (!in) return false;
    std::string line;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::istringstream ss(line);
        std::string codeStr, name, priceStr;
        if (!std::getline(ss, codeStr, ',')) continue;
        if (!std::getline(ss, name, ',')) continue;
        if (!std::getline(ss, priceStr, ',')) continue;
        Product p; p.code = std::atoi(codeStr.c_str());
        p.name = name; p.price = std::atof(priceStr.c_str());
        db[p.code] = p;
    }
    return true;
}

Notes:

  • Deleting is just erase_by_code(db, code) followed by save_csv(...). No in-place file surgery needed.
  • If you need duplicates per code, use std::multimap<int, Product> or std::map<int, std::vector<Product>>.
  • Compile with warnings (-Wall -Wextra) to catch stray statements like 0; and brace mismatches early.

Recommended Answers

All 10 Replies

The first step is to create a structure or class for the record. After that you can determine the file format. Finally, write each of the functions that add, insert, modify and delete records. Write and test these just one function at a time so that you don't get overwhelmed with the complexity of the program.

The first step is to create a structure or class for the record. After that you can determine the file format. Finally, write each of the functions that add, insert, modify and delete records. Write and test these just one function at a time so that you don't get overwhelmed with the complexity of the program.

hod do i create a structure i mean the code.i didnt know where i should start or what to write..:(

hod do i create a structure i mean the code.i didnt know where i should start or what to write..:(

i will used 3 arrays( of the products,code number)

Here is an example of how to write a structure

The first step is to create a structure or class for the record. After that you can determine the file format. Finally, write each of the functions that add, insert, modify and delete records. Write and test these just one function at a time so that you don't get overwhelmed with the complexity of the program.

hi there this is my code my problem is how do i organize this..there's a lot of error..please help me..

#include<iostream>
using namespace std;

main()
{
//menu
//\t - tab
    //string CornedBeef,Ham,CornedTuna,BeefLoaf,Sardines;
    string ProductName[100];
    int Price[100];
    int ProductCode[100];
    char ans;



    do
        //{
        cout<<"\t     Welcome My CompAny "<<endl<<endl
            <<"\t     Pls Choose your transactions below"<<endl<<endl
            <<"\t      <S>Search a Product"<<endl<<endl 
            <<"\t      <A>Search The Price List"<<endl<<endl
            <<"\t      <B>Search The Code"<<endl<<endl
            <<"\t\t\t   Transactions: ";
       char Transactions;
       cin>>Transactions;

ProductCode[0] = 9702146;
ProductCode[1] = 9700201;
ProductCode[2] = 9702147;
ProductCode[3] = 9700202;
ProductCode[4] = 9702148;


ProductName[0] = "CornedBeef";
ProductName[1] = "CornedTuna";
ProductName[2] = "Ham";
ProductName[3] = "BeefLoaf";
ProductName[4] = "Sardines";

int r = 6;
int i = 6;
{
for(i=0;i<r;i++)
{
    cout<<"The ProductName with the ProductCode "<<" "<<" is "<<" "<<endl;
}
}
           
 
        if(Transactions=='s')
            {
                ProductName[0] = "CornedBeef";
                ProductName[1] = "CornedTuna";
                ProductName[2] = "Ham";
                ProductName[3] = "BeefLoaf";
                ProductName[4] = "Sardines";
                cout<<"The ProductName with the ProductCode "<<" "[i]<<" is "<<" "[i]<<endl;
                cout<<endl;

            }

        else if(Transactions=='a')
           {
           0;
           1;
           2;
           3;
           4;
            }

        else if(Transactions=='b')
            {
                ProductCode[0] = 9702146;
                ProductCode[1] = 9700201;
                ProductCode[2] = 9702147;
                ProductCode[3] = 9700202;
                ProductCode[4] = 9702148;
               cout<<endl;
                
            }
        else
            {
                ProductName[0] = "0";
                ProductName[1] = "1";
                ProductName[2] = "2";
                ProductName[3] = "3";
                ProductName[4] = "4";
                cout<<endl;
            cout<<endl;
            cout<<"\t\t\tProductName         : "<<"CornedBeef" <<endl
                <<"\t\t\tProductName         : "<<"CornedBeef"<<endl<<endl
                <<"\t\t\tProductName              :"<<"Ham"<<endl
                <<"\t\t\tProductName          : "<<"Sardines"<<endl
                <<"\t\t\tProductName           : "<<"BeefLoaf"<<endl<<endl
                <<"\t\tDo you want to continue? (y-yes)";
                cin>>ans;
                system("cls");
        }while(ans=='y');
        
        return 0;
}

What do you propose that lines 64-68 are doing?

You've got a mismatch with your braces, too.

I think you're best off doing some reading or tutorials first, plan out your code with pencil and paper, and then do the coding.

I'm sure std map does all that for you. If you need duplicates, use std multi map.

What do you propose that lines 64-68 are doing?

You've got a mismatch with your braces, too.

I think you're best off doing some reading or tutorials first, plan out your code with pencil and paper, and then do the coding.

what do you mean??

Which part was unclear?

Lines 64-68 don't do anything, it's technically a valid statement when you write "0;", but why would you?

The braces that you have in the code don't match up, so an opening brace "{" has to match a closing brace "}" somewhere.

There's no ambiguity in the last part. It's not a mean sentiment, I'm just being truthful with you, you are missing fundamental concepts that need to be learned or re-learned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.