Hi everyone
I coded a small program, it works well except when I try to quit it by pressing 'q' or 'Q'. When I do this, an error message comes up saying "The memory could not be read".
Code:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
class HotDogStand
{
private:
int HotDogsOnHand;
int BunsOnHand;
double CashOnHand;
int status;
public:
void displayData()
{
cout<<"\t[ Hot dogs on hand = "<<HotDogsOnHand<<" ]"<<endl;
cout<<"\t[ Buns on hand = "<<BunsOnHand<<" ]"<<endl;
cout<<"\t[ Cash on hand = "<<CashOnHand<<" ]"<<endl<<endl;
}
void initData()
{
status = 1;
cout<<"\t::Hot dogs on hand:: \t[ ]\b\b\b\b\b";
cin>>HotDogsOnHand;
cout<<"\t::Buns on hand::\t[ ]\b\b\b\b\b";
cin>>BunsOnHand;
cout<<"\t::Cash on hand::\t[ ]\b\b\b\b\b"<<flush;
cin>>CashOnHand;
}
void SoldOneDog()
{
--HotDogsOnHand;
--BunsOnHand;
CashOnHand = CashOnHand + 1.95;
}
int statusreturn()
{
return status;
}
void init()
{
HotDogsOnHand = 0;
BunsOnHand = 0;
CashOnHand = 0;
status = 0;
}
};
void main()
{
HotDogStand stand[9];
for (int a=0;a<10;a++)
stand[a].init();
int n = 0,inittemp, saletemp, reporttemp, report2temp;
char choice = 'x';
while (choice !='q' && choice !='Q' && n < 10)
{
cout<<flush;
system("CLS");
cout<<"\n\tHOT DOG STAND [price per hotdog: $1.95]\n\n";
cout<<"\n\t[[[MENU]]]"<<endl<<endl;
cout<<"\t::Initialize/Change Stand Data [i]::"<<endl;
cout<<"\t::Record a Sale [s]::"<<endl;
cout<<"\t::Report Data [r]::"<<endl;
cout<<"\t::Quit Program [q]::"<<endl;
cout<<"\n\tEnter Choice [[ ]]\b\b\b"<<flush;
choice = getche();
switch(choice)
{
case 'i':
case 'I':
{
system("CLS");
cout<<"\n\t[ Initialize/Change Data for Stand (1-10) : ]\b\b\b\b\b";
cin>>inittemp;
cout<<endl<<endl;
stand[inittemp-1].initData();
system("CLS");
cout<<"\n\n\tStand "<<inittemp<<" initialized/changed.\n\n\t[ Press any key to continue to main menu ]"<<flush;
getch();
break;
}
case 's':
case 'S':
{
system("CLS");
cout<<"\n\n\t[ Record Sale for Stand (1-10) : ]\b\b\b\b\b";
cin>>saletemp;
if (stand[saletemp-1].statusreturn() == 1)
{
system("CLS");
cout<<"\n\n\t[[ Selling Hot Dog at Stand "<<saletemp<<"... ]]"<<flush;
stand[saletemp-1].SoldOneDog();
Sleep (700);
cout<<"\n\n\t::Hot Dog SOLD::\n\n\t[ Press any key to continue to main menu ]"<<flush;
getch();
}
else
{
system("CLS");
cout<<"\n\n\tThe Stand you are trying to access does not exist."<<flush;
cout<<"\n\n\t[ Press any key to continue to main menu ]"<<flush;
getch();
}
break;
}
case 'r':
case 'R':
{
system("CLS");
cout<<"\n\n\t[1] Report Data for all Stands..."<<flush;
cout<<"\n\t[2] Report Data for a certain Stand..."<<flush;
cout<<"\n\n\tEnter choice: [ ]\b\b"<<flush;
reporttemp = getche();
if (reporttemp == '1')
{
system("CLS");
for (int j=0;j<3;j++)
{
cout<<"\tData for Stand "<<j+1<<"..."<<endl<<endl;
stand[j].displayData();
}
cout<<"\n\n\t[ Press any key to see next 3 Stands ]"<<flush;
getch();
system("CLS");
for (j=3;j<6;j++)
{
cout<<"\tData for Stand "<<j+1<<"..."<<endl<<endl;
stand[j].displayData();
}
cout<<"\n\n\t[ Press any key to see next 3 Stands ]"<<flush;
getch();
system("CLS");
for (j=6;j<9;j++)
{
cout<<"\tData for Stand "<<j+1<<"..."<<endl<<endl;
stand[j].displayData();
}
cout<<"\n\n\t[ Press any key to see last Stand ]"<<flush;
getch();
system("CLS");
cout<<"\tData for Stand 10..."<<endl<<endl;
stand[9].displayData();
cout<<"\n\n\t[ Press any key to continue to main menu ]"<<flush;
getch();
}
else if (reporttemp == '2')
{
system("CLS");
cout<<"\n\n\t[ Report Data for Stand (1-10) : ]\b\b\b\b\b";
cin>>report2temp;
system("CLS");
cout<<"\n\n\tData for Stand "<<report2temp<<"..."<<endl<<endl;
stand[report2temp-1].displayData();
cout<<"\n\n\t[ Press any key to continue to main menu ]"<<flush;
getch();
}
break;
}
case 'q':
case 'Q':
{
system("CLS");
cout<<"\n\n\t[ Quiting Program... ]"<<flush;
Sleep(1000);
break;
}
};
};
}
I'm trying to find some logical or runtime error I might have coded but I can't find one
Any ideas?
Marc