access violation segmentation fault raised in your program

This is a discussion on access violation segmentation fault raised in your program within the C++ Programming forums, part of the General Programming Boards category; Hi folks! I have written one program, which works ok, compiles etc. But I have one problem with it: when ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    access violation segmentation fault raised in your program

    Hi folks! I have written one program, which works ok, compiles etc. But I have one problem with it: when i am trying to debug it i am getting the following error: "access violation segmentation fault raised in your program", it just pops up as a window and i can't even debug my program at all. I am using devc++ with it.If any one know how to fix this stupid proble, please reply asap.Any help is appreciated.Thanks.
    Heres the code:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<conio.h>
    
    using namespace std;
    
    const int MAX = 10;
    
    class LinearQueue
    {
          private:
    	  int queue[MAX];
    	  int rear;
    	  int front;
    
          public:
    	  //LinearQueue();
    
          void Queue();    
    	  void Delete();
    	  void Insert();
    	  void print_queue();
    	  void show_working();
    };
    
    void LinearQueue::Queue()
    {
             rear=-1;
             front=-1;
    
             for(int count=0;count < MAX;count++)
    	     queue[count]=0;
    }
    
    void LinearQueue::Insert()
    {
    int item;
    
             cout << setw(40) <<  "\n\n\n\n\n\t Enter value to insert into the Queue : " << endl << endl;
             cin >> item;
    
    if(rear == (MAX-1))
    	     cout << "\n\n\t ***  Error : Queue is full. \n" << endl;
    
    else if(rear==-1 && front==-1)
    	  {
    	     rear++;
    	     queue[rear]=item;
    	     front=rear;
    
    	     cout << "\n\n\t *** " << item << " is inserted into the Queue." << endl;
    	  }
    else
    	  {
    	     rear++;
    	     queue[rear]=item;
    
    	     cout << "\n\n\t *** " << item << " is inserted into the Queue." << endl;
          }
             cout << "\n\n\n\t\t Press any key to return to Menu " << endl;
          
             cin.get();
          
    }
    
    void LinearQueue::Delete()
    {
    if(rear==-1 && front==-1)
    	     cout << "\n\n\n\t ***  Error : Queue is empty. \n" << endl;
    
    else
    	  {
    	     cout << "\n\n\n\t *** " << queue[front] << " is deleted from the Queue." << endl;
    
    	     queue[front]=0;
    
    if(front==rear)
    		 front=rear=-1;
    
    else
    		 front++;
    	  }
    
             cout << "\n\n\n\t\t Press any key to return to Menu. " << endl;
    
             cin.get();
    }
    
    void LinearQueue::print_queue()
    {
    if(front!=-1 && rear!=-1)
    	  {
    	     cout << "\n\n\n\n\n\t Values inserted into the Queue are : \n" << endl;
    
    for(int count=front;count<=rear;count++)
    		 cout << "\t Stack [" << count << "]  =  " << queue[count] << endl;
    	  }
    
    else
    	     cout << "\n\n\n\n\n\t *** Nothing to show. " << endl;
    
             cout << "\n\n\n\t\t Press any key to return to Menu. " << endl;
    
             cin.get();
    }
    
    void LinearQueue::show_working()
    {
    char Key;
    
    do
    	  {
    	     system("CLS");
    
             cout << setw(40) << "                      " << endl;
             
    	     cout << setw(53) <<  "***Linear ***** MAIN MENU ***** Queue*** " << endl;
    
             cout << setw(40) << "                      " << endl;
             
    	     cout << setw(49) << "Select one of the listed operation : " << endl;
    	     cout << setw(42) << "Press 'I' to Insert a value : " << endl;
    	     cout << setw(42) << "Press 'D' to Delete a value : " << endl;
    	     cout << setw(44) << "Press 'P' to Print the values : " << endl;
    	     cout << setw(31) << "Press 'E' to Exit: " << endl;
    	     Input:
    	     cout << setw(40) << "                      " << endl;
    	     cout << setw(39) << "Please Enter Your Choice : " << endl;
    
    	     Key = cin.get();
    
    if(int(Key)==27 || Key=='e' || Key=='E')
    break;
    
    else if(Key=='i' || Key=='I')
    		Insert( );
    
    else if(Key=='d' || Key=='D')
    		Delete( );
    
    else if(Key=='p' || Key=='P')
    		print_queue();
    
    else
    goto Input;
    	  }
    while(1);
    }
    
    
    int main()
    {
           LinearQueue obj;
    
           obj.show_working();
    
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely you can use another do while for your loop instead of using goto?

    I also fail to see the purpose of this line:
    Code:
             cout << setw(40) << "                      " << endl;
    Wouldn't
    Code:
             cout << endl;
    do quite the same thing?


    Code:
    if(int(Key)==27 || Key=='e' || Key=='E')
    is the same as
    Code:
    if(Key==27 || Key=='e' || Key=='E')
    so why do you have a cast? [Any chance you've been programming Basic before?]

    Sorry, can't say where you are do get things wrong in the Insert function - I've got a train to catch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Thanks! I did basic C++ 2 years ago. Those lines of code that you have highlighted are not causing the problem, but thanks anyway. When i am trying to debug it gives me the message and it is said that error is in the following piece of code:
    Code:
    void LinearQueue::Insert()
    {
    int item;
    
             cout << setw(40) <<  "\n\n\n\n\n\t Enter value to insert into the Queue : " << endl << endl;
             cin >> item;
    
    if(rear == (MAX-1))
    	     cout << "\n\n\t ***  Error : Queue is full. \n" << endl;
    
    else if(rear==-1 && front==-1)
    	  {
    	     rear++;
    	     queue[rear]=item;
    	     front=rear;
    
    	     cout << "\n\n\t *** " << item << " is inserted into the Queue." << endl;
    	  }
    else
    	  {
    	     rear++;
    	     queue[rear]=item; ///*** this is the line that causes the problem***
    
    	     cout << "\n\n\t *** " << item << " is inserted into the Queue." << endl;
          }
             cout << "\n\n\n\t\t Press any key to return to Menu " << endl;
          
             cin.get();
          
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does your class have a constructor that gives rear and front an initial value of -1? I don't think so...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Quote Originally Posted by anon View Post
    Does your class have a constructor that gives rear and front an initial value of -1? I don't think so...
    I think i doesn't , can you tell me please what kind of construct should it be and where should i put it in the code?Thanks

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
          public:
    	  //LinearQueue();
    This is the correct declaration of a default constructor.

    Code:
    void LinearQueue::Queue()
    {
             rear=-1;
             front=-1;
    
             for(int count=0;count < MAX;count++)
    	     queue[count]=0;
    }
    I suppose this was supposed to be the implementation. However, constructors don't have return type and the name is the same as the name of the class:

    Code:
    LinearQueue::LinearQueue()
    {
        ...
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Quote Originally Posted by anon View Post
    Code:
          public:
    	  //LinearQueue();
    This is the correct declaration of a default constructor.

    Code:
    void LinearQueue::Queue()
    {
             rear=-1;
             front=-1;
    
             for(int count=0;count < MAX;count++)
    	     queue[count]=0;
    }
    I suppose this was supposed to be the implementation. However, constructors don't have return type and the name is the same as the name of the class:

    Code:
    LinearQueue::LinearQueue()
    {
        ...
    }

    I have changed my code as you have posted:
    now i have:
    [QUOTE=anon;857312]
    Code:
          public:
    	  LinearQueue();
    Code:
    void LinearQueue::LinearQueue()
    {
             rear=-1;
             front=-1;
    
             for(int count=0;count < MAX;count++)
    	     queue[count]=0;
    }
    but I am geting another error "return type specification for constructor invalid"
    and now program doesn't compile. If you want you can copy and paste the code and try to run it yourself, and you'll see what type of errors come up then.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    remove "void" from that, and you'd be good to go.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Thanks for your help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Access Violation?
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 10:56 AM
  5. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 07:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21