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.
#ifndef MOVIEREMINDER_H_
#define MOVIEREMINDER_H_
#include<fstream>
usingnamespace 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_*/
#include<ctime>
#include<iostream>
#include<cstdlib>
#include"MovieReminder.h"
usingnamespace 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.
#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.
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.