First off, some setup. I'm doing the final course project for my C++ class, and this is the first time I've had trouble. The things I'm supposed to be doing in this assignment seemed a lot simpler before I started working on this, and now I'm just hitting walls.
The assignment is to design a program that loads records from a file, and the records contain the names of movies, the two main actors for those movies, the year of release, and the length in minutes (a list of DVDs the user supposedly owns and information about each movie). It's supposed to contain, in a class file, methods for adding new DVDs, editing information on those DVDs, and for removing a DVD. Also in a class file, it's supposed to store the DVD information that is saved and loaded from file.
I really am struggling with loading and saving from files. What I assume is that I need a data structure to hold the information and then I need to pass the structure to my main to save. The problem is that we didn't go that in-depth when we learned this, and I'm not sure how to do it. How I'm trying to do it is not working. There are tons of problems.
Here is my main.cpp file:
#include "dvdinfo.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void displayMenu();
void mFileIn();
struct Movie
{
string sTitle, sA1, sA2;
int sYear, sTime;
};
int main ()
{
DvdInfo dInfo;
Movie thisDVD;
int mChoice;
bool doExit = false,
doSave;
fstream movieFile("movielist.dat", ios::out | ios::binary);
cout << "Welcome to your Electronic DVD Library" << endl << endl;
while (!doExit)
{
displayMenu();
cout << "[?] Choice: ";
cin >> mChoice;
while (mChoice < 1 || mChoice > 7)
{
cout << "\n\n[!] Invalid option." << endl;
cout << "[?] Choice: ";
cin >> mChoice;
}
cout << endl;
switch (mChoice)
{
case 1:
cout << "No functionality yet." << endl;
break;
case 2:
if (dInfo.setInfo())
{
thisDVD = dInfo.passInfo();
movieFile.write(reinterpret_cast<char *>(&thisDVD), sizeof(thisDVD));
}
break;
case 3:
cout << "No functionality yet." << endl;
break;
case 4:
mFileIn();
break;
case 5:
cout << "No functionality yet." << endl;
break;
case 6:
cout << "No functionality yet." << endl;
case 7:
doExit = true;
}
}
system("pause");
return 0;
}
void displayMenu()
{
cout << "\n|===============================================|" << endl;
cout << "[ 1 ] Review DVD Library" << endl;
cout << "[ 2 ] Add a new DVD" << endl;
cout << "[ 3 ] Remove a DVD" << endl;
cout << "[ 4 ] Add DVDs from a file" << endl;
cout << "[ 5 ] Purge DVD library" << endl;
cout << "[ 6 ] Save your list" << endl;
cout << "[ 7 ] Exit" << endl << endl;
}
void mFileIn()
{
fstream dvdFile;
string fName, fOut, iMov, iA1, iA2;
int iYear, iTime, search,
isAt = 1;
cin.get();
cout << endl;
cout << "Please input the name of the file you want to import from." << endl;
cout << "[?] File name: ";
getline(cin,fName);
dvdFile.open(fName, ios::in);
//See if the file will open.
if (dvdFile.fail()) {
cout << "[!] Error! File not found." << endl;
}
//Get the line for output.
while (dvdFile)
{
getline(dvdFile, fOut, '$');
cout << fOut << endl;
}
}
And here is my class header file, dvdinfo.h
#ifndef DVDINFO_H
#define DVDINFO_H
#include<string>
using namespace std;
class DvdInfo
{
private:
static int dvdCount;
struct Movie
{
string sTitle, sA1, sA2;
int sYear, sTime;
};
Movie thisMovie;
bool isCorrect, doSave;
char yesno;
public:
DvdInfo ()
{
thisMovie.sTitle = "empty";
thisMovie.sA1 = "empty";
thisMovie.sA2 = "empty";
thisMovie.sYear = 1900;
thisMovie.sTime = 20;
}
DvdInfo (string t, string a1, string a2, int y, int l)
{
thisMovie.sTitle = t;
thisMovie.sA1 = a1;
thisMovie.sA2 = a2;
thisMovie.sYear = y;
thisMovie.sTime = l;
}
Movie passInfo()
{
return thisMovie;
}
bool setInfo()
{
isCorrect = true;
yesno = 'Y';
cin.get();
cout << endl;
cout << "Please input the information for the DVD you wish to add." << endl;
cout << "[?] Title: ";
getline(cin, thisMovie.sTitle);
cout << endl << endl;
cout << "[?] Lead Actor: ";
getline(cin, thisMovie.sA1);
cout << endl << endl;
cout << "[?] Supporting actor: ";
getline(cin, thisMovie.sA2);
cout << endl << endl;
cout << "[?] Year of release: ";
cin >> thisMovie.sYear;
while (thisMovie.sYear < 1900 || thisMovie.sYear > 2015)
{
cout << "\n\n[!] Invalid year. (Please choose 1900 to 2015)" << endl;
cout << "[?] Year of release: ";
cin >> thisMovie.sYear;
}
cout << endl << endl;
cout << "[?] Length (in minutes): ";
cin >> thisMovie.sTime;
while (thisMovie.sTime < 20 || thisMovie.sTime > 300)
{
cout << "\n\n[!] Invalid length. (Please choose 20 to 300)" << endl;
cout << "[?] Length (in minutes): ";
cin >> thisMovie.sTime;
}
cout << endl;
cout << "\n|===============================================|" << endl;
cout << thisMovie.sTitle << " (" << thisMovie.sYear << ")" << endl;
cout << "Starring " << thisMovie.sA1 << " and " << thisMovie.sA2 << endl;
cout << thisMovie.sTime << " minutes." << endl;
cout << "|===============================================|" << endl << endl;
cout << "Is this correct?" << endl;
cout << "[?] (Y)es or (N)o: ";
cin >> yesno;
while (toupper(yesno) != 'Y' && toupper(yesno) != 'N')
{
cout << "\n\n[!] Please enter 'Y' for yes or 'N' for no." << endl;
cout << "[?] (Y)es or (N)o: ";
cin >> yesno;
}
switch (toupper(yesno))
{
case 'Y':
cout << "Saving the new title to the file." << endl;
bool doSave = true;
case 'N':
cout << "Returning to the main menu." << endl;
bool doSave = false;
}
return doSave;
}
};
#endif
Obviously, a lot hasn't been done. I got stuck pretty early in and now I don't know what to do. I work all this week and I won't have a lot of time, and it's due on Sunday.