hello! im trying to make my own stack program in c++. but my code doesnt seem to work. I got 6 errors, been trying to figure out what are those and how to solve it but theres no progress. Can somebody help me with my project. I would greatly appreciate any help please?

by the way here is mycode..
http://pastebin.com/1bzr1MhZ

for now, i have seen several errors:
1 - cout is from std namespace, so after #includes use: using namespace std;
2 - you use a function before create it: DisplayStack(), put these function before is used. i know what you mean, but C\C++ do these by order.
3 - where is the 'if', if you use the 'else', you must use the 'if':

void DisplayStack()

      { int i;
            cout<<"Contents:";


               for(i=0;i<=top;i++)
            cout<<"s[i]"<<"  ";
               else
         cout<<"Stack Empty\n";

         }

4 - theres isn't clrscr() sorry. only in Turbo IDE and conio2.h or something.
but you can use:

system("cls");

and add: #include <stdlib.h>

5 - the main must return a value so:
returntype main()

int main()

you return a value but you didn't said the type to the compiler ;)

please correct these function: DisplayStack()... because we don't know what you want ;)

Edited 2 Years Ago by cambalinho

while(o!=9);

Did you meant ot have that semi-colon? This code is the same as this:

while(o!=9)
{
};

Your code will loop forever, doing nothing.

Anyway, here is how to ask a question that will get an answer:

Present the code.
Tell us what you did (e.g. what you typed into the keyboard).
Tell us what happened.
Tell us what you think should have happened, or what you want to happen.

Edited 2 Years Ago by Moschops

heres the code more fixed and indented:

#include<iostream>
#include<conio.h>
#include <stdlib.h>

using namespace std; //or you use these line or you must do std::cout or std::cin

int s[10]={0,0,0,0,0,0,0,0,0,0}, top=-1, o=0;

//never forget the C\C++ read the code from top to bottom, so the functions order are important
void DisplayStack()
{
    int i=0;//1 variable not inicializated have random values(whtat is in memory)
    cout<<"Contents:";
    for(i=0;i<=top;i++)//whereis the 'if'????
        cout<<"s[i]"<<"  ";
    else
        cout<<"Stack Empty\n";
}

void CreateStack()
{
    int i=0;
    top=-1;
    for(i=0;i<=9;i++)
        s[i]=0;
    cout<<"Stack Created\n";
    getch();
}

int IsFull()
{
    if(top==9)
        return 1;
    else
        return 0;
}

int IsEmpty()
{
    if (top==-1)
        return 1;
    else
        return 0;
}

void push(int x)
{
    if(!IsFull())
    {
        top ++;
        s[top]=x;
        DisplayStack();
    }
    else
        cout<<"Stack Full\n";
}

void PushStack()
{
    int n=0;
    cout<<"Enter item to Add";
    cin>>n;
    push(n);
    getch();
}

void PopStack()
{
    if(!IsEmpty())
    {
        top--;
        DisplayStack();
    }
    else
        cout<<"Stack Empty\n";
    getch();
}

void TopStack()
{
    cout<<"Top of Stack:"<<s[top]<<"n";
    getch();
}

void CheckEmpty()
{
    if(IsEmpty())
        cout<<"Stack Empty\n";
    else
        DisplayStack();
    getch();
}

void CheckFull()
{
    if(IsFull())
        cout<<"Stack Full\n";
    else
        DisplayStack();
    getch();
}

void GetSize()
{
    cout<<"No. of elements"<<top+1<<"\n";
    getch();
}

//the main must have a return type even if is void
int main()
{
    while(o!=9)//i think these is better for a menu
    {
        system("cls");//there isn't a clrscr() function, only on olders compilers like Turbo C\C++
        cout<<"Options\n";
        cout<<"[1]Create\n";
        cout<<"[2]Push\n";
        cout<<"[3]Pop\n";
        cout<<"[4]Top\n";
        cout<<"[5]IsEmpty\n";
        cout<<"[6]IsFull\n";
        cout<<"[7]GetSize\n";
        cout<<"[8]DisplayContents\n";
        cout<<"[9]Exit\n";
        cout<<"Enter Choice:";
        cin>>o;

        //i changed these if
        if(o==1)
            CreateStack();
        else if(o==2)
            PushStack();
        else if(o==3)
            PopStack();
        else if(o==4)
            TopStack();
        else if(o==5)
            CheckEmpty();
        else if(o==6)
            CheckFull();
        else if(o==7)
            GetSize();
        else if(o==8)
            DisplayStack();
    }        
    getch();
    return 0;
}

now the big problem is the DisplayStack() function. so think better waht you need

sorry Moschops, but he can't execute the code, because of several errors ;)

So he should have said this:

Here is my code:
<insert code here>

Here is what I did:
<Tried to compile it>

Here are the compilation errors I got:
<Insert compilaton errors>

Comments
Indeed!
This article has been dead for over six months. Start a new discussion instead.