Acces Violation

I'm having hard time with acces violations...I read here it's usually from bad choice of chars/strings/constant chars. I corrected all the mistaked i could...but i still didn't get them al...so i turned to you guys. I just learned structures in scholl and was trying to make something practical by making a reminder for movie launch dates.

Here is the header file:
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
#ifndef MOVIEREMINDER_H_
#define MOVIEREMINDER_H_

#include<fstream>

using namespace std;

#define T 1
#define F 0

struct movie{
	char* name;
	int day;
	int month;
	int year;
};

struct movies{
	struct movie nmb[50];
	int mtotal;
};

struct movies loadmovies(){ //this loads the info from the file "movies.db"
	struct movies ret;
	int index;
	
	ifstream mov;
	mov.open("movies.db");
	
	while(!mov.eof()){
		mov.getline(ret.nmb[index].name,31);
		mov>>ret.nmb[index].day>>ret.nmb[index].month>>ret.nmb[index++].year;
	}
	
	mov.close();
	
return ret;
}

int checkmovies(){ //check to see if the file "movies.db" exists
	system("dir /b > files.txt");
	
	ifstream files;
	files.open("files.txt");
	char line[50];
	
	while(!files.eof()){
		files.getline(line,51);
		if(strcmp(line,"movies.db")==F) return 1;
	}
	
return 0;
}

#endif /*MOVIEREMINDER_H_*/ 


Here is the source file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<ctime>
#include<iostream>
#include<cstdlib>
#include"MovieReminder.h"

using namespace std;

int main(){
	struct movies movies;
	int i;
	
	if(checkmovies()) movies=loadmovies();
	else{
		cout<<endl<<endl<<endl<<endl<<"    You have no movie data file. Please make one...";
		exit('1');
	}
	
	for(i=0;i<movies.mtotal;i++){
		cout<<movies.nmb[i].name<<" appears on "<<movies.nmb[i].day<<"."<<movies.nmb[i].month<<"."<<movies.nmb[i].year<<" ."<<endl;
	}
}


Right now it's just supposed to print out the movie dates and names....didn't want to make it hard to read...

A movie file example:
1
2
3
4
Movie1
2 2 2008
Movie2
6 6 2008


Please tell me what i did wrong. I suspect it's somewhere inside the structure.
Your access violation is caused by trying to store a string into a pointer with no space associated with it. This line
 
mov.getline(ret.nmb[index].name,31);

causes the violation.
The name element in your movie struct needs space associated with it:
1
2
3
4
struct movie {
    char name[50];
    ...
};

And you shouldn't have executable code in a header file (not usually, at least). So you should move checkmovies and loadmovies into the source file, since that's where code belongs.
Last edited on
I corrected both my mistakes...same problem:|

The files now look like this:

CPP:
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
#include<ctime>
#include<iostream>
#include<cstdlib>
#include"MovieReminder.h"

using namespace std;

int checkmovies(){
	system("dir /b > files.txt");
	
	ifstream files;
	files.open("files.txt");
	char line[50];
	
	while(!files.eof()){
		files.getline(line,51);
		if(strcmp(line,"movies.db")==F) return 1;
	}
	
return 0;
}

struct movies loadmovies(){
	struct movies ret;
	int index=1,day,month,year,seen;
	char line[30];
	
	ifstream mov;
	mov.open("movies.db");
	
	while(!mov.eof()){
		mov.getline(line,31);
		strcpy(ret.nmb[index].name,line);
		mov>>day>>month>>year>>seen;
		ret.nmb[index].day=day;
		ret.nmb[index].month=month;
		ret.nmb[index].year=year;
		ret.nmb[index++].seen=seen;
	}
	
	mov.close();
	
return ret;
}

int main(){
	struct movies movies;
	int i;
	
	if(checkmovies()) movies=loadmovies();
	else{
		cout<<endl<<endl<<endl<<endl<<"    You have no movie data file. Please make one...";
		exit('1');
	}
	
	for(i=0;i<movies.mtotal;i++){
		cout<<movies.nmb[i].name<<" appears on "<<movies.nmb[i].day<<"."<<movies.nmb[i].month<<"."<<movies.nmb[i].year<<" ."<<endl;
	}
}


HEADER:
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
#ifndef MOVIEREMINDER_H_
#define MOVIEREMINDER_H_

#include<fstream>
#include<cstring>


#define T 1
#define F 0

struct movie{
	char name[50];
	int day;
	int month;
	int year;
	int seen;
};

struct movies{
	struct movie nmb[50];
	int mtotal;
};


#endif /*MOVIEREMINDER_H_*/ 

Below is my re-write of your code.
Please note in particular the following:
* It does not have any ridiculously long lines.
* It has spaces between most tokens on a line.
Also, I got rid of the checkmovies() function and left that check for the load_movies() function.

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
#include<iostream>
#include<fstream>
using namespace std;

const int MAX_NAME = 50;
const int MAX_MOVIES = 50;

struct Movie
{
    char  name[MAX_NAME];
    int   day;
    int   month;
    int   year;
};

struct Movies
{
    Movie movie[MAX_MOVIES];
    int   size;
};

void load_movies( Movies &movies )
{
    ifstream fmov;
    fmov.open( "movies.db" );
    movies.size = 0;
    while( true )
    {
        fmov.getline( movies.movie[movies.size].name, MAX_NAME );
        if( !fmov.good() ) break;

        fmov >> movies.movie[movies.size].day
             >> movies.movie[movies.size].month
             >> movies.movie[movies.size].year;
        fmov.ignore( 100, '\n' );
        ++movies.size;
    }
    fmov.close();
}

void show_movies( Movies &movies )
{
    for( int i = 0; i < movies.size; i++ )
    {
        cout << movies.movie[i].name  << " appears on "
             << movies.movie[i].day   << ","
             << movies.movie[i].month << ","
             << movies.movie[i].year  << ".\n";
    }
}

int main(){
    Movies movies;

    load_movies( movies );
    show_movies( movies );
}
Thx...i'll try to improve my style....yet you missed my point. I wanted to know what my mistake was....so i don't repeat it. I didn't just want to make this work...it's just something I thought about, not really important.
Topic archived. No new replies allowed.