Linked List Help !

I have a problem with my link list assignment

This is the question
c) Develop a program (based on the flow chart) that uses a linked list data structure to keep the details of item and price. The program also must have two functions. One of the functions should accept arguments for the loyalty member data, while the other function accepts arguments for non-member customer data. Both functions should return the total payment.

Note: The program should check for input validation that does not accept negative numbers for any data.

and so far i manage to this

#include<iostream>
using namespace std;
void insert();
void member();
void nonmember();


struct node
{
char item;
int price;
node *next;
};
node *newptr;
node *head;
int main()
{
char x,rcn,cashcard;
int payment, yn;
cout<<"Customer has royalty member card or not ? ( 1 for Yes / 0 for No )"<<endl;
cin>>yn;
if(x=1)
{
member();
cout<<"Royalty Card Number : "<<rcn<<endl;
cout<<"Total Payment : "<<payment;
cout<<cashcard;

}
else if(x=0)
{
nonmember();
cout<<"Total Payment : "<<payment;
cout<<cashcard;

}
return 0;
}

void member()
{
char rcn , cashcard , Cash , Card;
int total , payment , one;
cout<<"Please input royalty card number : "<<endl;
cin>>rcn;
cout<<"Press 1 to add item into the card"<<endl;
cin>>one;
while(one=1)
{
insert();
}
total = 0;
while(newptr)
{
total += newptr->price;
node= newptr->next;
}
if (total<100)
{
payment=total;
}
else if (total>=100 && total<301)
{
payment = 0.95*total;
}
else
{
payment=0.9*total;
}
cout<<"Payment type ? ( Cash / Card )"<<endl;
cin>>cashcard;
if(cashcard==Cash)
{
cout<<"The balance is RM "<<endl;
}
else if (cashcard==Card)
{
cout<<"All Payment has been deducted from your bank account"<<endl;
}


}
void nonmember()
{
char one , cashcard ,Cash , Card;
int total , payment;
cout<<"Press 1 to add item into the card"<<endl;
cin>>one;
while (one==1)
{
insert();
}
total = 0;
while(newptr)
{
total+= node->price;
node = node->next;
}
payment=total;
cout<<"Payment type ? ( Cash / Card )"<<endl;
cin>>cashcard;
if(cashcard==Cash)
{
cout<<"The balance is RM "<<endl;
}
else if (cashcard==Card)
{
cout<<"All Payment has been deducted from your bank account"<<endl;
}


}



void insert()
{
node *newptr, *cur, *prev;
cur = head;
prev = NULL;

newptr = new node;
cout<<"\nPlease enter a item bought : ";
cin>>newptr->item;
newptr->next = NULL;
cin>>newptr->price;
newptr->next = NULL;

if(head == NULL){
head = newptr;
}
else{
// a loop to move prev and cur along the list and stop at appropriate place
// to insert a new element
while(cur != NULL && newptr->item > cur->item){
prev = cur;
cur = cur->next;
}
//insert in front of the list
if(prev == NULL){
newptr->next = cur;
head = newptr;
}
else {
//coding to insert at the middle or the end of the list
newptr->next = cur;
prev->next = newptr;
}
}
}

And its look like i get error on While under void member and nonmember.

what im trying to do there is sumarize all the price inside the linked list.
while one = 1 is an assignment and a bug.
Try to create a diagram on paper to see how variables are assigned
at each step

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
/*
 ------------------- 
 | char  item[100] |  (first node)
 | int   price     |
 | node* next  ----------> NULL    
 -------------------
           ^
           '-------------
                        |                
 -------------------    |
 | char  item[100] |    |  
 | int   price     |    |   
 | node* next  ----------        
 -------------------
           ^
           '-------------
                        |                
 -------------------    |
 | char  item[100] |    |  (top node)
 | int   price     |    |
 | node* next  ----------        
 -------------------
*/

struct node {
	char item[100];
	int price;
	node* next;
}

// Declare node pointers and initialize to NULL 
node *head = NULL;
node *cur = NULL;


void insert()
{
	node *newptr = new node;	// Create a new node	
	newptr->next = cur;		// It will point to the previous node
	cur = newptr;			// Current node is the new node
	head = cur;			// Keep track of the top node
	
	// Assign node data
	cout << "Purchase item: ";
	cin.getline(cur->item, 100);

	cout << "Price: ";
	cin >> cur->price;
}

void listItems()
{
	cur = head; 
	while(cur != NULL) {
		cout << "Item: " << cur->item << "\nPrice: " << cur->price << endl;
		cur = cur->next;
	}
}
Topic archived. No new replies allowed.