Hey everyone.
I just joined this forum, so I might be doing something wrong, but I made a program with linked lists, but there are errors that occur:
1. Whenever I try to save a new movie, it overwrite the previous data stored in the file. [FIXED]
2. When the program exits, all information in movie.ini disappears (I have no idea why) [FIXED]
3. I need to getlines to get the movie name. [FIXED]
Code:
#include <iostream>
#include <string>
#include <conio.h>
#include <windows.h>
#include <math.h>
#include <time.h>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
bool checkFile(char filename[255])
{
ifstream check (filename);
if (check)
return 0;
else
return 1;
}
typedef class movie
{
public:
char *title;
char *cat;
char *genre;
movie *next;
};
int mainMenu()
{
int option;
cout << "+-----------------------------+" << endl;
cout << "| MAIN MENU |" << endl;
cout << "+-----------------------------+" << endl <<
"| 1. Enter a new movie |" << endl <<
"| 2. Show media |" << endl <<
"| 3. Search media |" << endl <<
"| 4. Help |" << endl <<
"| 5. About |" << endl <<
"| 6. Quit |" << endl <<
"+-----------------------------+" << endl <<
"| :ENTER AN OPTION: |" << endl <<
"+-----------------------------+" << endl;
cin >> option;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
return option;
}
void decide(int opt, movie *head)
{
string title;
string cat;
string genre;
int a = 0;
ifstream check ("movie.ini");
if (!check)
{
a = 1;
}
check.close();
if (opt == 1)
{
ofstream fOut ("movie.ini", ios::app);
cout << "Enter movie title: ";
getline(cin,title,'\n');
cout << "Enter movie category: ";
getline(cin,cat,'\n');
cout << "Enter movie genre: ";
getline(cin,genre,'\n');
if (a != 1)
fOut << endl;
fOut << title << endl << cat << endl << genre;
fOut.close();
}
if (opt == 2)
{
int cnt = 1;
movie *list;
list = head;
system("cls");
cout << setw(3) << left << "#" << setw(40) << left << "Movie" << setw(17) << left << "Category" << setw(17) << left << "Genre\n" << endl;
while (list != NULL)
{
cout << setw(3) << left << cnt << setw(40) << left << list->title << setw(17) << left << list->cat << setw(17) << left << list->genre << endl;
list = list->next;
cnt++;
}
}
if (opt == 4)
cout << endl << endl << " -- Error opening help file!" << endl << endl;
if (opt == 5)
MessageBox(NULL,"(C) 1991-2006 Matt Nesterenko","About Media Manager",MB_OK);
if (opt == 6)
exit(0);
}
void menu(string menu, movie *head)
{
int opt;
if (menu == "main")
opt = mainMenu();
decide(opt, head);
}
int main (int argc, char *argv[])
{
movie *head;
movie *mNode;
movie *tail;
while (1)
{
ifstream input( "movie.ini");
head = tail = NULL;
if (input)
{
while (! input.eof())
{
mNode = new movie;
mNode->title = new char[255];
mNode->cat = new char[255];
mNode->genre = new char[255];
mNode->next = NULL;
input.getline(mNode->title, 255, '\n');
input.getline(mNode->cat, 255, '\n');
input.getline(mNode->genre, 255, '\n');
if (head ==NULL)
head = mNode;
else
tail->next = mNode;
tail = mNode;
}
}
menu("main", head);
cout << endl << endl;
system("pause");
system("cls");
}
system("pause");
return 0;
}