checking

This is a discussion on checking within the C++ Programming forums, part of the General Programming Boards category; i know the answer don't read this i've written a programme and this was the question of it we read ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    checking(thanku i knew the answer cancel this thread)

    i know the answer don't read this

    i've written a programme and this was the question of it

    we read the numbers and push them into the stack without printing them until we read a negative number at this time we stop reading and pop five items from the stack and print them if there are fewer than 5items in the stack we print error message after printing the five items we resume reading data and placing them in the stack when the end of the file is detected we print a message and the item remaining in the stack.

    and i want to do it in two files one is a .dat file and other is to read from the .dat file i've did it and put the two files in the same directory but it doesn't work and there are my 2files:

    Code:
    //file name lab66.dat
    #include <stdio.h>
    #include <malloc.h>
    
    struct node{
    	int data;
    	node *link;
    	node(int x){
    		data = x;
    		link=NULL;}
    
    };
    
    class stack{
    
    private:
    	node *tos;
        int s;
      
    
    public:
    	int _size(){   //return the size of the stack
    	  return s;}
    	                
    	
    	stack(int x=0){  //constructor
                    tos=NULL;
    	s=x;}
        
    
    	void push(int x){
    		node *ptr;
    
    		if(!tos){
    			tos= new node(x);  return;}
          
    		ptr=new node(x);
            ptr->link=tos;
    		tos=ptr;
    	
    		++s;
    	}
    	
    	
    	int pop(){
                                    int x;
    		node *ptr;
    		ptr = tos;
    		if(!ptr){printf("The stack is empty\n");
    		         return 0;}
    
    		x=ptr->data;
    		tos=ptr->link;
    		delete ptr;
                                    --s;
    		return x;
    	}
    
    void print(){
    
                      if(!tos)
    	{printf("Cannot print The stack is empty\n");             
                                  return;}
                                   node *ptr;
    		ptr=tos;
    		while(ptr){
    			printf("%d ",ptr->data);
    			ptr=ptr->link;
    		}
              printf("\n");
    	}
    
    
    };
    
    
    
    
    //file2 lab6.cpp
    #include <stdio.h>
    
    int main(){
    
      FILE *f;
      int r,i;
      
      f=fopen("lab66.dat","r");
      if(f==NULL){
    		printf("File doesn't exist\n");
    	return -1;}
         
        r=fscanf(f,"%d",&i);
    	stack t;   
    	while(r!=EOF){
    		   
    		   if(i>=0)
    	                      t.push(i);
    		   
    		   else {if(t._size>=5)
    		    for(int j=0;j<5;j++) 			                    printf("%d",t.pop());
                                         printf("\n\n");
    		   
    		             else
    		              printf("Error\n");}
             while(t._size()>0)
    	t.pop();
             while(t._size()>0)
    	printf("%d", t.pop());
    
           r=fscanf(f,"%d",&i);
    	   }
    	
    return 0;
    }

    shall i include the two files in one file and make a new file that have the test data numbers if so how can i do it can u please help me?
    Last edited by kuwait; 04-07-2003 at 04:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21