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

#define size 5

int stack[size],top=-1;

void push(int s[size],int &t,int m);

int pop (int s[size],int &t);

void main()
{
  clrscr();
  cout<<"1-push \n 2-pop \n 3-exit \n";
  int x,y;
  do
  {
    cout<<"enter number to choice:";
    cin>>x;
    switch(x)
    {
      case 1:
        cout << "enter anumber to push in stack \n";
        cin >> y;
        push(stack, top, y)
        break;
      case 2:
        y = pop(stack, top);
        if(y == -1)
          cout << "\n empty \n";
        else
          cout << "\n popis ok \n y=" << y << "\n";
        break;
    }
  } while( x != 3 );

  getch();
}

void push(int s[size], int &t, int m)
{
  if(t == size-1)
    cout << "the stack is full \n";
  else
    s[++t] = m;
}

int pop(int s[size], int &t)
{
  if(t==-1)
    return -1;
  else 
    return s[t--];
}

Recommended Answers

All 12 Replies

void main()

That's not C++.

Do you have a question?

As for the code, there are several problems with it:

  • The header #include <iostream.h> has been deprecated for a long time, it is a pre-standard syntax that some compilers still support today, but it is not standard. The standard libraries do not have a ".h" after them, e.g., #include <iostream>. Also, for C libraries (like stdlib.h or math.h), the ".h" is omitted and they are prefixed with the letter "c" (like #include <cstdlib> or #include <cmath>).
  • The header #include <conio.h> dates back to MS-DOS and is also deprecated. Not many up-to-date compilers still provide that header, and I think that only older versions of Windows can still run it. And, of course, it's not portable.
  • The definition #define size 5 breaks the conventions used in C/C++. Definitions like these (and macros) should be in upper-case letters entirely, and should have a much more unique name than simply "size". Replace with something like #define MY_STACK_SIZE 5.
  • You cannot use just cout. You must use either std::cout or issue the following statement before using cout: using namespace std;. Your code works only because it uses the pre-standard header "iostream.h" which does not define cout inside the std namespace as prescribed by the standard. The same goes for cin, and for absolutely everything else from the standard library (including functions from C headers like <cmath> or <cstdlib>).
  • The main function should not have the signature void main(). There are only two acceptable signatures for the main function (since standardization), which are: int main() or int main(int argc, char** argv) (if capturing command-line arguments).
  • The lines clrscr(); and getch(); are taken from the conio.h header and should be removed for better portability. The getch(); function can be replaced by std::cin.get() without much difference in functionality.
  • The main function should return an integer value of zero to signify that the program executed correctly (otherwise, return a non-zero value). So, add the line return 0; before the end of the main function.
  • Passing a static array as int s[size] in a function is not very nice. In C++, people like things to be clear. And, in C++, a parameter of type int s[size] actually decays to simply int* s (i.e., you cannot pass a static array by value, it always decays to a pointer to the first element). To be polite, you should avoid that syntax, prefer int* s or int s[] (or even int (&s)[size] is better), which all mean the same thing.

The header #include <conio.h> dates back to MS-DOS and is also deprecated. Not many up-to-date compilers still provide that header, and I think that only older versions of Windows can still run it.

It was never part of the C or C++ standards, so it can't be deprecated. There are only two compilers I know of that support it: Borland and Microsoft. I don't know about Borland, but it's still supported by Visual Studio 2012.

As for the rest of your comments, he is probably using Turbo C. That was a valid program, if written 30 years ago.

It was never part of the C or C++ standards, so it can't be deprecated.

Well, not deprecated by the C++ standard, but deprecated by the people providing it, i.e., Microsoft. When you use function from conio.h, with highest warning levels (in VS2012), don't you get a message saying something like "warning use of this is function is deprecated" or something like that? Similar to when you use Win32 API functions dating to before all the post-XP strengthening of security. At least I thought so. In any case, if Microsoft hasn't officially deprecated it. It is certainly informally deprecated, as in, obsolete and no longer recommended.

As for the rest of your comments, he is probably using Turbo C. That was a valid program, if written 30 years ago.

Yeah, I had that hunch too. I don't know how Turbo C managed to stay alive like that and constantly being used in courses that teach C++, even today. I guess just a lot of professors who have been teaching the same course every year for 20 years without ever updating the content, maybe their course-notes still read "Introduction of 'C with Classes'" ;)

Similar to when you use Win32 API functions dating to before all the post-XP strengthening of security. At least I thought so. In any case, if Microsoft hasn't officially deprecated it.

Nope. I set it to warning level 4 and no warnings, after changing <iostream.h> to <iostream> and adding the using statements.

One error: line 26 needs a semicolon at the end of the line.

Microsoft didnt warn about void main()?

Microsoft didnt warn about void main()?

No, it produced no warnings at all.

Well give a round of applause to Microsoft for that. It has only been standard since 1998. I guess they havent found the time yet.

That was a valid program, if written 30 years ago.

If by "valid", you mean "it compiles". The void main() means it's techincally not legal C++.

You must use either std::cout or issue the following statement before using cout: using namespace std;

I personally use like to include only the parts of the std namespace that I want to use, so I can use what I want without getting naming conflicts from unrelated parts of the namespace, like so: using std::cout;

So, add the line return 0; before the end of the main function.

I think the standard says that main() will return 0 automatically.

The void main() means it's techincally not legal C++.

I think it was 30 years ago, before the first c++ standards.

I think the standard says that main() will return 0 automatically.
True for C, but not C++

I think it was 30 years ago, before the first c++ standards.

C++ never supported void main() officially, even at the time of C with Classes. That was always a compiler extension, just as it is now.

I think the standard says that main() will return 0 automatically.

True for C, but not C++

C++ has supported an implicit return from main() since C++98, and C has supported it since C99.

C++ has supported an implicit return from main() since C++98, and C has supported it since C99.

Yes, I was just testing you to see if you knew it too :) Guess I'm getting obsolete.

commented: ;) +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.