IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

find the mistake

Go down

find the mistake Empty find the mistake

Post by BIT0122-Amit Sat Aug 01, 2009 1:49 am

Well guys, I have a challenge for you.
I have typed this program from "Teach yourself c++" by Herbert Schildt. (page 26)

Though the code is "almost similar" when compared to book, I made a mistake/some mistakes here, for which, you can't compile it.

the challenge?
Find the mistake Very Happy
(It took me a while to find the mistake ^_^! )
(AND IF YOU DO FIND THE MISTAKE, DON'T POST IT HERE. JUST SEND THE SOLUTION TO ME VIA PRIVATE MESSAGE Wink )
Code:

#include<iostream>
#include<conio>
using namespace std;
#define SIZE 10;
   //declare a stack class for characters
   class stack{
      char stck[SIZE]; //holds the stack.
      int tos; //index of top of stack;
   
   public:
      void init();//initialize stack
      void push(char ch);//push character on stack;
      char pop();
   };
//initialize the stack
void stack::init(){
   tos=0;
}
//push a character
void stack::push(char ch){
   if(tos==SIZE){
      cout<<"Stack is full";
      return;
   }
   stck[tos] = ch;
   tos++;
   }
//pop a character.
char stack::pop(){
   if(tos==0){
      cout<<"Stack is empty";
      return 0;//return null on empty stacl
   }
   tos--;
   return stck[tos];
}
int main(){
   stack s1, s2; //create two stacks;
   int i;
   //initialize two stacks;
   s1.init();
   s2.init();
   
   s1.push('a');
   s2.push('x');
   s1.push('b');
   s2.push('y');
   s1.push('c');
   s2.push('z');
   
   for(i=0;i<3;i++) cout<<"pop s1: "<< s1.pop() <<".\n";
   for(i=0;i<3;i++) cout<<"pop s2: "<< s2.pop() <<".\n";
   
   getch();
   return 0;
   
}
   

Note: there is no error in the header files. In gcc, just remove the
Code:
#include<conio>
and getch() part.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum