Thread: Error when trying to quit program...

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Unhappy Error when trying to quit program...

    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
    No matter how much you know, you NEVER know enough.

  2. #2
    Unregistered
    Guest

    case

    I think it's with the
    case q:
    case Q:
    thing...

    try,
    toupper(input) = 'Q'


    www.akilla.tk

  3. #3
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    hmm, what's: toupper(input) = 'Q' ?
    No matter how much you know, you NEVER know enough.

  4. #4
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Oh, I tried to take off the case 'q': case 'Q': thing...
    Still gets the error
    Any ideas?
    No matter how much you know, you NEVER know enough.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    This is your problem:

    Code:
    HotDogStand stand[9];
    
    for (int a=0;a<10;a++)
      stand[a].init();
    You allocated space for 9 HotDogStand classes but you used 10 (0-9).
    Increase your buffer by one...

  6. #6
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Geezzzzz, I didn't even notice...
    Stupid me!
    Thanks
    No matter how much you know, you NEVER know enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM