only function calls in main

My code shouldn't have any calls in the main other than function calls. However, I keep on getting errors when I try. How should I properly do it?

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
  
#include <iostream>
#include <iomanip>
#include <assert.h>
#include <stdlib.h>

using namespace std;

struct Structure;
typedef Structure * structure_pointer;
struct Structure
{
    int f1;
    int f2;
    structure_pointer f3,f4;
};
structure_pointer get_structure(int n, char ch);
void print_structure(structure_pointer p);
void tranverse_structure(structure_pointer p);
void create_linked_structure(structure_pointer p);
void delete_linked_structure(structure_pointer p);
void print_main_structure(structure_pointer p);
int main()
{
    structure_pointer head,p,q;
    head = new Structure;
    head = get_structure(1,'a');
    q=head;
    for(int i=2; i<=10; i++)
    {
        p= get_structure(i, char('a' + -1));
        q-> f3 = p;
        q= q-> f3;
    }
    print_main_structure(head);
    
    return 0;
}

structure_pointer get_structure(int n, char ch)
{
    structure_pointer temp = new Structure;
    assert (temp != NULL);
    temp -> f1 =4;
    temp -> f2 = ch;
    temp -> f3 = NULL;
    temp -> f4 = NULL;
    return temp;
}

void print_structure (structure_pointer p)
{
    cout << endl;
    cout << "f1 = " << setw(2) << (*p).f1 << "f2 = " << p -> f2;
    
    if (p-> f3 !=NULL)
        tranverse_structure (p->f3);
}

void tranverse_structure (structure_pointer p)
{
    print_structure ( p);
    if(p -> f3 != NULL)
        tranverse_structure(p-> f3);
}

void delete_linked_structure(structure_pointer p)
{
    structure_pointer head,q;
    tranverse_structure(head);
    p = head;
    
    for(int i=1; i<=10; ++i)
    {
        q=p;
        p = p-> f3;
        delete q;
        
    }
}
void create_linked_structure(structure_pointer p)
{
    structure_pointer head,q;
    tranverse_structure(head);
    p = head;
    
    for(int i=1; i<=10; ++i)
    {
        q=p;
        p = p-> f3;
    }
}

void print_main_structure(structure_pointer p)
{
    cout << endl;
    cout << "Tranversing the whole structure " << endl;
    tranverse_structure(p);
    cout << endl;
    cout << "Deleting the whole structure" << endl;
    delete_linked_structure(p);
    
}
What error is happening? Can you be more specific?

You should compile with warnings enabled. On g++ (GCC) this can be done with -Wall.
Visual Studio has warning levels available in its options.

 In function 'void create_linked_structure(structure_pointer)':
83:28: warning: variable 'q' set but not used [-Wunused-but-set-variable]
 In function 'void delete_linked_structure(structure_pointer)':
70:30: warning: 'head' is used uninitialized in this function [-Wuninitialized]
 In function 'void create_linked_structure(structure_pointer)':
84:30: warning: 'head' is used uninitialized in this function [-Wuninitialized]


"Head" is uninitialized, so when you assign it to be p, p's value is also uninitialized, so you are trying to dereference some junk value, and your program is probably crashing or doing some other weird behavior. This will happen during your call to delete_linked_structure.

1
2
    head = new Structure;
    head = get_structure(1,'a');

Not really an issue (besides being a clear memory leak), but note that here you're assigning head a value, but then immediately overwriting it with the call to get_structure. I would just delete the first line.

Also, you're not calling it, but if you want create_linked_structure to actually do something beneficial, I suggest having it return the head pointer.
Last edited on
For now, I just want someone to help me only have function calls in main.
Okay then, like I said, you should call create_linked_structure and have it return a structure_pointer instead, and also you should change it to match the logic existing in main.

Then, call it like this, replacing lines 26 to 34 with:
head = create_linked_structure();

Change prototype, as well:
structure_pointer create_linked_structure();

The function itself:
1
2
3
4
5
6
7
8
9
10
11
12
13
structure_pointer create_linked_structure()
{
    structure_pointer head, q, p;
    head = get_structure(1, 'a');
    q = head;
    for (int i=2; i<=10; i++)
    {
        p = get_structure(i, char('a' + -1));
        q->f3 = p;
        q = q->f3;
    }
    return head;
}

Something like that (not tested).
Last edited on
It meets your requirements lol,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int myFakeMain()
{
    structure_pointer head,p,q;
    head = new Structure;
    head = get_structure(1,'a');
    q=head;
    for(int i=2; i<=10; i++)
    {
        p= get_structure(i, char('a' + -1));
        q-> f3 = p;
        q= q-> f3;
    }
    print_main_structure(head);
    
    return 0;
}

int main() {
   return myFakeMain();
}


But you haven't addressed any of the issues from your previous threads, like the gratuitous memory leak, and the meaningless names you use all over the place.

I mean, you use _structure or structure_ everywhere, and in doing so, you render it meaningless.

Last edited on
Salem's answer isn't bad, though. Turn old main into a function. Call it with new main. Clean up old main if you need to (here, make it void and remove return, move the for loop into a function or method (pass p,q, whatever it needs), and its done).
Last edited on
Topic archived. No new replies allowed.