Linked List Help!! (Now have 23 errors with code)

Pages: 123
@OP A few small changes to make yours work. Just put the functions back in one at a time and test as you go.

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
#include <iostream>

int main() {
    char userAnswer{};
    
    do {
        std::cout << "\nA--Add a month of statistics\n"
        << "E--Edit a month of statistics\n"
        << "P--Print report\n"
        << "Q--Quit\n"
        << "Choice: ";
        
        std::cin >> userAnswer;
        userAnswer = toupper(userAnswer); // <--
        
        int m{0}; // <---
        switch (toupper(userAnswer))
        {
            case 'A':
                std::cout << "Enter month: ";
                std::cout << "Enter rainfall (in Inches): ";
                break;
            case 'E':
                std::cout << "Enter month: 0 or 1(error)";
                std::cin >> m;
                
                if (m==0)
                {
                    std::cout << "Enter new rainfall (in Inches): ";
                }
                else
                    std::cout << "ERROR: Invalid Month\n";
                break;
            case 'P':
                std::cout << "Total rainfall: " ;
                std::cout << "Average rainfall: " ;
                std::cout << "Highest rainfall: " ;
                std::cout << "Lowest rainfall: " ;
                break;
            case 'Q':
                std::cout << "Quitter\n";
                break;
            default:
                std::cout << "Bad answer\n";
        }
    } while ( userAnswer != 'Q');
    
    std::cout << "That's it!\n";
    return 0;
}
Even the best screw up code. I once had a summer job at the university getting some code the Professor of Programming had written to work! (which he then proceeded to present as his own the next term!) He was great on writing complicated flowcharts but less so on actually writing the code. I later found out his doctorate was in financial modelling using a computer...... He was a self-taught programmer. Great ideas man. Guess that's what got him his job.

what about a variable declared before the do loop; some calcs are done in the body; then that value is part of the while condition?


In this case the value used in the while condition is the value set in the body as it's defined outside the body. The confusion can come when a variable is defined before the do loop and also within the body. In this case the value of the variable before the do is used in the while condition.

1
2
3
4
5
6
int a = 1;

do {
    int a {};
    std::cin >> a;
} while (a > 0);


This is an infinite loop as the value of a defined within the do body end scope at the }. The value of a used in while is always 1 !

What we really want is something like this:

1
2
3
do (int a{}) {
    std::cin >> a;
} while (a > 0);


but also not yet in the standard. If we want this we have to do:

1
2
3
4
5
6
7
{
    int a {};

    do {
        std::cin >> a;
    } while (a > 0);
}


Last edited on
Hence int m{0}; // <--- with https://stackoverflow.com/questions/5685471/error-jump-to-case-label/5685578 as one example discussing it.
@OP
Needs to be completed but it doesn't bomb.
Also note that by decoupling the data from the list life is a little bit simpler.
It might be useful in making it clearer on that point to remove the name 'Rainfall' from the code/methods etc. The Data is simply a combination of an int (or double - an inch of rain is a lot of rain) and a string for the month name to improve flexibility and reusability.

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <string>

struct Data
{
    double number{0};
    std::string name;
    
    void setNumber(double aNumber){number = aNumber;}
    void setName(std::string aName){name = aName;}
    
    Data operator+ (const Data& dat)
    { return { dat.number + this->number, "SUM"}; }
    
    bool operator== (const Data& rhs) const
    { return this -> number == rhs.number; }
    
    bool operator<= (const Data& rhs)
    { return this -> number <= rhs.number; }
    
    bool operator>= (const Data& rhs)
    { return this -> number >= rhs.number; }
    
    friend std::ostream& operator<<(std::ostream& os, const Data& dat)
    { os << '(' << dat.number << ' ' << dat.name << ')'; return os; }
};

class List
{
private:
    struct node
    {
        Data item; // Store the data of each node
        node* next; // Will point to the next node in the list
    };
    
    typedef struct node* nodePtr;
    
    nodePtr head; // head of node
    nodePtr temp; // temprary node
    nodePtr curr; // current node
    
public:
    List();
    void addNode(Data);
    void deleteNode(Data);
    void printList();
    
    //void appendNode(Data);
    void displayList() const;
    Data getHighestRainfall() const;
    Data getLowestRainfall() const;
    Data getTotal() const;
};

List::List()
{
    head = nullptr;
    temp = nullptr;
    curr = nullptr;
}

void List::addNode(Data dat)
{
    nodePtr n = new node; // node pointer equals new node
    n->next = nullptr;
    n->item = dat;
    
    if (head != nullptr)
    {
        curr = head;
        while (curr->next != nullptr)
        {
            curr = curr->next;
        }
        curr->next = n;
    }
    else
    {
        head = n;
    }
}

void List::deleteNode(Data dat) {
    nodePtr delPtr = nullptr;
    temp = head;
    curr = head;
    
    while (curr != nullptr && !(curr->item == dat) )
    {
        temp = curr;
        curr = curr->next;
    }
    
    if (curr == NULL) {
        std::cout << dat << "was not in the list\n";
        delete delPtr;
    }
    else {
        delPtr = curr;
        curr = curr->next;
        temp->next = curr;
        if (delPtr == head) {
            head = head->next;
            temp = NULL;
        }
        delete delPtr;
        std::cout << "The value " << dat << "was deleted\n";
    }
}

void List::printList() {
    curr = head;
    while (curr != nullptr) {
        std::cout << curr->item << std::endl;
        curr = curr->next;
    }
}

Data List::getLowestRainfall() const
{
    nodePtr np = head;
    Data lowest = np->item;
    
    while (np)
    {
        if (np->item <= lowest)
            lowest = np->item;
        np = np->next;
    }
    
    return lowest;
}

Data List::getHighestRainfall() const
{
    nodePtr np = head;
    Data highest = np->item;
    
    while (np)
    {
        if (np->item >= highest)
            highest = np->item;
        np = np->next;
    }
    
    return highest;
}

Data List::getTotal() const
{
    nodePtr np = head;
    Data total = np->item;
    
    while (np)
    {
        
        total = total + np->item;
        np = np->next;
    }
    
    return total;
}

void List::displayList() const
{
    nodePtr np = head;
    
    while (np)
    {
        std::cout << "Element " << np->item;
        np = np->next;
    }
}

int main()
{
    // SETUP SOME DATA
    Data year_2021[]
    {
        {12, "Jan"}, {34, "Feb"}, {56, "Mar"}, {1,"Apr"}, {7,"May"}, {32,"Jun"},
        {7,"Jul"},{3,"Aug"},{1,"Sep"}, {5,"Oct"},{30,"Nov"},{7,"Dec"}
    };
    size_t SIZE = sizeof(year_2021)/ sizeof(Data);
    
    // CREATE AND POPULATE A LIST
    List rainfall_record;
    for(int i = 0; i < SIZE; i++){ rainfall_record.addNode( year_2021[i] ); }
    
    // SOME TESTS
    rainfall_record.displayList();
    std::cout << year_2021[2] + year_2021[4] + year_2021[5];
    std::cout << "Minimum: " << rainfall_record.getLowestRainfall();
    std::cout << "Maximum: " << rainfall_record.getHighestRainfall();
    std::cout << rainfall_record.getTotal();
    
    Data clone = year_2021[3];
    std::cout << clone << '\n';
    
    
    // MENU STUFF
    char userAnswer{};
    
    do {
        std::cout
        << '\n'
        << "A--Add a month of statistics\n"
        << "E--Edit a month of statistics\n"
        << "P--Print report\n"
        << "Q--Quit\n"
        << "Choice: ";
        
        std::cin >> userAnswer;
        userAnswer = toupper(userAnswer); // <--
        
        int m{0}; // <---
        Data temp;
        
        switch (toupper(userAnswer))
        {
            case 'A':
                std::cout << "Enter month: ";
                std::cin >> temp.name;
                
                std::cout << "Enter rainfall (in Inches): ";
                std::cin >> temp.number;
                rainfall_record.addNode(temp);
                break;
            case 'E':
                std::cout << "Enter month: 0 or 1(error)";
                std::cin >> m;
                
                if (m==0)
                {
                    std::cout << "Enter new rainfall (in Inches): ";
                }
                else
                    std::cout << "ERROR: Invalid Month\n";
                break;
            case 'P':
                std::cout << "FULL LIST: ";
                rainfall_record.displayList();
                std::cout << "  Total rainfall: " << rainfall_record.getTotal() << '\n';
                std::cout << "Highest rainfall: " << rainfall_record.getHighestRainfall() << '\n';
                std::cout << " Lowest rainfall: " << rainfall_record.getLowestRainfall() << '\n';
                break;
            case 'Q':
                std::cout << "Quitter\n";
                break;
            default:
                std::cout << "Bad answer\n";
        }
    } while ( userAnswer != 'Q');
    
    std::cout << "That's it!\n";
    return 0;
}



Element (12 Jan)Element (34 Feb)Element (56 Mar)
Element (1 Apr)Element (7 May)Element (32 Jun)
Element (7 Jul)Element (3 Aug)Element (1 Sep)
Element (5 Oct)Element (30 Nov)
Element (7 Dec)
(95 SUM)
Minimum: (1 Sep)
Maximum: (56 Mar)(207 SUM)(1 Apr)

A--Add a month of statistics
E--Edit a month of statistics
P--Print report
Q--Quit
Choice: a
Enter month: January
Enter rainfall (in Inches): 999

A--Add a month of statistics
E--Edit a month of statistics
P--Print report
Q--Quit
Choice: p
FULL LIST: Element (12 Jan)Element (34 Feb)Element (56 Mar)
Element (1 Apr)Element (7 May)Element (32 Jun)Element (7 Jul)
Element (3 Aug)Element (1 Sep)Element (5 Oct)
Element (30 Nov)Element (7 Dec)Element (999 January)  
Total rainfall: (1206 SUM)
Highest rainfall: (999 January)
 Lowest rainfall: (1 Sep)

A--Add a month of statistics
E--Edit a month of statistics
P--Print report
Q--Quit
Choice: Q
Quitter
That's it!
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.
Pages: 123