Hello everyone
My program stops working when I enter the number of rating stars when adding an item that is option 2 in the Main Menu. Would someone help me with this please ?
//*********************************
// Libraries used for the program
//*********************************
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
//*********************************
// Linked list Structure for Movie
//*********************************
struct movie_node
{
string movie_name;
string cast_name[3];
string movie_genre;
int movie_rate;
movie_node* link;
};
//*****************************
// Main body of the program
//*****************************
int main ()
{
int choice, choice1;
string name, label, label1, cast_movies[3], choice2;
movie_node* nodeptr, *before_nodeptr;
movie_node* head;
head = new movie_node;
//*****************************************************
// Do-while loop to satisfy options from the Main Menu
//*****************************************************
do {
cout << " MAIN MENU :- "<< endl;
cout << endl;
cout << " 1) Removing an item. " << endl;
cout << " 2) Adding an item. " << endl;
cout << " 3) Displaying an item. " << endl;
cout << " 4) Searching an item. " << endl;
cout << " 5) Exit the program. " << endl;
cout << endl;
cout << " Enter your option out of these 5 options please : " << endl;
cin >> choice;
//***************************************************
// If condition to satisfy option number 1 from the Main Menu.
// Removing an item.
//***************************************************
if(choice == 1)
{
cout << " Enter the name of the movie/movies you want to remove ? " << endl;
cin >> name;
//***************************************************
// If condition to show if the list is empty or not.
//***************************************************
if (!head)
{
cout << " Empty list ! " << endl;
return -1;
}
//**************************************************************************
// If condition to check if the first node is the one to be deleted or not.
//**************************************************************************
if (head->movie_name == name)
{
nodeptr = head;
head = head->link;
delete nodeptr;
}
else
{
//*************************************************
// nodeptr is initialized to the head of the list.
//*************************************************
nodeptr = head;
while (nodeptr != NULL && nodeptr->movie_name != name)
//***************************************************
// Else if condition to satisfy option number 2 from the Main Menu.
// Adding an item.
//***************************************************
else if (choice == 2)
{
//***************************************
// Entering the name of the movie.
//***************************************
head = new movie_node;
cout << " Please enter the name of the movie you want to add or press '-5' to EXIT the program " << endl;
cin >> label;
if(label == "-5")
break;
//***************************************
// Entering the genre of the movie.
//***************************************
cout << " Enter your genre choice of the following genres:" << endl;
cout << " 1. ACTION " << endl;
cout << " 2. COMEDY " << endl;
cout << " 3. DRAMA " << endl;
cout << " 4. HORROR " << endl;
cout << " 5. MARTIAL ARTS " << endl;
cout << " 6. ROMANCE " << endl;
cout << " 7. SCIENCE FICTION " << endl;
cout << " 8. SUPER NATURAL " << endl;
cin >> choice2;
cout << endl;
cout << endl;
//******************************************
// Entering the actor names of the movie.
//****************************************
cout << " Enter 3 names of actors from this movie " << endl;
for (int a = 0; a < 3; a++)
{
cin >> cast_movies[a];
cout << endl;
cout << endl;
}
//***************************************
// Entering the movie rating.
//***************************************
cout << " Enter the movie rating:- 1, 2, 3, 4 or 5 stars " << endl;
cin >> choice1;
cout << endl;
cout << endl;
//***************************************************
// Else if condition to satisfy option number 3 from the Main Menu.
// Displaying an item.
//***************************************************
else if (choice == 3)
{
movie_node *current;
if (!head)
{
cout << " Empty list ! " << endl;
return-1;
}
else
{
cout << " The list contains -: " << endl;
while (current !=NULL)
{
cout << current->movie_name << endl;
for ( int a = 0; a < 3; a++)
cout << current-> cast_name[a] << endl;
cout << current-> movie_genre << endl;
cout << current-> movie_rate << endl;
cout << " *************** " << endl;
current = current->link ;
}
}
}
//***************************************************
// Else if condition to satisfy option number 4 from the Main Menu.
// Searching for an item.
//***************************************************
else if (choice == 4)
{
cout << " Enter the movie name you want to search for. " << endl;
cin >> label1;
movie_node* search = head;
//***************************************************
// If condition to check for an empty linked list.
//***************************************************
if (search == NULL)
{
return-1;
}
//***************************************************
// Moving to the next node for searching the movie.
//***************************************************
else
{
while (search->movie_name != label1 && search->link !=NULL)
{
search = search->link;
}
//***************************************************
// If condition to display the movie searched for.
//***************************************************
if (search->movie_name == label1)
{
cout << search->movie_genre << endl;
cout<< search->movie_name << endl;
cout<< search->movie_rate << endl;
for (int a = 0; a < 3; a++)
cout << search->cast_name[a] << endl;
}
else
{
return-1;
}
}
}
//***************************************************
// Else if condition to satisfy option number 5 from the Main Menu.
// Quitting the program.
//***************************************************
else if (choice == 5)
{
break;
}
//*********************************************************************************************
// Else condition to satisfy an option out from the options given from 1 to 5 in the Main Menu.
// User re-enters a choice between 1 to 5 from the Main Menu.
//*********************************************************************************************
else (choice <1 || choice >5);
cout << " Your choice made is incorrect. Please choose again an option. " << endl;
}
while ( choice >=1 && choice <=5);
//******************************************
// Pausing the system.
//******************************************
system ("pause");
if (head == NULL) // <---- You need to create an object for head: head = new movie_node;
{
for (int a = 0; a < 3; a++)
head->cast_name[a] = cast_movies[a];
head-> movie_genre = choice2;
head-> movie_name = label;
head-> movie_rate = choice1;
}
You cannot access the members when the pointer to the object is NULL.
You always need to initialize at least the link member of movie_node. If not it will very likely crash later. The easiest way would be a constructor.
I created the object, but now when I enter the rating number of stars and press enter, program goes back to -" Your choice made is incorrect. Please choose again an option"
//*********************************
// Libraries used for the program
//*********************************
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
//*********************************
// Linked list Structure for Movie
//*********************************
struct movie_node
{
string movie_name;
string cast_name[3];
string movie_genre;
int movie_rate;
movie_node* link;
};
//*****************************
// Main body of the program
//*****************************
int main ()
{
int choice, choice1;
string name, label, label1, cast_movies[3], choice2;
movie_node* nodeptr, *before_nodeptr;
movie_node* head;
head = new movie_node;
//*****************************************************
// Do-while loop to satisfy options from the Main Menu
//*****************************************************
do {
cout << " MAIN MENU :- "<< endl;
cout << endl;
cout << " 1) Removing an item. " << endl;
cout << " 2) Adding an item. " << endl;
cout << " 3) Displaying an item. " << endl;
cout << " 4) Searching an item. " << endl;
cout << " 5) Exit the program. " << endl;
cout << endl;
cout << " Enter your option out of these 5 options please : " << endl;
cin >> choice;
//***************************************************
// If condition to satisfy option number 1 from the Main Menu.
// Removing an item.
//***************************************************
if(choice == 1)
{
cout << " Enter the name of the movie/movies you want to remove ? " << endl;
cin >> name;
//***************************************************
// If condition to show if the list is empty or not.
//***************************************************
if (!head)
{
cout << " Empty list ! " << endl;
return -1;
}
//**************************************************************************
// If condition to check if the first node is the one to be deleted or not.
//**************************************************************************
if (head->movie_name == name)
{
nodeptr = head;
head = head->link;
delete nodeptr;
}
else
{
//*************************************************
// nodeptr is initialized to the head of the list.
//*************************************************
nodeptr = head;
while (nodeptr != NULL && nodeptr->movie_name != name)
before_nodeptr = nodeptr;
nodeptr = nodeptr->link;
}
if (nodeptr)
{
before_nodeptr-> link = nodeptr-> link;
delete nodeptr;
cout << " .............End............. " << endl;
}
}
//***************************************************
// Else if condition to satisfy option number 2 from the Main Menu.
// Adding an item.
//***************************************************
elseif (choice == 2)
{
//***************************************
// Entering the name of the movie.
//***************************************
head = new movie_node;
cout << " Please enter the name of the movie you want to add or press '-5' to EXIT the program " << endl;
cin >> label;
if(label == "-5")
break;
//***************************************
// Entering the genre of the movie.
//***************************************
cout << " Enter your genre choice of the following genres:" << endl;
cout << " 1. ACTION " << endl;
cout << " 2. COMEDY " << endl;
cout << " 3. DRAMA " << endl;
cout << " 4. HORROR " << endl;
cout << " 5. MARTIAL ARTS " << endl;
cout << " 6. ROMANCE " << endl;
cout << " 7. SCIENCE FICTION " << endl;
cout << " 8. SUPER NATURAL " << endl;
cin >> choice2;
cout << endl;
cout << endl;
//******************************************
// Entering the actor names of the movie.
//****************************************
cout << " Enter 3 names of actors from this movie " << endl;
for (int a = 0; a < 3; a++)
{
cin >> cast_movies[a];
cout << endl;
cout << endl;
}
//***************************************
// Entering the movie rating.
//***************************************
cout << " Enter the movie rating:- 1, 2, 3, 4 or 5 stars " << endl;
cin >> choice1;
cout << endl;
cout << endl;
if (choice1 < 1 || choice1 >5)
{
cout << " Please re-enter 1, 2, 3, 4, or 5:" << endl;
cin >> choice1;
cout << endl;
}
if (head == NULL)
{
for (int a = 0; a < 3; a++)
head->cast_name[a] = cast_movies[a];
head-> movie_genre = choice2;
head-> movie_name = label;
head-> movie_rate = choice1;
}
else
{
movie_node* node = head;
while (node->link != NULL)
{
node = node-> link;
}
node = new movie_node;
for ( int a = 0; a < 3; a++)
node->cast_name[a] = cast_movies[a];
node->movie_genre = choice2;
node->movie_name = label;
node->movie_rate = choice1;
}
}
//***************************************************
// Else if condition to satisfy option number 3 from the Main Menu.
// Displaying an item.
//***************************************************
elseif (choice == 3)
{
movie_node *current;
if (!head)
{
cout << " Empty list ! " << endl;
return-1;
}
else
{
cout << " The list contains -: " << endl;
while (current !=NULL)
{
cout << current->movie_name << endl;
for ( int a = 0; a < 3; a++)
cout << current-> cast_name[a] << endl;
cout << current-> movie_genre << endl;
cout << current-> movie_rate << endl;
cout << " *************** " << endl;
current = current->link ;
}
}
}
//***************************************************
// Else if condition to satisfy option number 4 from the Main Menu.
// Searching for an item.
//***************************************************
elseif (choice == 4)
{
cout << " Enter the movie name you want to search for. " << endl;
cin >> label1;
movie_node* search = head;
//***************************************************
// If condition to check for an empty linked list.
//***************************************************
if (search == NULL)
{
return-1;
}
//***************************************************
// Moving to the next node for searching the movie.
//***************************************************
else
{
while (search->movie_name != label1 && search->link !=NULL)
{
search = search->link;
}
//***************************************************
// If condition to display the movie searched for.
//***************************************************
if (search->movie_name == label1)
{
cout << search->movie_genre << endl;
cout<< search->movie_name << endl;
cout<< search->movie_rate << endl;
for (int a = 0; a < 3; a++)
cout << search->cast_name[a] << endl;
}
else
{
return-1;
}
}
}
//***************************************************
// Else if condition to satisfy option number 5 from the Main Menu.
// Quitting the program.
//***************************************************
elseif (choice == 5)
{
break;
}
//*********************************************************************************************
// Else condition to satisfy an option out from the options given from 1 to 5 in the Main Menu.
// User re-enters a choice between 1 to 5 from the Main Menu.
//*********************************************************************************************
else (choice <1 || choice >5);
cout << " Your choice made is incorrect. Please choose again an option. " << endl;
}
while ( choice >=1 && choice <=5);
//******************************************
// Pausing the system.
//******************************************
system ("pause");
return 0;
}