Error in Circular Queue

Oct 27, 2015 at 9:06am
I am not getting any error in the code below but the output is not as desired; that is, when I insert more than 1 values in the array, I only get the first value printed on the screen, rest is followed by 0s.
I am not getting what is wrong with the code, please help me and do consider that I am a beginner, so kindly describe the required steps in detail.



#include<conio.h>
#include<stdio.h>
#include<process.h>
#define max 10
int a[max],front=-1,rear=-1,n;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
insert()
 {
 if((front==rear+1)||(rear==max&&front==0))
  {
  printf("no space available"); 
  }
 else if(front==-1)
 {
   printf("enter element");
   front=0;
   rear=0;
   scanf("%d",&a[front]);
   printf("to exit press 1\n to enter new element press 2");
   scanf("%d",&n);
    switch(n)
    {
    case 1:
      {
      exit(1);
      break;
      }
    case 2:
      {
      insert();
      }
     }
  }
 else if(rear==max  &&  front !=0)
 {
  rear=0;
  scanf("%d",a[rear]);
  printf("to exit press 1\n to enter new element press 2");
  scanf("%d",&n);
  switch(n)
   {
   case 1:
     {
      exit(1);
      break;
     }
   case 2:
     {
      insert();
     }
    }
 }

else
 {
 rear=rear+1;
 scanf("%d",&a[rear]);
 printf("to exit press 1\n to enter new element press 2");
 scanf("%d",&n);
  switch(n)
  {
  case 1:
    { int i;
     for(i=front;i<=rear;i++)
       {
       printf("%d",a[i]);
       }
     break;
    }
  case 2:
     {
      insert();
     }
   }
 }
}

void main()
{
clrscr();
insert();
getch();
}



PS- this is only the 'insertion' function of queue, rest of the program is not displayed here.
Last edited on Nov 3, 2015 at 4:57am
Oct 27, 2015 at 9:44am


First off, please use code tags.

Check your scanf's, where you have scanf("%d",a[rear]); you are missing the &, so it should read scanf("%d",&a[rear]);

Oct 27, 2015 at 10:46am
Hi,

When you do scanf make use of it's return value to see that it worked.

It's int main() not void main()

Try to make use of functions - there seems to be repeated code.

Code Tags:

http://www.cplusplus.com/articles/z13hAqkS/
Oct 29, 2015 at 4:35am
I apologize for that silly mistake (scanf) but even after correcting it, the problem still persists.
And I do not know, how int main and void main differ from each other and their respective functions,yet.
Oct 29, 2015 at 7:02am
Hi,

Can you please read the article about code tags, and edit your first post so that it has them. Use the <> button on the format menu.

int main() is in the c++ standard, it means the program returns an int to the operating system, which is handy for those writing scripts. The other form that is acceptable is int main(int argc, char* argv[]) which allows for command line arguments.

void main() is a hangover from ancient times (pre ANSI C++) particularly with software like Borland Turbo C++ V3.0 , which is apparently and very unfortunately still the standard for the Indian Education Board.
Topic archived. No new replies allowed.