Problems with Struct being called from other functions

So my code has a struct called Storage that first appears in the function void List::set(int no);

For test sake I have it displaying the results inside that same function, where it works fine. The problem comes in when i try to bring with me the same Storage inside another function (after using it to set integers and strings) the output comes out whacky.
Both are bolded . the first one to appear works fine(but i do not want it inside this function) but the second one (which is inside a display function, is exactly where i want it but it does not work...

How can i fix this?


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
#include <iostream>
#include <string.h>
#include <ctype.h>

using namespace std;
//#include "List.h"

struct Line {
  private:
    int lineNumber;
    char lineString[6];
  public:
    bool set(int n,const char* str);
    void display() const;
};

struct List {
  private:
    int  lineNumber;
    char lineString[];
    int  MAX_LINES;
  public:
    void set(int no);
    void display() const;
};

int main() {
    int no;
    List list;

    cout << "List Processor\n==============" << endl;
    cout << "Enter number of items : ";
    cin  >> no;

    list.set(no);
    list.display();
}

bool Line::set(int n, const char* str){
    int i;
    if (n >= 1){
        lineNumber = n;
        for (i=0;i<6;i++){
                lineString[i]=str[i];
                }
        lineString[6]='\0';
//      cout << lineString << endl;
        return true;
        }
    else if (n <= 0)
        return false;
}

void Line::display()const{
cout << "LINE: " << lineNumber << " STRING: " << lineString << endl;
}

void List::set(int no){
  Line Storage[no];
  int i =0, x;
    while (i!=no){
        cout << "Enter line number : ";
        cin  >> lineNumber;
        cout << "Enter line string : ";
        cin  >> lineString;
        x = Storage[i].set(lineNumber, lineString);
        if (x == 1){
            i++;
        }
    }
MAX_LINES = no;
    i=0;
    while (i!=MAX_LINES){
        Storage[i].display();
        i++;
    }
}

void List::display() const{
   Line Storage[MAX_LINES];
   int i=0;
   while (i!=MAX_LINES){
        Storage[i].display();
        i++;
   }
}
     
Looks like the problem is scope.

You declare Storage inside the List::set() scope, so that means once the function ends, the variable will go out of scope.

You can't reference it outside of the function.

There are lots of ways of getting around this -- declaring the variable outside of the function, making storage a pointer to the object array instead, or passing a pointer to the function (which is related to the first choice).
Last edited on
how would i make storage a pointer to the object array instead?
Topic archived. No new replies allowed.