HELP! POS using text files

sorry for the many questions but I only post here once im stuck

i have 2 questions:
1. How do i display the values in the text file?(i know it uses loop but i have no idea how to)
2. How do i add the prices if it keeps changing?

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
35
36
37
38
39
  
    string product name;
    double price;
    ofstream item("example.txt");
    cout<<"Enter Product Name: ";
    cin>>product;
    cout<<"Enter Price: ";
    cin>>price;
    item<<product<<" "<<price;
      item.close();
      cout<<endl;

    cout<<"Do you want to enter another product?[y/n]:  ";
    cin>>choi;

    if(choi=="y" || choi=="Y" || choi=="n" || choi=="N")
    {
        pointCHARLIE:
            if(choi=="n" || choi=="N"){return choi;}

            string product;
            double price;
            fstream item;
            item.open("example.txt");
            item.seekg(0, ios::end);
       
            cout<<"Enter Product Name: ";
            cin>>product;
            cout<<"Enter Price: ";
            cin>>price;
            item<<endl<<product<<" "<<price;
            item.close();


                cout<<"\nDo you want to enter another product?[y/n]:  ";
                cin>>choi;

                if(choi=="y" || choi=="Y") {goto pointCHARLIE;}
                 if(choi=="n" || choi=="N"){return choi;}


this works..as for displaying the values i've seen several videos that uses arrays and loops but i can't seem to understand it..i just dont want to copy codes cuz its useless if i don't understand it
base on your code, i asume your text file has a certain pattern and looks like this:

apple 1.00
banana 0.75
mango 0.76
greenpeas 1.00


how to read based on the text file pattern:
1
2
3
4
5
6
7
8
9
10
11
12
ifstream myfile ( "MYFILE.txt" );

char product_name[10];
double price;

if ( myfile.is_open() ) {
    while ( myfile >> product_name ) {
        myfile >> price;
        cout << product_name << " ";
        cout << price << endl;
    }
} else return 0;


EDIT: sorry for wrong typos earlier
Last edited on
may i ask why is there a need to use an array and how would i know how much elements should i use?
Actually, you can use string instead and i don't recommend using an array with constant sized elements because it is inefficient, the value you will read can either extend the size or is lower than the allocated size, which will either cause errors and memory leak.


Use string instead in place of the char array:

instead of char product_name[10];, use string product_name

With string, you don't have to know how many elements you will have to declare
during initialization on the char array
Last edited on
i finally understand it..thanks for the help :)

one last thing how do i store each price every time it loops in this:

1
2
3
4
5
6
7
8
9
10
11
12
ifstream myfile ( "MYFILE.txt" );

char product_name[10];
double price;

if ( myfile.is_open() ) {
    while ( myfile >> product_name ) {
        myfile >> price;
        cout << product_name << " ";
        cout << price << endl;
    }
} else return 0;


so its like when the first time it loops it stores 1.00 to a variable/memory and when it loops again it stores 0.75 to another so when its time to choose a product to buy the prices will be stored and i just have to access those variable

apple 1.00
banana 0.75
mango 0.76
greenpeas 1.00



Last edited on
The original code has lots of repeated code. It could be simplified considerably:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    char choi;
    string product;
    double price;
    ofstream item("example.txt");
    
    do {
        cout << "Enter Product Name: ";
        cin  >> product;
        cout << "Enter Price: ";
        cin  >> price;
        item << product << " " << price << '\n';
        cout << "\nDo you want to enter another product?[y/n]:  ";
        cin  >> choi;
    } while (choi == 'y' || choi == 'Y');
    
    item.close();
    return choi;

(I defined a variable char choi - I didn't see a need for it to be a string here).

As for the latest question, you could read the contents of the file into an array or vector.

You might find it useful to define a structure or class like this:
1
2
3
4
5
class product {
public:
    string name;
    double price;
};


Then you could simply define an array or vector of products.
1
2
3
4
5
6
7
8
9
10
11
    ifstream fin("example.txt");
    vector <product> prods;
    product item;
    
    while (fin >> item.name >> item.price)
        prods.push_back(item);
        
    fin.close();
    
    for (unsigned int i=0; i<prods.size() ; i++)
        cout << (i+1) << " "<< prods[i].name << " " << prods[i].price << endl;

Output:
1 apple 1
2 banana 0.75
3 mango 0.76
4 greenpeas 1

Last edited on
Topic archived. No new replies allowed.