C++ programming error message.

0 votes
asked Oct 20 by Richard Tidwell (120 points)

C++ programing question: I don't understand why I am getting this error. can someone help me please. I have attached my code files and error.

Error

Compilation failed due to following error(s). In file included from MovieSelectionFunctions.cpp:1:0, from main.cpp:13: MovieS

Code

Main.cpp

#include <stdio.h>
#include <iostream>
#include"MovieSelectorHeader.h"
#include"MovieSelectionFunctions.cpp"

int main()
{
int choice=1;
while(choice) ///loop until Seat menu return 0
{
  
choice= MovieSelectorMenu();
  
}
cout<<"\n\t----Good Bye!----\t" << endl; /// good bye message
}

Function file

#include "MovieSelectorHeader.h"

int MovieSelectorMenu()
{
char Choice;
cout << "Moive Selector Menu\n"
<< endl;
cout << "Display Movie List :D" << endl;
cout << "Search for Movie :S" << endl;
cout << "Quit :Q" << endl;
cout << "Enter your Choice :";
cin >> Choice; ///user input choice

if (Choice == 'd' || Choice == 'D')
{
fillDatabase();
display();
return 1;
}
else if (Choice == 's' || Choice == 'S')
{
linearSearch();
return 1;
}
else if (Choice == 'q' || Choice == 'Q')
{
return 0; /// return 0 and exit the program
}
else
{
cout << "\n Please enter a valid choice\t Try Again " << endl; ///return 1 and prompt the menu again
return 1;
}
}

void fillDatabase()
{
fstream input_file;
input_file.open("MovieData.txt"); ///open Arrival file
if (!input_file.is_open()) ///if file open fails
{
cout << "Cannot open file" << endl;
exit(EXIT_FAILURE);
}
else
{
int i = 0;

///read every line in the file until end of file reached
while (!input_file.eof() && i < NUM_OF_MOVIES)
{
string ip, temp;
getline(input_file, ip); //reading a line
istringstream ss(ip); //using istringstream
getline(ss, items[i].Title, ','); //tokenising
getline(ss, temp, ',');
istringstream temp1(temp);
temp1 >> items[i].ReleaseYear; /*processing the year of release as an integer*/
getline(ss, items[i].Director, ',');
getline(ss, items[i].Genre, ',');
getline(ss, items[i].Starring[0], ',');
getline(ss, items[i].Starring[1], ',');
i++;
}
tot = i;
input_file.close(); ///close the file
}
}

void linearSearch()
{
cout << "Please enter the year of the movie in YYYY format: ";
int year;
cin >> year;
int found = 0; //for checking if the movie is found or not

for (int i = 0; i < tot; i++)
{
if (items[i].ReleaseYear == year)
{
cout << "\n";
cout << "Movie found in the database... \n\n";
cout << "Movie Title: " << items[i].Title << endl;
cout << "Release Year: " << items[i].ReleaseYear << endl;
cout << "Director: " << items[i].Director << endl;
cout << "Genre: " << items[i].Genre << endl;
cout << "Starring 1: " << items[i].Starring[0] << endl;
cout << "Starring 2: " << items[i].Starring[1] << endl;
found = 1;
}
}
if (found == 0)
{
cout << "\nSorry No movie found";
}
cout << "\n";
}

void display() //function to display the movie details
{
for (int i = 0; i < tot; i++)
{
cout << "\n";
cout << "Movie Title: " << items[i].Title << endl;
cout << "Release Year: " << items[i].ReleaseYear << endl;
cout << "Director: " << items[i].Director << endl;
cout << "Genre: " << items[i].Genre << endl;
cout << "Starring 1: " << items[i].Starring[0] << endl;
cout << "Starring 2: " << items[i].Starring[1] << endl;
cout << "\n";
}
}

Header file

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
#define NUM_OF_MOVIES 100

struct Movie
{
string Title;
int ReleaseYear;
string Director;
string Genre;
string Starring[2];
};

Movie items[NUM_OF_MOVIES]; //creating an array of 100 movie items
int tot; //variable that will be used to store the total number of movies in the file
int MovieSelectorMenu();
void fillDatabase();
void linearSearch();
void display();

1 Answer

0 votes
answered Oct 22 by Peter Minarik (12,220 points)

I think your problem is that you your header file does not have the include guard that prevents it to be included multiple times unnecessarily.

Solution #1

Using the #ifdef/#define/#endif preprocessor calls to ensure you only include your header once.

You should start your header file (first 2 line) and end your header file (last line) as below.

#ifndef MY_HEADER_NAME_H
#define MY_HEADER_NAME_H

// Put your header file content here

#endif // MY_HEADER_NAME_H

Of course, you can replace MY_HEADER_NAME_H by anything. Usually this name is the same as the name (and path) of your header file.

This solution works in every platform.

Solution #2

You can use the #pragma once preprocessor call. Make it the first call of your header file.

#pragma once

// Put your header file content here

This is shorter, works on many platforms, but is not necessarily supported by all the compilers.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...