When I call my print function inside of another function, it works. But when I try to call it in main, nothing prints out.

My code is supposed to get a text file called "contacts.txt", read in the contacts information into nodes, put those nodes into a linked list, remove duplicates, and print out the linked list.

My code is working all except for one thing - when I was testing my code in my readData function, it printed out the linked list that I had created using printContactList function. However, when I then tried to use my printContactList in main, nothing prints out. I've removed all other functions to make my code be as concentrated as possible. I would appreciate the help.

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
 #include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>

using namespace std;
typedef string ElementType;


class Node
{
private:
  ElementType first, mid, last, phone;
  Node *next;

  friend class List;
public:
  Node(ElementType f, ElementType mi, ElementType sur, ElementType ph){
        first = f;
        mid = mi;
        last = sur;
        phone = ph;
        next = nullptr;
  };

  Node* getNext(){
    return next;
  }

void printNode(){
  cout << first << " " << mid << " " << last << " " << phone;
}

bool sameAs(Node* q){
  if(first == q->first){
    if(mid == q->mid){
      if(last == q->last){
        if(phone == q->phone){
          return true;
        }
      }
    }
  }
}


};
typedef Node* NodePointer;

/*------------------------------------------------------*/

class List {
private:
  Node* first;

public:
List(){
  first =  nullptr;
}

NodePointer getFirst(){
  return first;
};

/*--------------------------------------------------------*/

void insert(ElementType fir, ElementType midd, ElementType las, ElementType phon, NodePointer pos){
  NodePointer pN;
 if(pos == first){
    pN = new Node(fir, midd, las, phon);
    pN->next = pos;
    first = pN;
  }

  else{
    pN = new Node(fir, midd, las, phon);
    pN->next = pos->next;
    pos->next = pN;
  }

}

/*--------------------------------------------------------------*/

void printContactList(){
    NodePointer pN;
    for(pN = first; pN != nullptr; pN = pN->next){
      pN->printNode();
      cout << endl;
    }
  }

};

/*--------------------------------------------------------------*/
void readData(List L){

  ElementType fi, mi, la, ph, ph1, ph2;
  ifstream fin;
  fin.open("C:\\Users\\owner\\Documents\\contacts.txt");

  while(  fin >> fi >>  mi >>la >> ph >> ph1 >> ph2){

  ph = ph + " " + ph1 + " "+ ph2;
  L.insert(fi, mi, la, ph, L.getFirst());
}

//L.printContactList();
}



int main()
{
List R;
    readData(R);
   R.printContactList();

    return 0;
}
You're passing "R" into readData, it makes a copy of "R" and then readData is going to work with a completely new variable.

There are two ways to fix it:

The way you probably wanted to do it:

void readData(List &L)

This small change should be made to line 96.


Or another possible way:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
List readData(List L){

  ElementType fi, mi, la, ph, ph1, ph2;
  ifstream fin;
  fin.open("C:\\Users\\owner\\Documents\\contacts.txt");

  while(  fin >> fi >>  mi >>la >> ph >> ph1 >> ph2){

  ph = ph + " " + ph1 + " "+ ph2;
  L.insert(fi, mi, la, ph, L.getFirst());
}

  return L;

//L.printContactList();
}

int main()
{
    List R;
    R = readData(R);
    R.printContactList();
    return 0;
}


Where you give it R, you still let it take the variable in by value, but then it returns the object and sets R equal to it.
Thank you!
Topic archived. No new replies allowed.