Stacks without class - URGENT help needed

Pages: 12
I used Visual Studio's automatic formatting [Edit -> Advanced -> Format Selection (in 2010, I think 2012 lets you format the entire document)] to indent your code properly, and the problem becomes clear as day. Your braces don't balance where you think they do, and your indentation is making your problem harder to see. (Missing a closing brace at line 21)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<stdlib.h>
using namespace std;

const int stackSize = 5;
int stack[stackSize];
int top;

void push (int x)
{
    if ( top == stackSize )
    {
        cout << "Unsuccessful push (" << x << ")\n";
    }
    else
    {
        stack[top] = x ;
        ++top ;
        cout << "Successful push (" << x << ")\n";
    }

    void pop()
    {
        if ( top > 0 )
        {
            cout<<"pop is successful"<<endl;
            --top;
        }
        else
        {
            cout<< "pop is unsuccessful, stack is underflowed"<<endl;
        }
    }


    void display()
    {
        if (top<0)
        {
            cout<<"stack is empty"<<endl;
        }
        else
        {
            for (int i = top; i>0;i--)
                cout<<stack[i]<<" "<<endl;
        }
    }



    int main()
    {
        while(1)
        {
            int choice ;
            cout<<" Welcome to the stack implemention"<<endl;
            cout<< "Please look at the following options"<<endl;
            cout<<" key in the corresponding number to sleect that option"<<endl;
            cout<<" 1. PUSH 2. POP 3. DISPLAY 4. Exit\n"<<endl;

            cout<<" Please enter your choice\n"<<endl;
            cin>>choice;

            switch(choice)
            {
            case 1:
                {
                    int num ;
                    cout << "Enter a number\n" ;
                    cin >> num ;
                    push(num) ;
                    break ;
                }

            case 2: cout<<"you have popped an element out!"<<endl;
                pop();
                break;

            case 3: display();
                break ;

            case 4: break;


            }
        }

    }
Last edited on
oh my! thank you, i was using MS until my student copy messed up, now im using codeblocks, thank you! i shall post with more info soon :)
Topic archived. No new replies allowed.
Pages: 12