So i'm fooling around with classes, fstream, and arrays. I'm (strangely) used to Javascript, where an Array is a pre-determined datatype, and all you would have to write would be
var array1 = new Array();
or
var array2 = [1,"2"];
. I can see how this is similar to C++, where arrays are defined by what they contain:
std::string* entry_list [20] = {"1","2",etc...};
However, i'm a bit confused on writing data to an array. I've got four files, for experimentation, on creating a class, getting some stuff from a file, then writing it in an array so that the user can access it when they want.
SSCCE:
I've trimmed parts of the code that are unrelated.
My problem is that when I call database::add_entry, the program breaks on line 17 of database.cpp. Visual Studio gives me the error,
Unhandled exception at 0x00e71f07 in g_cpp_database.exe: 0xC0000005: Access violation writing location 0x33732a98. |
How do I add information to my array?
Database.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <string>
const int maxEntries = 20;
class database {
public:
int entry_list_length;
database(void);
~database(void);
void addEntry();
void add_entry(std::string title,std::string artist,std::string album,std::string length);
void remove_entry(int entry_id);
void display();
void command(int option);
void check_file();
int exit();
private:
std::string* entry_list [maxEntries];
int entry_list_current;
};
|
Database.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
|
#include "database.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
database::database(void) {}
database::~database(void) {}
void database::add_entry(std::string title,std::string artist, std::string album,std::string length) {
if (database::entry_list_length >= maxEntries) {
return;
} else {
std::string tempArray [4] = {title,artist,album,length};
if (database::entry_list_length == NULL || database::entry_list_current == NULL) {
database::entry_list_length = 0;
database::entry_list_current = 0;
}
database::entry_list[database::entry_list_current] = tempArray;
for (int n=0;n<4;n++) {
std::cout << tempArray[n] << std::endl;
}
database::entry_list_length++;
database::entry_list_current++;
}
}
void database::check_file() {
using namespace std;
ifstream musicFile;
string line;
musicFile.open("music.txt",ios::in);
if (musicFile.is_open()) {
getline(musicFile,line);
if (line == "good") {
bool remaining = true;
while (remaining) {
string title;
string artist;
string album;
string length;
getline(musicFile,title);
getline(musicFile,artist);
getline(musicFile,album);
getline(musicFile,length);
if (length == " " || length == "") {
remaining = false;
} else {
database::add_entry(title,artist,album,length);
}
}
}
}
musicFile.close();
}
|
main.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
|
#include <iostream>
#include <string>
#include "database.h"
int main() {
using namespace std;
database dB;
int option;
bool mainMenu = true;
bool running = true;
dB.check_file();
/*while (running) {
while (mainMenu) {
cout << "Welcome to the music catalog." << endl << "1)Enter new song" << endl << "2) display all songs" << endl << "10) exit";
if (!(cin>>option)) {
cout << "Please enter a numeric option." << endl;
return 0;
} else if (option == 10) {
return 0;
} else {
mainMenu = false;
dB.command(option);
}
}
}*/
system("PAUSE");
return 0;
}
|
(I understand that using
system("PAUSE");
is a bad idea, but this is just a test, and not the final version of the code.)
music.txt:
1 2 3 4 5 6 7 8 9
|
good
Derezzed
Daft Punk
TRON: Legacy
1:44
Mass
Virtual Boy
Symphony No. None
7:31
|