help printing garbage

Sep 12, 2019 at 12:40am
I'm pretty sure my program is almost completely done, i've worked around this problem as long as i can. When i build it builds, when i print though it just prints garbage. commented out every single function call in my main function and left only a cout statement that doesn't print. any help would be appreciated, i have no idea what to change.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;


struct listType
{
    string last = "last";
    string first = "first";
    int id = 0;
    listType *link;
};

void createList(listType *&head, listType *&tail)
{
    head = new listType;
    tail = new listType;
    head -> last = "AAA";
    head -> first = "AAA";
    head -> id = 0;
    tail -> last = "zzz";
    tail -> first = "zzz";
    tail -> id = 0;
    head -> link = tail;
    tail -> link = NULL;
    cout << "create list" << endl;
}

void insertList(listType *head, listType *tail, string last, string first, int id) 
{
    listType *prior, *knew, *next;
    knew = new listType;
    knew -> last = last;
    knew -> first = first;
    knew -> id = id;
    prior = head;
    next = head -> link;
   while (next != tail && knew -> id > next -> id)
   {
       prior = next;
       next = next -> link;
   }
   
    prior -> link = knew;
    knew -> link = next;
    }

void readEm(listType *head, listType *tail)
{
    listType *c, *prior, *next;
    string last, first;
    int id;
    
    string pos;
    ifstream inf("program2.dat");
    while(!inf.eof())
        {
            inf >> last >> first >> id;
            last.erase(last.find(","), 1);
            insertList(head, tail, last, first, id);
         cout << id << endl;
        }


}

bool emptyList(listType *head, listType *tail)
{
    return head->link == tail;
}

void traversEm(listType *head, listType *tail, ofstream &outf)
{
    listType *c;
    if (!emptyList(head, tail));
    {
        c = head->link;
    
    
        while (c != tail)
        {
                cout << c-> id << " " << c-> first << " " << c-> last << endl;
                outf << c -> id << " " << c-> first << " " << c -> last << endl;
                c = c->link;
        }
    
        //else
            {
                cout << "Empty List" << endl;
                outf << "Empty List" << endl;
            }
            system("pause");
    }   
}
 

    void deleteList(listType *head, listType *tail, string last, string first, int id)
{
    listType *c, *prior, *next;
    c = prior -> link;
    c -> last = last;
    c -> first = first;
    c -> id = id;
    prior = head;
    next = head->link;

    while (next != tail && c -> id != next->id)
    {
        prior = next;
        next = next->link;
    }
    
        prior->link = c;
        delete c;
    

}

int main()
{
    cout << "1st action" << endl;
    //string last, first;
    //int id;
    //ofstream outf("program2.ot");
    //listType *c, *head, *tail;
    cout << "before list" << endl;
    //createList(head, tail);
    //emptyList(head, tail);
    //readEm(head, tail);
    //traversEm(head, tail, outf);
    //insertList(555, Luke, Skywalker);
    //traversEm(head, tail, outf);
    //deleteList(800, Leia, Organa);
    //traversEm(head, tail, outf);
    return 0;
    system("pause");
}
Sep 12, 2019 at 2:01am
your pause is after the program ends. It is ignored.
That makes me ask "are you sure it did not print the 2 cout statements" vs "it printed them and closed the console in 1/10 of a nanoseconds and you missed it.

move the pause above return 0.
see if it works.
if it does not work, clean (delete ALL intermediate files) and fully rebuild all.
try again.
if that does not work, tell us.

once the 2 prints work uncomment functions one by one and test their modifications carefully to ensure correctness. That is, uncomment all the code to line 130 and then validate each line from 130 to 137 one by one.
Last edited on Sep 12, 2019 at 2:03am
Sep 12, 2019 at 2:06am
> When i build it builds
¿does it build without warnings?

> left only a cout statement that doesn't print.
make sure that you are actually compiling that code
do a clean build
execute your program from a terminal
run your program step-by-step through a debugger
Topic archived. No new replies allowed.