stack

my code is running perfectly but just one line is not executing ..dont know why..i have highlighted it.!

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
71
72
#include<stdio.h>
#include<conio.h>

void push(int*,int,int);
void pop(int*);
int empty(int);
int arr[100];
int main()
{
    int n,top,choice,x,i;
    char z;
    printf("enter size of stack\n");
    scanf("%d",&n);
    top=-1;
    for(i=0;;i++)
    {
           printf("1.push\n2.pop\n");
           scanf("%d",&choice);
           if(choice==1)
           {
                        printf("enter element to be pushed\n");
                        scanf("%d",&x);
                        push(&top,x,n);
           }
           else if(choice==2)
           pop(&top);
           printf("want to stop?(y/n)\n");
           scanf("%c",&z);                                   //this line
           if(z=='y')
           break;
    }
  
    getch();
}
         
void push(int *top, int x,int n)
{
     int i;
     if(*top==n-1)
     printf("stack overflow\n");
     else
     {
         *top=*top+1;
         arr[*top]=x;
     }
     printf("STACK IS:\n");
     for(i=0;i<=*top;i++)
     printf("%d",arr[i]);
     printf("\n\n");
}

void pop(int *top)
{
     int i;
     if(empty(*top))
     printf("stack underflow");
     else
     *top=*top-1;
     printf("STACK IS:\n");
     for(i=0;i<=*top;i++)
     printf("%d",arr[i]);
     printf("\n\n");
}

int empty(int top)
{
     if(top==-1)
     return 1;
     else 
     return 0;
}     
Last edited on
What make you think that that line is not executed?
because when i enter a character, it doesnt take input and loops again . Doesn't matter either i enter 'y' or 'n', it loops again.

though it should be like if i enter 'y', loop should break.
In previous input operation you write a number and then press enter. When you press enter a new line character '\n' is added to the stream. scanf("%d",&x); will only read the number but leaves the new line character in the stream so when you do scanf("%c",&z); it will read the next character in the stream which is the new line character.

The code is much easier to read if the code has indentation. To make the code formatting stay intact you can use the code tags [code] code here [/code].
Last edited on
see...what i mean to say is..when the line "printf("want to stop?(y/n)\n");" is executed..the loop runs again and next statement i.e. "printf("1.push\n2.pop\n");" is executed..it doesnt let me put any value to be scanned ..

in this way..the scanf statement is not getting executed.
There is already a character to read so there is no point in waiting for more input. It is executed and z gets the value '\n'. Of course this is not what you want. You have to somehow remove the new line character before reading the character. I don't know what the best way is because I rarely use C but scanf("\n%c",&z); appears to work when I test it.
Last edited on
ya..its wrking..but wen i just use scanf("%c",&z);, still it doesn't work...why?
I have just told you why.
ya..its wrking..but wen i just use scanf("%c",&z);, still it doesn't work...why?


Peter already answered this, so there's no point just repeating his answer. What about his answer do you not understand?
i mean when i use
1
2
printf("want to stop?(y/n)");
                                        scanf("%c",&z); 


still it doesn't work!!

And also..following profgram is working which should not work coz of \n..
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
#include<conio.h>
int main()
{
     char ch;
     printf("\n");
     scanf("%c",&ch);
     if(ch=='y')
     printf("%c",ch);
     getch();
}

Works fine for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>

using namespace std;

int main()
{
  char z = 'n';
  while (z != 'y')
  {
    printf("want to stop?(y/n)");
    scanf("%c",&z);
  }
}


And also..following profgram is working which should not work coz of \n..

Can't see anything wrong with that program. You print out a newline, and then you gather an input letter and output that if it's 'y'. Works fine. What did you expect it to do?
Last edited on
I rarely use C but scanf("\n%c",&z); appears to work when I test it.
scanf(" %c",&z); would be more idiomatic: it skips all whitespace (newlines, spaces, tabs) and then reads the first non-whitespace character and stores it in z. Just like cin >> z.
Last edited on
Topic archived. No new replies allowed.