Sometimes the code runs till the end without any errors while other times it stops in the middle and gives me this error Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) here is a picture of it (https://i.stack.imgur.com/uZDX1.png). The error is in my header file named Functions, and the compiler used in this picture is Xcode on a Mac device. I also tried another compiler "Visual Studio" on a Windows device and the code never runs till the end, it always stops in the middle of running and gives me an error in the same line of code, here is a picture of the error Visual Studio gave me Error in Visual Studio.
#include <iostream>
using namespace std;
//products' data
struct products{
int ID;
string Name;
double Price;
int Quantity;
};
//receipt
struct receipt{
string name;
double price;
receipt* link;
};
struct linkedListFunctions{
//inserts node at the end of the list
void insert(receipt** head_name_ref, string new_name, double new_price)
{
receipt* new_name_node = new receipt();
receipt *last = *head_name_ref;
new_name_node->name = new_name;
new_name_node->price = new_price;
new_name_node->link = NULL;
if (*head_name_ref == NULL)
{
*head_name_ref = new_name_node;
return;
}
while (last->link != NULL)//The error is right here
{
last = last->link;
}
last->link = new_name_node;
return;
}
//prints list
void printReceipt(receipt* n){
while(n!=NULL){
cout<<n->name<<": ";
cout<<n->price<<'\t'<<" ";
cout<<endl;
n=n->link;
}
}
//removes first node in the list
receipt* removeFirstreceipt(struct receipt* head)
{
if (head == NULL)
return NULL;
receipt* temp = head;
head = head->link;
delete temp;
return head;
}
};
The first two code boxes are in the header file named Functions.h The error is in the second code box at line 15, it has a comment next to it
#include "Functions.h"
int main(){
struct products details[5];
details[0] = {0, "Apple Juice", 12, 240};
details[1] = {1,"Bread", 10, 100};
details[2] = {2, "Chocolate", 5, 500};
details[3] = {3, "Dates", 50, 150};
details[4] = {4, "Eggs", 30, 360};
linkedListFunctions list;
//declaring first node in receipt linked list
receipt* head = NULL;
head = new receipt;
//prints all products IDs and Names
for (int i=0; i<5; i++) {
cout<<details[i].ID<<": ";
cout<<details[i].Name<<" ";
cout<<details[i].Price<<"LE"<<endl;
}
char buyAgain;
while ((buyAgain='y' && buyAgain!='n')){
//choosing a product
cout<<"Enter the product's ID to choose it: ";
int chooseProduct;
cin>>chooseProduct;
cout<<"ID: "<<details[chooseProduct].ID<<endl
<<"Name: "<<details[chooseProduct].Name<<endl
<<"Price: "<<details[chooseProduct].Price<<endl
<<"Quantity: "<<details[chooseProduct].Quantity<<endl<<"********"<<endl;
//choosing the quantity
cout<<"How much "<<details[chooseProduct].Name<<" do you want? ";
int chooseQuantity;
cin>>chooseQuantity;
list.insert(&head, details[chooseProduct].Name, details[chooseProduct].Price*chooseQuantity);//
details[chooseProduct].Quantity=details[chooseProduct].Quantity-chooseQuantity;
cout<<details[chooseProduct].Name<<" Left: "<<details[chooseProduct].Quantity<<endl<<"********"<<endl;
cout<<"Would you like to order something else? y=yes n=no";
cin>> buyAgain;
switch(buyAgain) {
case 'y':
break;
case 'n':
//prints receipt
cout<<"***Receipt***"<<endl;
list.printReceipt(head);
}
}
}
The last code box is the main function