hi everyone..
i need to ask 2 questions..
first..is the below code done using recursion or not?
second..how can i make the below code take input from a text file instead of generating random numbers and make the output go to a new text file?

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>
#include<ctime>
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=0;     
               	while(i!=MAX)
               	cout<<st[i++]<<endl;     
               }
};
int main(int argc, char *argv[])
{  
srand(time(0));    
stack x;
while(!x.isfull()) 
x.push(rand()%1000+1);
x.print();
cout<<"--------\n";
x.print_reverse();
cin.get();
    return 0;
}

>>is the below code done using recursion or not?
No. Recursion requires a function to call itself, and I see no indication of that behavior in the code you posted.

>>how can i make the below code take input from a text fiel
Open the text file, then change the code on line 37 to read a line from the file then send it to the push() function instead of the rand() value.

hi ancient dragon..
mmmm i figured out how to make it read input from a text file..put how can i make this code a recursive function?

I suppose you could make print_reverse() recursive, something like this: (Note: not compiled or tested)

void print_reverse(int i)
{
    if( i < MAX)
        print_reverse(++i);
   cout << st[i] << "\n";
}

mmmm
i was trying to do it put i have 5 errors..can someone help?

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>
#include <fstream>
using std::ifstream;
using std::ofstream;
#include<ctime>
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(int argc, char *argv[])
{  
stack x;
int data;

	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;
}

ok i decreased the errors to 2 put can't solve them.
they are:
1- error C2664: 'stack::push' : cannot convert parameter 1 from 'ItemType' to 'int'
2- error C2660: 'stack::print_reverse' : function does not take 0 arguments
1>unsorted list - 2 error(s), 0 warning(s)

the new code is below:(how can i solve these problems?)

#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;
}

what's wrong with
x.push(item); and x.print_reverse(); ????

what's wrong with
x.push(item); and x.print_reverse(); ????

1. Item is ItemType, not int..Get int from ItemType first then Push..
2. x.print_reverse(int) rite!. The arg must int..

1. Item is ItemType, not int..Get int from ItemType first then Push..
2. x.print_reverse(int) rite!. The arg must int..

i really dont get it..the ItemType is a class in the header..what should i change?

Post ItemType.h

here u go:

/* Information provided by the user */
const int MAX_ITEMS = 50;
enum RelationType {LESS, GREATER, EQUAL};

class ItemType {
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print() const;
void Initialize(int number);
int ItemType::getval()
{return value;}
private:
int value;

};

ItemType::ItemType()
{
value=0;
}


RelationType ItemType::ComparedTo(ItemType
otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print() const
{
cout<<value<<" ";
}
int main()
{  
stack x;
int data;
ItemType item;

	ifstream instream; 
	ofstream outstream;

while(!x.isfull()) 
{

	while ( !instream.eof() )
	{	
		instream>> data;	
		item.Initialize(data);
   // here
   int temp = item.getvalue();
		x.push(temp); 
		x.print();

	}
	instream.close();
}
cout<<"--------\n";
// here
x.print_reverse(5/*maybe 5*/);
cin.get();
    return 0;
}

mmmm it is a disaster..
the program is not running as it is supposed to run..
the program must read the items in the input.txt and put them in the stack and then reverse the items and the result is put in another output.txt..but this is giving me a very strange output

mmmmmm..i changed some things here as u said and did other changes..the program runs but the problem now is that the input items are printed but not the reversed stack..how can i change this?
here is the new 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;
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.getval();
		x.push(temp); 
		x.print();

	}
	instream.close();
}
int temp;
cout<<"--------\n";
x.print_reverse(5);
cin.get();
outstream.close();
    return 0;
}
void print_reverse(int i)
{
   if(i<MAX)
   {
      for(int j=i;j>=0;j--)
         cout<<st[j];
   }
}

still fails to run properly..it doesnt show the reversed items in the stack..and there is an error in the ouput..the output.txt is emply after i run it..
there is also another error in the code that i put a comment near it
here is the new code..plz help

#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();

	}
	instream.close();

cout<<"--------\n";
x.print_reverse(data);
outstream<<x.print_reverse(data).getvalue();//when compiled the program gives me that there is an error here
cin.get();
}
outstream.close();
    return 0;
}

Sure..

void print_reverse(int i)
{
   if(i<MAX)
   {
      ofstream out;
      out.open("output.txt");
      for(int j=i;j>=0;j--)
      {
         cout<<st[j];
         out<<st[j];
      }
      out.close();
   }
}

it's still not working..r u compiling what ur giving me?
it only prints me the input items from the text file and then puts the dashed lines and then no reversed items in the stack are appearing..it's taking input from the user and giving me these dashed lines again and again

Have you delete the outstream from main function..

yes..but still having the same problem..

In the main func

// try
   x.print_reverse(9);
   // not data

i did try that too..but it gives me these wierd numbers..and besides, i dont want to specify a number..it should be a varaible that has the number of the items in the input.txt
i think cin.get(); is causing a problem

In the main func

while (!x.isfull())

carrying in

while(!instream.eof())
{
   if (x.isfull())
      break;
   // ...
}

not really working yet..
it gives the same results..
u r not running the program?it will really help if ur trying to run and compile it urself

Show me the input n output..

this is the input: (from input.txt)
660 684 514 737 197 794 722 453 279 787
but the output in the output.txt doesnt appear..it's still empty
the problem is with the reversed function..i really tried to solve it but didnt succeed..thanks alot for ur help
plz help me run it as it should run

void print_reverse(int i)
{
   //if(i<MAX)
   //{
      ofstream out;
      out.open("output.txt");
      // not recommended
      for(int j=9;j>=0;j--)
      {
         cout<<st[j];
         out<<st[j];
      }
      out.close();
   //}
}

it's not the output i was hoping for..
this is the output it gave me
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460787
and by the way.. i need this print_reverse function to stay a recursive function

void print_reverse(int i)
{
   if(i<MAX && i >= 0)
   {
         cout<<st[i]<<endl;

         print_reverse(--i);
   }
}

for saving, declare ofstream outstream as a class member..

// main
print_reverse(MAX-1);
// ...

not working yet..

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.