Linked List and Queue Problem w/ (Possibly) a Continue Statement

So I have a program that has linked lists and queue. In the text file the S is a sale, the P a promotion and R a receipt. What I want my program to do is to insert the R widgets into a queue and if the the next item is an S widget to subtract the the number of the sale from the number in the queue. If the number in the queue becomes negative then I want the program to pop the front of the queue and take that negative number and subtract from the next item in the queue. The problem comes up when I get to the second sale. For some reason I think the continue; statement is going to the next iteration of the for loop instead of the while loop and setting everything out of whack. What is going wrong?
Thanks.


node.h
1
2
3
4
5
6
7
struct node {
    char type;
    int num_per;
    double price;
    node *head;
    node *next;
};


data.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
R 150 1.00
R 130 2.00
S 145 
R 50 2.50
S 75
S 180
R 50 4.00
R 30 5.00
R 40 5.50
P 30
S 50
S 30
R 50 6.00
R 265 10.00
S 60
P 50
S 100
S 70
S 175
R 40 14.00
R 75 15.00
S 110
R 30 16.00
R 40 18.00


main.cpp
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
96
97
98
99
100
101
102
103
104
105
 #include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <queue>
#include "node.h"
using namespace std;
fstream fin;
int main()
{
    queue <double> pri;
    queue <int> num;
 
    fin.open("data.txt");
    
    node *first = new node;
    
    first->head = first;
    first->next = NULL;
    first->price = 0.0;
    
    fin >> first->type >> first->num_per;
    if (first->type == 'R')
       fin >> first->price;
    
    node *prev = first;
    
    while(!fin.eof()){
        node *newNode = new node;
        newNode->head = prev->head;
        newNode->next = NULL;
        newNode->price=0.0;
        fin >> newNode->type >> newNode->num_per;
        if (newNode->type == 'R')
            fin >> newNode->price;
        prev->next = newNode;
        prev = newNode;
    }

    fin.close();
    
    node *q = first;
    
    double price = 0.0;
    int l_over = 0, length = 0;
    
    while (q){
        if (q->type == 'R'){
            cout << "Receipt- # of widgets: " << q->num_per << " price: $" << q->price << endl;
        }
        q = q->next;
        length++;
    }
    
    cout << endl << endl;
    
    int promo = 0;
    node *z = first;
    for (int i = 0; i < length; i++){
        
        if (z->type == 'R'){
            pri.push(z->price);
            num.push(z->num_per);
        }
        else if (z->type == 'P'){
            cout << "Promotion card read! Next two customers get a " << z->num_per << "% discount!" << endl << endl;
            promo++; //I haven't inserted the code of what happens when promo hits a certain number
        }
        
        else if (z->type == 'S'){
            while(!num.empty()){
                cout << "*--------------------*" << endl;
                price = pri.front() + (pri.front()*(.3));
                if (z->num_per > num.front()){
                    cout << num.front() << " widgets sold at $" << price << endl;
                    cout << "Sales: $" << (num.front()*price) << endl << endl;
                }

                if (l_over > 0){
                    num.front()-=l_over;
                    l_over = 0;
                }
                else { num.front() -= z->num_per; }
                
                if (num.front() <= 0){
                    l_over = abs(num.front());
                    // cout << "remainder of " <<  abs(num.front()) << " widgets unavailable!" << endl;
                    pri.pop();
                    num.pop();
                    continue;
                }
                cout << z->num_per << " widgets sold at $" << price << endl;
                cout << "Sales: $" << (z->num_per*price) << endl << endl;
            
            }
            if (num.empty()) cout << "Widgets unavailable!" << endl << endl;
        }
    
        z = z->next;
        
}
    

    return 0;
}


EDIT: I was testing the with a break and continue and forgot to switch them out again. Now it has the continue.
Last edited on
But... You do not have continue anywhere in your snippet.
I don't see any continue statements, but anyway,
For some reason I think the continue; statement is going to the next iteration of the for loop instead of the while loop
that's not possible.
Now I fixed it. As for the continue statement, I know it shouldn't be possible. It could be the line 74 if statement but so far my tracing hasn't come up with problems yet.
On each iteration of for loop only one value gets pushed in num queue
Before continue you have num.pop(); which removes only element from it. So continue will force next iteration, !num.empty() returns false and while loop terminates.
Rest of for loop is executed and next iteration begins.
Now I believe that the problem lies with the condition of the while loop because in the while loop !num.empty() means that it will keep printing until the the queue is empty. So far of the conditions I used worked.
So I took out the continue statement because it just wasn't working. I also changed the conditions of the while loop a little. I almost have it but it's still not working 100%. Any suggestions?
Thanks.

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
  else if (z->type == 'S'){
            cout << "*--------------------*" << endl;
            cout << z->num_per << " widgets sold" << endl;
            z->type = 'I';
            while(z->type == 'I' && l_over >= 0){
                if (z->num_per > num.front()){
                    cout << num.front() << " widgets sold at $" << price << endl;
                    cout << "Sales: $" << (num.front()*price) << endl << endl;
                }
            
                price = pri.front() + (pri.front()*(.3));
                if (l_over > 0)
                    num.front()-=l_over;
                else
                    num.front() -= z->num_per;
                
                if (num.front() < 0){
                    l_over = abs(num.front());
                    // cout << "remainder of " <<  abs(num.front()) << " widgets unavailable!" << endl;
                    pri.pop();
                    num.pop();
                    break;
                }
                cout << z->num_per << " widgets sold at $" << price << endl;
                cout << "Sales: $" << (z->num_per*price) << endl << endl;
            }
            if (num.empty()) cout << "Widgets unavailable!" << endl << endl;
        }
Topic archived. No new replies allowed.