A
Amjad Amer
Guest
Hi ,
I have in my project a Queue of patients , every patient has a stack of prescriptions .
Ive filled the queue from a file ( read from file ) successfully , but when I am trying to fill the stack there is a runtime error occurs in "push" function .
*patient is class
*prescription is class
here the LinkedStack class
#pragma once
#include "stdAfx.h"
#include <iostream>
#include <string>
#include "stack.h"
#include "Link.h"
using namespace std;
using namespace System;
#ifndef LINKEDSTACK_CLASS
#define LINKEDSTACK_CLASS
// This is the declaration for LStack.
// Linked stack implementation
template <typename E>
class LinkedStack : public stack<E>
{
public:
Link<E>* top; // Pointer to first element
int size; // Number of elements
LinkedStack(int sz = 0) // Constructor
{
top = NULL ; size = sz;
}
~LinkedStack() { clear(); } // Destructor
void clear()
{ // Reinitialize
while (top != NULL) { // Delete link nodes
Link<E>* temp = top;
top = top->next;
delete temp;
}
size = 0;
}
void push( const E it)
{
top = new Link<E> (it , top );
size++;
}
E pop()
{ // Remove "it" from stack
E T;
if (top != NULL)
{
E it = top->element;
Link<E>* ltemp = top->next;
delete top;
top = ltemp;
size--;
return it;
}
cout << "Stack is empty\n";
return T ;
}
const E& topValue() const
{ // Return top value
if (top != 0)
return top->element;
else cout << "Stack is empty\n";
return top->element;
}
int length() const { return size; } // Return length
};
#endif
and here the function that read from the file and fill the Queue and the stack
#include "stdAfx.h"
#include "Variabels.h"
#include "LinkedStack.h"
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std ;
LinkedQueue<Patient> Variabels:
Q ;
LinkedList<std::string> Variabels::LL ;
Variabels::Variabels(void) {}
Patient Variabels::ReturnVla(std::string key)
{
Patient P ;
std::string n ;
std::string id ;
presecrption presc ;
std::string A ;
std::string T ;
std::string N ;
std::string ID ;
ifstream P_data("Patient.dat", ios::in) ;
if(P_data.is_open())
{
while ( P_data >> id >> n)
{
if(id == key )
{
P.FileNumber = id ;
P.name = n ;
} //end if id match key
}//end while p_data
P_data.close();
ifstream Pre_data("pre.dat", ios::in);
if( Pre_data.is_open())
{ while( Pre_data >> ID >> N >> A >> T )
{ if( ID == key )
{
presc.amount = A ;
presc.MidicenName = N ;
presc.TimesPerDay = T ;
P.Pre.push(presc);
}//end if
}//end while
Pre_data.close();
return P ;
}//end if pre open
} //if p_data open
return P ;
}//end fun
bool Variabels::Search(std::string key)
{
ifstream P_data("Patient.dat", ios::in);
if(P_data.is_open())
{
std::string n ;
std::string id ;
while ( P_data >> id >> n)
{
if(id == key )
{
P_data.close();
return true ;
}//end if
}//end while
}//end if
P_data.close();
return false ;
}//end function
here the message that appears when the runtime error occurs
An unhandled exception of type System.AccessViolationException occurred in PharmcySystem.DS.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Can you help me please ,
Thanks in advance ,
Continue reading...
I have in my project a Queue of patients , every patient has a stack of prescriptions .
Ive filled the queue from a file ( read from file ) successfully , but when I am trying to fill the stack there is a runtime error occurs in "push" function .
*patient is class
*prescription is class
here the LinkedStack class
#pragma once
#include "stdAfx.h"
#include <iostream>
#include <string>
#include "stack.h"
#include "Link.h"
using namespace std;
using namespace System;
#ifndef LINKEDSTACK_CLASS
#define LINKEDSTACK_CLASS
// This is the declaration for LStack.
// Linked stack implementation
template <typename E>
class LinkedStack : public stack<E>
{
public:
Link<E>* top; // Pointer to first element
int size; // Number of elements
LinkedStack(int sz = 0) // Constructor
{
top = NULL ; size = sz;
}
~LinkedStack() { clear(); } // Destructor
void clear()
{ // Reinitialize
while (top != NULL) { // Delete link nodes
Link<E>* temp = top;
top = top->next;
delete temp;
}
size = 0;
}
void push( const E it)
{
top = new Link<E> (it , top );
size++;
}
E pop()
{ // Remove "it" from stack
E T;
if (top != NULL)
{
E it = top->element;
Link<E>* ltemp = top->next;
delete top;
top = ltemp;
size--;
return it;
}
cout << "Stack is empty\n";
return T ;
}
const E& topValue() const
{ // Return top value
if (top != 0)
return top->element;
else cout << "Stack is empty\n";
return top->element;
}
int length() const { return size; } // Return length
};
#endif
and here the function that read from the file and fill the Queue and the stack
#include "stdAfx.h"
#include "Variabels.h"
#include "LinkedStack.h"
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std ;
LinkedQueue<Patient> Variabels:

LinkedList<std::string> Variabels::LL ;
Variabels::Variabels(void) {}
Patient Variabels::ReturnVla(std::string key)
{
Patient P ;
std::string n ;
std::string id ;
presecrption presc ;
std::string A ;
std::string T ;
std::string N ;
std::string ID ;
ifstream P_data("Patient.dat", ios::in) ;
if(P_data.is_open())
{
while ( P_data >> id >> n)
{
if(id == key )
{
P.FileNumber = id ;
P.name = n ;
} //end if id match key
}//end while p_data
P_data.close();
ifstream Pre_data("pre.dat", ios::in);
if( Pre_data.is_open())
{ while( Pre_data >> ID >> N >> A >> T )
{ if( ID == key )
{
presc.amount = A ;
presc.MidicenName = N ;
presc.TimesPerDay = T ;
P.Pre.push(presc);
}//end if
}//end while
Pre_data.close();
return P ;
}//end if pre open
} //if p_data open
return P ;
}//end fun
bool Variabels::Search(std::string key)
{
ifstream P_data("Patient.dat", ios::in);
if(P_data.is_open())
{
std::string n ;
std::string id ;
while ( P_data >> id >> n)
{
if(id == key )
{
P_data.close();
return true ;
}//end if
}//end while
}//end if
P_data.close();
return false ;
}//end function
here the message that appears when the runtime error occurs
An unhandled exception of type System.AccessViolationException occurred in PharmcySystem.DS.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Can you help me please ,
Thanks in advance ,
Continue reading...