Read data from text file into linked list of type user defined and then perform specific operations on it

Pages: 12
Ok. So the split function is a likely candidate. Again, as I said above, use the debugger to trace the code to see where the issue is. That's what it's there for - to assist in the identification of run-time errors. All programmers need to get to grips with it. The sooner you start the better IMO.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include<fstream>
#include<string>
#define max 8 // define the max string
#include"LinkedList.h"
#include"Item.h"
using namespace std;
string strings[max]; // define max string 
int len(string str);  
void split (string str, char seperator);   


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	LinkedList<Item*> product;
	LinkedList<Item*> cart;
	
	string line;
	string num;  
	
		cout<<"Enter FileName"<<endl;
		string fn;
		getline(cin, fn);
		ifstream obj;
		int size=0;
		obj.open(fn.c_str());
		if(!obj)
			{
				cout<<"File not opened"<<endl;
			}
		else
			{
				cout<<fn<<" Opened"<<endl;
				while(obj)
				{
				    getline(obj,line);
				    size++;
					split(line,','); 
					Item *p1=new Item(strings[0],strings[1],strings[2],strings[3]);
					product.addToTail(p1);	
				}
				cout<<"size"<<size<<endl;
				obj.close();
				product.traversing();
				int opt;
				cout<<endl;
				do{
						cout<<"Press 1 to add items in cart"<<endl;
						cout<<"Press 2 to delete item from cart"<<endl;
						cout<<"Press 3 to display list of items again"<<endl;
						cout<<"Press 0 to generate bill"<<endl;
						cin>>opt;
						switch(opt)
						{
							case 1:
								cout<<"Enter product id:"<<endl;
								cin>>num;
								product.searching(num);
								
								
								break;
							case 2:
							    break;
							case 3:
								product.traversing();
							    break;
							case 0:
							    break;			
						}
				  } while(true);
			}	
}

  
// create custom split() function  
void split (string str, char seperator)  
{  
    int currIndex = 0, i = 0;  
    int startIndex = 0, endIndex = 0;  
    while (i <= str.length())  
    {  
        if (str[i] == seperator || i == str.length())  
        {  
            endIndex = i;  
            string subStr = "";  
            subStr.append(str, startIndex, endIndex - startIndex);  
            strings[currIndex] = subStr;  
            currIndex += 1;  
            startIndex = endIndex + 1;  
        }  
        i++;  
        }     
}  

Searching Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template<class T>
Node<T>* LinkedList<T>::searching(T item)
{
	if(head==0)//list empty
	{
		cerr<<"There is nothing to search \n";
	}
	else
	{
		Node<T> *ptr=head;
		Node<T>* loc=0;
	   while(ptr!=0)
	   { 
	
			if(item==ptr->getInfo()->getId())
			{
				loc=ptr;
				return loc;//loc contains the address of specific node where the value exist
			}
			ptr=ptr->getNext();
	   }
	   return loc;//loc contains 0 which indicates value not found
    }
}//searching 


Ok so now it is reading text file fine with a little bit of problem that it is reading last line 2 times and size on output is 9 instead of 8 and Kindly guide me I have searching function in my linked list and what logic will be used to add items in cart from product by getting ID and Quantity from user and Also deletion of item from cart by getting ID from user and at end to generate bill containing selected items, quantity, price per item and total amount to pay What logic will be used for all these?

Output
Enter FileName
grocery.txt
grocery.txt Opened
size9
ID Name Description Price
1 Bread BreakFast 50
2 Butter Used with bread 60
3 Milk Gives calcium 80
4 Eggs Gives protein 64
5 Carrot Vegetable 90
6 Tomato Vegetable 100
7 Apple Fruit 120
Apple Fruit 120

Last edited on
Change L35-37 to:

1
2
while (getline(obj, line))
{

Kindly tell me what logic will be used for these tasks

• If user presses 1, the system should ask user to enter item ID and quantity and should add it into the cart.
• If user presses 2, the system should ask for item ID and then it should delete that item from the cart.
• If user presses 3, list of items with item ID, name, description and price per item should be displayed again.
• If user presses 0, the system should generate bill containing selected item, quantity, price per item and total amount to pay.
1) Ask the use to enter ID and quantity. Then add to cart.

This is the basic logic. This should be part of the program design which is preferable to do before you start coding. You're trying to design after part of the program has already been coded. This is not really the way to do it. First design the program, then code from the design. Code in small parts and compile and test frequently. When you're coded/tested the whole design then you should have a working program that conforms to the requirements.

What don't you understand about this? How is cart represented? You seem to have it as a LinkedList of Item? I would contend that this isn't right. You have a struct for cart items something like:

1
2
3
4
struct cart_item {
    int id {};
    unsigned qty {};
};


and then have a LinkedList of this type:

 
LinkedList<cart_item> cart;


id would reference the Item id. So to add an item to cart, obtain the id and check that it's present in product. If not then display a message. If it is present then ask for quantity and verify input (eg not neg or 0 etc). Then add a new item to cart.

Similar for 2. Ask for id, check that it exists in the cart and if so ask for deletion verification and then erase from cart list.
Last edited on
but how will bill be generated because In bill I have to display bill containing selected item, quantity, price per item and total amount to pay.
Whereas I have created separate class for qty
Last edited on
You iterate the cart container to get id and qty. For each id you obtain the item details (price etc) from product container and do a calculation to get cart price per item (item price * qty). Sum all the cart prices to get the bill total.
And why are we creating struct why not class for cart_item?
Make it a class if you want. I was only giving you pointers for this.
Topic archived. No new replies allowed.
Pages: 12