Pointer problems.

Hello. Need some somewhat urgent help, so me love you loong time if you can .. umm help.
So i have this bit of code here:

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
struct contbancar {
    char *nume,*numar,*tara,*idsec;
    int *varsta;
};

struct nod {
    nod *next;
    contbancar info;
};

void scriere(char *n) {
  int nr;
  int pointer;
  nod *prim;
  prim=creare_lista();
  strcat(n,".txt");  
  ofstream myfile (n);
  if(myfile.is_open()) {
    while (prim->next) {
        myfile<<*prim->info.varsta<<"ani ";
        myfile<<prim->info.nume<<" "<<prim->info.tara<<" "<<prim->info.idsec<<" "<<prim->info.numar<<endl;
        prim=prim->next;}
  }
  myfile.close();
}


Now, the function "creare_lista()" works just fine, and when i try to "cout<<*prim->info.varsta" FROM WITHIN THE FUNCTION it prints the corect value.
My problem however is that the line "myfile<<*prim->info.varsta<<"ani ";"
from WITHIN the function "void scriere()", spits out something like "273653122ani" / "-842150451ani" untill the end of the output, all of the other fields beeing outputed ok.
So it`s a problem with outputing integers from withing that function. I have tryed everything i could think of and i still don`t know why it acts like that. I`m not good at using the debuger, just so you know. I eagerly await your reply !

P.S. If you need me to put up the whole code of the program, i will.


Last edited on
there is probably enough to look at here to critique your code, but still don't really know what creare_lista(); is doing behind everything... post that too

plus.. if prim->info.nume is working... why didn't you logically just remove the '*' in *prim->info.varsta... but I'd still like to see the rest any way
Last edited on
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
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

struct contbancar {
    char *nume,*numar,*tara,*idsec;
    int *varsta;
};

struct nod {
    nod *next;
    contbancar info;
};

struct arbore {
    arbore *st,*dr;
    contbancar info;
};

nod *creare_lista(){
    int nr;
    char aux[20];
    ifstream myfile("input.txt");
    nod *prim,*ultim,*q;
    if (myfile.is_open()) {
        prim=new nod;
        ultim=prim;
        prim->next=NULL;
        myfile>>aux;  prim->info.nume=new char[strlen(aux)+1];  strcpy(prim->info.nume,aux);
        myfile>>nr;   prim->info.varsta=new int;                            prim->info.varsta=&nr; 
        myfile>>aux;  prim->info.tara=new char[strlen(aux)+1]; strcpy(prim->info.tara,aux);
        myfile>>aux;  prim->info.idsec=new char[strlen(aux)+1]; strcpy(prim->info.idsec,aux);
        myfile>>aux;  prim->info.numar=new char[strlen(aux)+1]; strcpy(prim->info.numar,aux);
        cout<<prim->info.nume<<" "<<*prim->info.varsta<<"ani "; cout<<prim->info.tara<<" "<<prim->info.idsec<<" "<<prim->info.numar<<endl;
        while(!myfile.eof()){
            q=new nod;
            myfile>>aux;  q->info.nume=new char[strlen(aux)+1];  strcpy(q->info.nume,aux);
            myfile>>nr;   q->info.varsta=new int;                            q->info.varsta=&nr;
            myfile>>aux;  q->info.tara=new char[strlen(aux)+1]; strcpy(q->info.tara,aux);
            myfile>>aux;  q->info.idsec=new char[strlen(aux)+1]; strcpy(q->info.idsec,aux);
            myfile>>aux;  q->info.numar=new char[strlen(aux)+1]; strcpy(q->info.numar,aux);  
            q->next=NULL;
            ultim->next=q;
            ultim=q;
            cout<<q->info.nume<<" "<<*prim->info.varsta<<"ani "; cout<<q->info.tara<<" "<<q->info.idsec<<" "<<q->info.numar<<endl;
        }
        
        
    }
    else cout<<" File unreachable";
    return prim;
}

void scriere(char *n) {
  int nr;
  int pointer;
  nod *prim;
  prim=creare_lista();
  strcat(n,".txt");  
  ofstream myfile (n);
  if(myfile.is_open()) {
    while (prim->next) {
        myfile<<*prim->info.varsta<<"ani ";
        myfile<<prim->info.nume<<" "<<prim->info.tara<<" "<<prim->info.idsec<<" "<<prim->info.numar<<endl;
        prim=prim->next;}
  }
  myfile.close();
}

void main(){
    char n[20];
    cout<<"Output file for the list: ";
    cin>>n;
    nod *cap;
    cap=creare_lista();
    scriere(n);
    //afisare(cap);
}


Basicly, i have an input file that looks like 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
Andi 16 RO 03YUVJ 2898967909123678
Andrei 20 RO 03QXVJ 2897467909123678
George 21 FR 04SDPD 1875099801742357
Ionut 22 CD 04NCAJ 9376004981267456
Andreea 23 US 54DOWQ 7365487761890165
Ioana 24 US 04CVVS 1237456730981237
Tolea 25 US 24WQUL 2387660382756347
Andrada 26 RO 54NSDK 2395783671098554
Carmen 27 FR 74XCZC 2130098563781096
Cristina 28 CD 98AWED 6454098765431234
Marius 29 GR 47VDSE 4321098778903467
Cosmin 30 RO 14MOMK 3565364533456566
Gigi 32 GR 23LLDO 1929838311276345
Vlad 33 RO 04MUNY 9876234567899090
Maria 34 GR 34WPEO 9988899898989987
Elena 35 FR 44MXHU 1237890987432212
Eugen 36 FR 33BCYU 7890789078907890
Cristi 37 CD 11UIYR 1344567834231298
Goloiu 38 RO 34WQEC 8655865865671233
Hageoglu 39 US 34LMNY 3456655443432323
Albu 40 US 07BMBT 7364502394702944
Mihai 41 TK 66QEQW 3484346843216400
Seth 42 TK 40REWC 3173654104537360
Summer 43 RO 34SAER 8741308720480204
Marissa 44 RO 74MUQE 4657068454809899
Ciprian 45 TK 24OOIN 4642310556880865
Kyle 46 RO BVRT87 1414511413515834
Cartman 47 RO 15LOPM 3409230947293455
Kenny 48 TK 44HHUD 4253097429374232
Jody 49 RO 22MCEU 3949273904724337
Francesca 50 RF 44EGLM 8374657109812353
Random 51 RO 50OIUB 2913658817658769


And the function "creare_lista()" creates a list, from the values it gets from my input file.
The function "scriere()" fills the output file with the elements of the list.

The output file looks like 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
273653122ani Andi RO 03YUVJ 2898967909123678
-842150451ani Andrei RO 03QXVJ 2897467909123678
-842150451ani George FR 04SDPD 1875099801742357
-842150451ani Ionut CD 04NCAJ 9376004981267456
-842150451ani Andreea US 54DOWQ 7365487761890165
-842150451ani Ioana US 04CVVS 1237456730981237
-842150451ani Tolea US 24WQUL 2387660382756347
-842150451ani Andrada RO 54NSDK 2395783671098554
-842150451ani Carmen FR 74XCZC 2130098563781096
-842150451ani Cristina CD 98AWED 6454098765431234
-842150451ani Marius GR 47VDSE 4321098778903467
-842150451ani Cosmin RO 14MOMK 3565364533456566
-842150451ani Gigi GR 23LLDO 1929838311276345
-842150451ani Vlad RO 04MUNY 9876234567899090
-842150451ani Maria GR 34WPEO 9988899898989987
-842150451ani Elena FR 44MXHU 1237890987432212
-842150451ani Eugen FR 33BCYU 7890789078907890
-842150451ani Cristi CD 11UIYR 1344567834231298
-842150451ani Goloiu RO 34WQEC 8655865865671233
-842150451ani Hageoglu US 34LMNY 3456655443432323
-842150451ani Albu US 07BMBT 7364502394702944
-842150451ani Mihai TK 66QEQW 3484346843216400
-842150451ani Seth TK 40REWC 3173654104537360
-842150451ani Summer RO 34SAER 8741308720480204
-842150451ani Marissa RO 74MUQE 4657068454809899
-842150451ani Ciprian TK 24OOIN 4642310556880865
-842150451ani Kyle RO BVRT87 1414511413515834
-842150451ani Cartman RO 15LOPM 3409230947293455
-842150451ani Kenny TK 44HHUD 4253097429374232
-842150451ani Jody RO 22MCEU 3949273904724337
-842150451ani Francesca RF 44EGLM 8374657109812353

As you can see, the values in the front of the names should be 16, 17, 18 and so on. That`s my problem.

This is the output without the '*' in *prim->info.varsta. So as you can see, it returns some memory addresses, not the values at those addresses that i want.
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
0013FCB4ani Andi RO 03YUVJ 2898967909123678
00039B78ani Andrei RO 03QXVJ 2897467909123678
00039CD0ani George FR 04SDPD 1875099801742357
00039E28ani Ionut CD 04NCAJ 9376004981267456
00039F80ani Andreea US 54DOWQ 7365487761890165
0003A0D8ani Ioana US 04CVVS 1237456730981237
0003A230ani Tolea US 24WQUL 2387660382756347
0003A388ani Andrada RO 54NSDK 2395783671098554
0003A4E0ani Carmen FR 74XCZC 2130098563781096
0003A638ani Cristina CD 98AWED 6454098765431234
0003A790ani Marius GR 47VDSE 4321098778903467
0003A8E8ani Cosmin RO 14MOMK 3565364533456566
0003AA40ani Gigi GR 23LLDO 1929838311276345
0003AB98ani Vlad RO 04MUNY 9876234567899090
0003ACF0ani Maria GR 34WPEO 9988899898989987
0003AE48ani Elena FR 44MXHU 1237890987432212
0003AFA0ani Eugen FR 33BCYU 7890789078907890
0003B0F8ani Cristi CD 11UIYR 1344567834231298
0003B250ani Goloiu RO 34WQEC 8655865865671233
0003B3A8ani Hageoglu US 34LMNY 3456655443432323
0003B500ani Albu US 07BMBT 7364502394702944
0003B658ani Mihai TK 66QEQW 3484346843216400
0003B7B0ani Seth TK 40REWC 3173654104537360
0003B908ani Summer RO 34SAER 8741308720480204
0003BA60ani Marissa RO 74MUQE 4657068454809899
0003BBB8ani Ciprian TK 24OOIN 4642310556880865
0003BD10ani Kyle RO BVRT87 1414511413515834
0003BE68ani Cartman RO 15LOPM 3409230947293455
0003BFC0ani Kenny TK 44HHUD 4253097429374232
0003C120ani Jody RO 22MCEU 3949273904724337
0003C278ani Francesca RF 44EGLM 8374657109812353


Also, i`m a beginner. That`s why i posted in this section of the forums. So if you could explain in detail, if you find something wrong, and how to fix it, it would be great.

Please also note that, the line "cout<<q->info.nume<<" "<<*prim->info.varsta<<"ani "; cout<<q->info.tara<<" "<<q->info.idsec<<" "<<q->info.numar<<endl;" outputs the list corectly. But that`s within the function that creates the list ("creare_lista");
Last edited on
You're a beginner using pointers? Why don't you use a vector or something instead? It would make things a lot more readable also.

Yeah, looks like you just need to remove the '*'... I'll go over the code in a bit though.
Using pointers is a requirement of the assignment. I have no saying in the matter. :(
ok, np... either way, you don't need prim to be a pointer, you could have just passed it as a reference to the function and changed as needed..

1
2
3
4
5
6
7
8
9
10
11
12

void creare_lista( nod& prime ){
  //... oh, don't redeclare prime also...
  // now you can change prime directly
}

//...

  nod prime;
  creare_lista(prime);

// ... 


now to access like that you just need the '.' not the "->"

the int in contbancar can be just a regular int and not a pointer, because it's working just as an int.

creating extra pointers is probably not part of the assignment, and you might lose points..

Why the "nod"? Are you supposed to do a node array
Last edited on
you are setting everything to point to int nr; plus you lose the address of the new int you create... try this...

 
        myfile>>nr;   prim->info.varsta=new int;                            prim->info.varsta=&nr; 


you are setting the varsta pointer to point to nr, but you are changing it, and the function probably deletes it...

1
2
3
        myfile>>nr;
        prim->info.varsta=new int;
        *prim->info.varsta=nr; 


that might work... but you have quite a few issues anyway, but for now try that
Last edited on
as far as removing the '*' that I said before... I was still in the mind that it was an int... so sue me, it shouldn't be a pointer anyway
I'm supposed to declare ALL of the elements within the structure as pointers. (requirement).
And i`m supposed to do a list. First element points to the next, second to 3'rd and so on. So i need my function "creare_lista()" to return the head of the list, wich is a "nod *", once it completes creating the list, so that i can keep track of the elements. Also i need the head of the list to traverse it, so the function must return the head as a "nod ".
The
1
2
3
myfile>>nr;
prim->info.varsta=new int;
*prim->info.varsta=nr;
seems to fix it.
You have saved me, thank you very much. I was up for about 20+ hours and "*prim->info.varsta=nr;" didn`t even cross my mind. Now time for some sleep. Once again than you very much !
Last edited on
yeah, I should have figured it was a node array... I'm lacking sleep myself...

you could make the function

1
2
3
void creare_lista( nod* prime ){
  //...
}


just to send the pointer also... that way you can still treat it like a node, and you can adjust it in the function
Topic archived. No new replies allowed.