C++ queue system

HELP!!!!!!!!!!!

Page 1 of 1

3 Replies - 2014 Views - Last Post: 18 June 2008 - 05:27 AM Rate Topic: -----

#1 liverpool0912   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 04-June 08

C++ queue system

Post icon  Posted 17 June 2008 - 05:47 AM

#include <iostream>

using namespace std;

void addnum (int[], int&);
void removenum (int[], int&);
void sort (int[], int&);
void show (int[], int&);
bool quit( bool);

int main()
{
	bool check = true;
	int a, choice;
	int list[10];
	
	
	while ( check )
	{
		  cout << "***********************************************" << endl;
		  cout << "****************** QUEUE **********************" << endl;
		  cout << "***********************************************" << endl;
		  cout << "1. Add Number" << endl;
		  cout << "2. Remove Number" << endl;
		  cout << "3. Sort" << endl;
		  cout << "4. Show" << endl;
		  cout << "5. Quit" << endl;
		  cout << "Selection: ";
		  cin >> choice;
		  switch ( choice)
		  {
				 case 1 : addnum( list, a);
						  break;
				 case 2 : removenum( list, a);
						  break;
				 case 3 : sort( list, a);
						  break;
				 case 4 : show( list, a);
						  break;
				 case 5 : check = quit( check );
						  break;
				 default : cerr << " Error! Pls input a value between 1 to 5! ";
						  break;		 
		  }
		  cout << endl;
	}
	return 0;
}

void addnum ( int list[10], int& a)
{
	 if ( a <10 )
	 {
		int b;
		
		cout << "Please enter the value you want to add: ";
	   cin >> b;
		list[a++] = b;
	
	 }
	 else
		 cout << endl << "Sorry! Your queue has a maximum of 10 numbers, addition of numbers is not allowed!";
}

void removenum ( int list[10], int& a )
{
	 a--;
}

void sort ( int list[10], int& a )
{
	int temp;
	for ( int b=0; b < a; b++)
	{
		for ( int c=0; c < a-1; c++ )
		{
			if ( list[c] > list[c+1] )
			{
				 temp = list[c];
				 list [c] = list [c+1];
				 list [c+1] = temp;
			}
		}
	}
			 
}

void show ( int list[10], int& a )
{
   cout << "Queue : ";
   for ( int b=0; b < a; b++)
	   cout << list[b] << " ";
   cout << endl;  
}

bool quit ( bool check )
{
	 char a;
	 cout << "Are you sure ( Y or N ) :";
	 cin >>  a;
	 if ( a == 'y' || a == 'Y' )
		return false;
	 else
		 return true;
} 




right heres the problem

when i run my program i typed in option 1 and pressed 1 again to input my value

and i got tis error Unhandled exception at 0x004118fd in C++.exe: 0xC0000005: Access violation writing location 0x33463228. my a value was -858993460 my list value was
-858993460
only my b value was fine help plsssss

and pls hlp me check if my program has any more bugs

Is This A Good Question/Topic? 0
  • +

Replies To: C++ queue system

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: C++ queue system

Posted 17 June 2008 - 05:57 AM

You need to initialize a before passing it to other values. I assumed a default value of zero to start at the beginning of the array/queue. The rest of it seems to work fine.

#include <iostream>
using namespace std;

void addnum (int[], int&);
void removenum (int[], int&);
void sort (int[], int&);
void show (int[], int&);
bool quit( bool);

int main()
{
    bool check = true;
    int a = 0, choice; //a had no value before BAD!!!! always declare variables values before use!!!
    int list[10];
    
    
    while ( check )
    {
          cout << "***********************************************" << endl;
          cout << "****************** QUEUE **********************" << endl;
          cout << "***********************************************" << endl;
          cout << "1. Add Number" << endl;
          cout << "2. Remove Number" << endl;
          cout << "3. Sort" << endl;
          cout << "4. Show" << endl;
          cout << "5. Quit" << endl;
          cout << "Selection: ";
          cin >> choice;
          switch ( choice)
          {
                 case 1 : addnum( list, a);
                          break;
                 case 2 : removenum( list, a);
                          break;
                 case 3 : sort( list, a);
                          break;
                 case 4 : show( list, a);
                          break;
                 case 5 : check = quit( check );
                          break;
                 default : cerr << " Error! Pls input a value between 1 to 5! ";
                          break;        
          }
          cout << endl;
    }
    return 0;
}

void addnum ( int list[10], int& a)
{
     if ( a <10 )
     {
        int b;
        
        cout << "Please enter the value you want to add: ";
       cin >> b;
        list[a++] = b;
    
     }
     else
         cout << endl << "Sorry! Your queue has a maximum of 10 numbers, addition of numbers is not allowed!";
}

void removenum ( int list[10], int& a )
{
     a--;
}

void sort ( int list[10], int& a )
{
    int temp;
    for ( int b=0; b < a; b++)
    {
        for ( int c=0; c < a-1; c++ )
        {
            if ( list[c] > list[c+1] )
            {
                 temp = list[c];
                 list [c] = list [c+1];
                 list [c+1] = temp;
            }
        }
    }
            
}

void show ( int list[10], int& a )
{
   cout << "Queue : ";
   for ( int b=0; b < a; b++)
       cout << list[b] << " ";
   cout << endl;  
}

bool quit ( bool check )
{
     char a;
     cout << "Are you sure ( Y or N ) :";
     cin >>  a;
     if ( a == 'y' || a == 'Y' )
        return false;
     else
         return true;
} 


Was This Post Helpful? 0
  • +
  • -

#3 linuxunil   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 07-March 06

Re: C++ queue system

Posted 17 June 2008 - 08:24 AM

After questioning my self I found out my statement was wrong and removed it. Thats what I get for trying to use rusty c++ skills.

This post has been edited by linuxunil: 17 June 2008 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 liverpool0912   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 04-June 08

Re: C++ queue system

Posted 18 June 2008 - 05:27 AM

thx for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1