errors that im not sure how to solve

Discussion in 'C++' started by elsa87, Nov 24, 2008.

  1. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    hi everyone..
    i need to ask 2 questions..
    first..is the below code done using recursion or not?
    second..i have 2 errors in the following code..they relate to x.push(item); and x.print_reverse();
    the code is here:
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<cstdlib>
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include"ItemType.h"
    using namespace std;
    class stack
    {
    private:
            static const int MAX=10;  
            int st[MAX];
            int top;
    public:
                   stack():top(-1){}
                   void push(int v){st[++top]=v;}
                   int pop(){return st[top--];}
                   bool isempty(){return (top==-1);}  
                   bool isfull(){return top==MAX-1;} 
    
    void print()
    {
    while(!isempty())
    cout<<pop()<<endl;
    }
    void print_reverse(int i)
    {
    if( i < MAX)
            		print_reverse(++i);
    cout << st[i] << "\n";
    }};
    int main()
    {  
    stack x;
    int data;
    ItemType item;
    
    	ifstream instream; 
    	ofstream outstream;
    
    while(!x.isfull()) 
    {
    
    	while ( !instream.eof() )
    	{	
    		instream>> data;	
    		item.Initialize(data);
    		x.push(item); 
    		x.print();
    
    	}
    	instream.close();
    }
    cout<<"--------\n";
    x.print_reverse();
    cin.get();
        return 0;
    }
    
     
    Last edited: Nov 24, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Recursion is where a function calls itself (you can also get mutually recursive functions where two recurse into each other, e.g. f(){g();} g(){f();}). Can you find a function where this happens? (Yeah I know, here I go again. But if I just say "Yes" or "No" then you won't learn how to spot whether or not recursion is happening, and the point of the exercise is that you learn how to spot, not that you learn how to get others to answer questions for you. If you want to learn that then drop programming and get into a management class instead.)

    push cannot convert ItemType to int, that's correct. You define push as a function that takes an int:
    Code:
    void push(int v){st[++top]=v;}
    
    but you call it with an ItemType rather than an int:
    Code:
    ItemType item;
    ...
    x.push(item);
    
    So to fix the error you need to change push() to take an ItemType, or change the call to pass an int.


    print_reverse does not take 0 arguments.
    This is quite self explanatory. How many arguments does print_reverse() have, in its definition, and what are they for? How many are you calling it with? Is there a difference?
     
  3. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    mmmm i solved this problem but the program now doesnt do what i want it to do..i just wanted to display the items in the input.txt and then reverse these items in the stack..can u help?
    here is the new code:
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<cstdlib>
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include"ItemType.h"
    using namespace std;
    class stack
    {
    private:
            static const int MAX=10;  
            int st[MAX];
            int top;
    public:
                   stack():top(-1){}
                   void push(int v){st[++top]=v;}
                   int pop(){return st[top--];}
                   bool isempty(){return (top==-1);}  
                   bool isfull(){return top==MAX-1;} 
    
    void print()
    {
    while(!isempty())
    cout<<pop()<<endl;
    }
    
    void print_reverse(int i)
    {
       if(i<MAX)
       {
          for(int j=i;j>=0;j--)
             cout<<st[j];
       }
    }
    };
    int main()
    {  
    stack x;
    int data;
    ItemType item;
    
    	ifstream instream; 
    	ofstream outstream;
    instream.open("input.txt");
    	if(instream.fail()){
        	cout<<"Error opening file\n";
    exit(1);
    	}
    outstream.open("output.txt");
    	if (!outstream) 
    		return 0; 
    
    while(!x.isfull()) 
    {
    
    	while ( !instream.eof() )
    	{	
    		instream>> data;	
    		item.Initialize(data);
    		 int temp = item.getvalue();
    		x.push(temp); 
    		x.print();
    		
    
    	}
    	cout<<"--------\n";
    	x.print_reverse(data);
    	instream.close();
    cin.get();
    }
    outstream.close();
        return 0;
    }
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why did you change print_reverse()?
     
  5. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    because another guy is andivising me to do so..but now the program has changed upside down and still not working..it has an error
    here is the code:
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<cstdlib>
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include"ItemType.h"
    using namespace std;
    class stack
    {
    private:
            static const int MAX_ITEMS=10;  
            int st[MAX_ITEMS];
            int top;
    public:
    	
    	stack();
    	void push(int);
    	int pop();
    	bool isempty();
    	bool isfull();
    	void print();
    	void print_reverse(int);
    };
    
    stack::stack(){top=-1;}
    void stack::push(int v){st[++top]=v;}
    int stack::pop(){return st[top--];}
    bool stack::isempty(){return (top==-1);}
    bool stack::isfull(){return top==MAX_ITEMS-1;}
    void stack::print()
    {
    while(!isempty())
    cout<<pop()<<endl;
    }
    
    void print_reverse(int i)
    {
    	
       if(i<MAX_ITEMS && i >= 0)
       {
             cout<<st[i]<<endl;
             print_reverse(--i);
       }
    }
    
    
    int main()
    {  
    stack x;
    int data;
    ItemType item;
    
    	ifstream instream; 
    	ofstream outstream;
          
    	instream.open("input.txt");
    	outstream.open("output.txt");
    	if(instream.fail()){
        	cout<<"Error opening file\n";
    	exit(1);
    	}
    
    	while ( !instream.eof() )
    	{	
    		if (x.isfull())
    			break;
    		instream>> data;	
    		item.Initialize(data);
    		 int temp = item.getvalue();
    		x.push(temp); 
    		x.print();
    		
    	}
    	instream.close();
    
    	cout<<"--------\n";
    	x.print_reverse(MAX_ITEMS-1);
    	cin.get();
    	outstream.close();
        return 0;
    }
     
    Last edited: Nov 25, 2008
  6. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    im sorry..it slipped my mind that i should be doing it using a linked list implementation..
    here is my try but there are too many errors:
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<cstdlib>
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include"ItemType.h"
    using namespace std;
    
    class stack
    {
    private:
            NodeType* topPtr;
    public:
    	
    	stack();
    	void push(ItemType);
    	int pop();
    	bool isempty();
    	bool isfull();
    	void print();
    	void print_reverse(stack);
    	void reverse(Node*)
    	void push(stack, int);
    };
    
    stack::stack()
    {
    	topPtr = NULL;
    }
    void stack::push(int ItemType newItem)
    {
    	NodeType*  location;
    	location = new  NodeType;
    	location->info = newItem;
    	location->next = topPtr;
    	topPtr = location;
    }
    int stack::pop()
    {
    	NodeType* tempPtr;
    	tempPtr = topPtr;
    	topPtr = topPtr ->next;
    	delete tempPtr;
    }
    bool stack::isempty()
    {
    	return (topPtr == NULL);
    }
    bool stack::isfull()
    {
    	NodeType* location;
    	location = new NodeType;
    	if(location != null)
    	{
    		delete location;
    		return false;
    	}
    	else
    		return true;
    }
    void stack::print()
    {
    while(!isempty())
    cout<<pop()<<endl;
    }
    
    void stack::print_reverse(stack st)
    {
    	if (ptr! =(node*) NULL)
    		print_reverse(ptr->next)
    		print ptr->info;
    }
    
    void stack::reverse(Node* head,){ 
    Node* current = head; 
    stack stk; 
    
    while(current->next() != NULL){ 
    stk.push(current);
    current->next(); 
    } 
    
    head->next = NULL;
    assign null to its next 
    head = current;
    the first node now 
    
    while(current->next() != NULL) { 
    Node* prevNode = stk.pop();
    current->addLink(prevNode);
    prevnode 
    current = current->next();
    } 
    } 
    
    int main()
    {  
    stack x;
    int data;
    ItemType item;
    
    	ifstream instream; 
    	ofstream outstream;
          
    	instream.open("input.txt");
    	outstream.open("output.txt");
    	if(instream.fail()){
        	cout<<"Error opening file\n";
    	exit(1);
    	}
    
    	while ( !instream.eof() )
    	{	
    		if (isfull())
    			break;
    		instream>> data;	
    		item.Initialize(data);
    		 int temp = item.getvalue();
    		push(temp); 
    		print();
    		
    	}
    	instream.close();
    
    	cout<<"--------\n";
    	print_reverse(MAX_ITEMS);
    	cin.get();
    	outstream.close();
        return 0;
    }
    
    here is the ItemType.h
    Code:
    
    /* Information provided by the user */
    const int MAX_ITEMS = 50;
    struct NodeType{
    	ItemType  info;
    	NodeType* next;
    };
    class ItemType {
    public:
    	Stack();
    	bool IsEmpty() const;
    	bool IsFull() const;
    	void Push(ItemType item);
    	void Pop();
    	ItemType Top() const;
    
    private:
    	NodeType* topPtr;
    
    };
    
    
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > because another guy is andivising me to do so

    Um, ok, but why did you change the function? Don't change stuff just because someone tells you to; change it for a good reason and to fix a specific problem. The original version of print_reverse() looked just fine to me. But what didn't it do, that meant you had to change it?

    As for "st undefined", that's probably because you have defined print_reverse() as a global function rather than as a member function of class stack.

    When you call print_reverse, obviously this is going to depend on how you've written it, but you call it with MAX_ITEMS-1, i.e. 9. But if, say, only st[0] .. st[3] are defined, you're going to print junk for the first six elements. If st[0]..st[3] are defined, then surely print_reverse will display st[3], st[2], st[1], st[0], correct? If print_reverse isn't going to recurse, then I'd suggest it shouldn't take an argument.
     
  8. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    i changed the first print_reverse function becasue it gave me errors and i needed to send the output to the output.txt file..
    could u plz forget about the first stack program..i should have started doing it using a linked list but i totally forgot..how could i solve the errors i have?
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It can be daunting if you get loads of errors, particularly if you've just spent ages writing loads of code. It's a good idea just to write a small amount of code and test it, then to add a bit more and test the changes, and so on until you've got the full program.

    The best way to solve a large stack of errors is just to start with the first one. What is it?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice