468,490 Members | 2,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

circular link list in c++

69
I'm trying to built and array of circular link list but when I read the input file Ijust get a long link list instead of different list and after trying to use Linked_list_Stack *obj[arraysize]; I'm getting this errors
newcir.cpp:107: request for member `push' in `obj[i]', which is of
non-aggregate type `Linked_list_Stack*'
newcir.cpp:110: request for member `print_list' in `obj[i]', which is of
non-aggregate type `Linked_list_Stack*'
Oct 20 '06 #1
103 15100
arne
315 Expert 100+
I'm trying to built and array of circular link list but when I read the input file Ijust get a long link list instead of different list and after trying to use Linked_list_Stack *obj[arraysize]; I'm getting this errors
newcir.cpp:107: request for member `push' in `obj[i]', which is of
non-aggregate type `Linked_list_Stack*'
newcir.cpp:110: request for member `print_list' in `obj[i]', which is of
non-aggregate type `Linked_list_Stack*'
May it be that you try to access the push member with code like
Expand|Select|Wrap|Line Numbers
  1.   obj[i].push();
  2.  
?

If so: the array contains only pointers to objects, not the objects themselves. So, you have to use
Expand|Select|Wrap|Line Numbers
  1.   (*obj[i]).push();
  2.  
or
Expand|Select|Wrap|Line Numbers
  1.   obj[i]->push();
  2.  
Oct 20 '06 #2
saraSS
69
fix that problem thatnks but still getting only one list I need different list my main is
int main( )
{
int i;
cin>>arraysize;
Linked_list_Stack *obj[arraysize];

for(i=0;i<arraysize;i++)
{
obj[i] = new Linked_list_Stack;
while(!cin.eof())
{
cin>>num;
if(num!=-1)
{
obj[i]->push( );
}
}
obj[i]->print_list( );
cout<<endl;
}
return 0;
Oct 20 '06 #3
arne
315 Expert 100+
Could you please explain a little bit more in detail what you're trying to do?
As far as I understand from your code, you're

- building an array of pointers to elements of type Linked_list_Stack

- allocating objects for each of the pointers in the array

- invoking the member function push() of these objects (what is pushed here btw?)

- and finally you invoke the member function print_list (what will/should be printed here?)

Could you explain what should happen if you're program worked correctly?
Oct 20 '06 #4
saraSS
69
I im trying to implement a stack-based postfix calculator for sets of nonnegative
integers. The input consists of the following items:
a. n, the number of input sets, on a line by itself.
b. n ordered sets of non-negative integers, each starting on a new line and each terminated by -1. Each set is to be stored as an ordered, singly-linked,circular list with a header.
c. A string containing a postfix expression, on a line by itself, without any spaces. The following
may appear:
U set union (destructive, i.e. the result set will be created using some elements of the operand
sets. Extra elements must be recycled.)
I set intersection (destructive)
- set difference (destructive)
\ symmetric difference (destructive)
= equality test (destructive)
< tests whether the first operand set is properly contained in the second operand set (destructive)
& Boolean and (implementation of TRUE/FALSE is your decision)
| Boolean or
! Boolean not
x where x is a lower-case symbol in the range a . . . z to indicate that a copy should be made
of an input set. This copy will be an ordered singly-linked circular list with a header. A
pointer to the header of the cloned list is to be pushed to the stack. a indicates that the first
input set should be copied, b indicates that the second input set should be copied, and so on.
The input will be provided to your program through standard input using a shell redirect.
2. The output is the one item that remains on the stack after processing the entire postfix expression. It
may be a set (e.g. a linked list) or a Boolean value. If it is a set, then print the values separated by
spaces. If it is a Boolean value, then print TRUE or FALSE.

one example input file is
6
1 3 5 7 9 11 13 15 17 19 -1
2 4 6 8 10 12 14 16 18 20 -1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
0 3 6 9 12 15 18 -1
1 4 7 10 13 16 19 -1
2 5 8 11 14 17 20 -1
abcdefUUUUIabIacIadIaeIafIUUUU=

what I have done is just the link list my program

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7. int arraysize,num;
  8.  
  9. class Linked_list_Stack
  10. {
  11. private:
  12. struct node
  13. {
  14. int data;
  15. node *next;
  16. };
  17.  
  18. node *top;
  19. node *entry;
  20. node *print;
  21. node *bottom;
  22. node *last_entry;
  23. node *second_last_entry;
  24.  
  25. public:
  26. Linked_list_Stack( );
  27. void pop( );
  28. void push( );
  29. void print_list( );
  30. void show_working( );
  31. };
  32.  
  33. Linked_list_Stack::Linked_list_Stack ( )
  34. {
  35. top=NULL;
  36. bottom=NULL;
  37. }
  38.  
  39. void Linked_list_Stack::push( )
  40. {
  41.  
  42. entry=new node;
  43. if(bottom==NULL)
  44. entry->data=num;
  45. entry->next=NULL;
  46. bottom=entry;
  47. top=entry;
  48. }
  49. else
  50. {
  51. entry->data=num;
  52. entry->next=NULL;
  53. top->next=entry;
  54. top=entry;
  55. }
  56. cout<<"\n\n\t *** "<<num<<" is pushed onto the Stack."<<endl;
  57. }
  58.  
  59. void Linked_list_Stack::pop( )
  60. {
  61. if(bottom==NULL)
  62. cout<<"\n\n\n\t *** Error : Stack is empty. \n"<<endl;
  63. else
  64. {
  65. for(last_entry=bottom;last_entry->next!=NULL;
  66. last_entry=last_entry->next)
  67. second_last_entry=last_entry;
  68. if(top==bottom)
  69. bottom=NULL;
  70.  
  71. int poped_element=top->data;
  72. delete top;
  73. top=second_last_entry;
  74. top->next=NULL;
  75. cout<<"\n\n\n\t *** "<<poped_element<<" is poped from the Stack."<<endl;
  76. }
  77. }
  78.  
  79. void Linked_list_Stack::print_list( )
  80. {
  81. print=bottom;
  82.  
  83. if(print!=NULL)
  84. cout<<"\n\n\n\n\n\t Values pushed onto Stack are : \n"<<endl;
  85.  
  86. while(print!=NULL)
  87. {
  88. cout<<print->data;
  89. print=print->next;
  90. }
  91. }
  92.  
  93. int main( )
  94. {
  95. int i;
  96. cin>>arraysize;
  97. Linked_list_Stack *obj[arraysize];
  98.  
  99. for(i=0;i<arraysize;i++)
  100. {
  101. obj[i] = new Linked_list_Stack;
  102. while(!cin.eof()) 
  103. cin>>num;
  104. if(num!=-1)
  105. {
  106. obj[i]->push( );
  107. }
  108. }
  109. obj[i]->print_list( );
  110. cout<<endl;
  111. return 0;
  112. }
  113.  
thanks for helping me
Oct 20 '06 #5
saraSS
69
did I scare you off I hope not I need some help mostly with the lists I have some code ready for the operations between the list mean the union and all that
thanks again
Oct 20 '06 #6
arne
315 Expert 100+
did I scare you off I hope not I need some help mostly with the lists I have some code ready for the operations between the list mean the union and all that
thanks again
No, you didn't. It was just night time where I live, so I had to get some sleep :-)

Do you need to build the lists/stacks on your own (for instance, since it is part of the assignment)? If not, you may consider to use the "list" and "stack" containers from the C++ standard template library. This would save you a lot of trouble, I guess.
Oct 21 '06 #7
saraSS
69
ok that sound good but how do I do that ?
Oct 21 '06 #8
arne
315 Expert 100+
ok that sound good but how do I do that ?
:)
Start off with getting a good introductory book / tutorial on C++: the template containers are explained in each of these. It's really worth the effort!

For a quick start, you can also have a look at
http://www.cppreference.com
They explain the usage of containers with easy examples and list all member functions to manipulate them.
Oct 21 '06 #9
saraSS
69
in the site you gave me talk a lot about vector can I use the same functions for my list and I'll need to change all my program for some of those functions I dont know I'm confuse
Oct 21 '06 #10
saraSS
69
for example I have the class

Expand|Select|Wrap|Line Numbers
  1.  
  2. class list
  3. {
  4. public:
  5. node* begin() const;
  6. node* end() const;
  7. int size() const;
  8. bool empty() const;
  9. void insert(node* p, const data_type& d);
  10. void erase(node*& p);
  11. list();
  12. list (const list& l);
  13. ~list();
  14. list& operator= (const list& l);
  15. private:
  16. void free_list();
  17. node* head;
  18. #define tail NULL
  19. };
  20.  
  21.  
then I need to implement some code from the site you gave me for every function I have in this class? and what about the ones that dont have an implementation they dont need implementation because I'm adding #include <list> ?
see I'm really confuse
lol
Oct 21 '06 #11
arne
315 Expert 100+
in the site you gave me talk a lot about vector can I use the same functions for my list and I'll need to change all my program for some of those functions I dont know I'm confuse
The question was if you _have_ to implement the containers (i.e. data structures such as list or stack) on your own or if you may want to use the built-in classes.

It is nothing wrong with implementing and using one's own data structures, as long as you have a good reason to do so.

Using the STL containers was just a proposal that may save you some time now (since you don't need to worry about how to implement and debug your data structures) and later (as you know about STL containers next time you have to do some coding).

I am sorry if I confused you :)
Oct 21 '06 #12
saraSS
69
I want to save some time and learn to use the stl but getting confuse here
Oct 21 '06 #13
arne
315 Expert 100+
I want to save some time and learn to use the stl but getting confuse here
Upps, sorry didn't see your last post ... that's why I replied to an old posting by you ...

You don't have to implement the member functions of the STL list, it's already done. The STL list contains all functions you typically need for a list, such as inserting elements, finding elements, removing elements and the like. The same holds true for the stack container.
Look at the "Constructors" to see how objects of these classes can be created.

In addition, there are some (generic) algorithms, such as copy, sort, rotate and many more that support operations on STL containers. See the "Algorithms" section on the site I mentioned.
Oct 21 '06 #14
saraSS
69
then if the fuction are in the stl I dont need to include then in my class? if I include then means I need to implement them?

do I need just to work in main and call for all those functions when I need them?
Oct 21 '06 #15
arne
315 Expert 100+
then if the fuction are in the stl I dont need to include then in my class? if I include then means I need to implement them?

do I need just to work in main and call for all those functions when I need them?
Yes, you just include the header. I attach a simple example that uses the STL list container and an STL algorithm:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main( void )
  7. {
  8.     // define a list that holds ints
  9.     list<int> i_list;
  10.  
  11.     // add new elements at the end of the list (list member function)
  12.     i_list.push_back( 1 );
  13.     i_list.push_back( 2 );
  14.     i_list.push_back( 3 );
  15.     i_list.push_back( 4 );
  16.  
  17.     // traverse the list and print out the elements
  18.     list<int>::iterator iter;
  19.     for( iter=i_list.begin(); iter!=i_list.end(); iter++ ) {
  20.  
  21.         cout << *iter << endl;
  22.     }
  23.  
  24.     // reverse the list (generic STL algorithm)
  25.     reverse( i_list.begin(), i_list.end() );
  26.  
  27.     // traverse the list and print out the elements (again)
  28.     for( iter=i_list.begin(); iter!=i_list.end(); iter++ ) {
  29.  
  30.         cout << *iter << endl;
  31.     }
  32.  
  33.     return 0;
  34. }
  35.  
  36.  
Oct 21 '06 #16
saraSS
69
then yes this way save a lot time .
I trying to make various containers using an array like

list<int> i_list[arraysize];

and use a for loop like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(i=0;i<arraysize;i++)
  3. {
  4. while(!cin.eof())
  5. {
  6. cin>>num;
  7. if(num!=-1)
  8. {
  9. i_list[i].push_back( num );
  10. }
  11. else
  12. return 0;
  13. }
  14.  
  15. and printing out like this
  16.  
  17. for(i=0;i<arraysize;i++)
  18. {
  19. // traverse the list and print out the elements
  20. list<int>::iterator iter;
  21. for( iter=i_list[i].begin(); iter!=i_list[i].end(); iter++ )
  22. {
  23. cout << *iter << endl;
  24. }
  25.  
I'm not getting errors just blank
Oct 21 '06 #17
saraSS
69
opps was not readding the size of the array now I got
-bash-2.05b$ a.out < hw2file1.txt
279-1-bash-2.05b$
how can I rewrite the while loop so it stops every -1 and keep going with the array of containers ?
Oct 21 '06 #18
saraSS
69
I got it working the list
I guess you when home? ok thanks

do you think this is gonna work as circular list or I need an extra funtion just to make every list a circular lis you know making the end point to the head ?
Oct 21 '06 #19
arne
315 Expert 100+
I got it working the list
I guess you when home? ok thanks

do you think this is gonna work as circular list or I need an extra funtion just to make every list a circular lis you know making the end point to the head ?
Good!
Do you really need a circular list?
Oct 21 '06 #20
saraSS
69
the assignment say yes and I need to name every list with and alphabet letter so I can do the operatinons later
Oct 21 '06 #21
saraSS
69
Each set is to be stored as an ordered, singly-linked,
circular list with a header.
Oct 21 '06 #22
arne
315 Expert 100+
Each set is to be stored as an ordered, singly-linked,
circular list with a header.
Is that a hint or a demand? Sounds a little like a demand ...

STL lists are neither singly-linked nor circular. And AFAIK the STL does not provide such lists, since they are not that common, I would guess.

That's why I said in the beginning that here is nothing wrong with implementing own containers, if you have a good reason for it. Non-existence is a good reason :-)

One could certainly mimic a circular list using the features of the STL list container ...
Oct 21 '06 #23
saraSS
69
so how can I mimic the circular list I work good with exemples lol
Oct 21 '06 #24
saraSS
69
remember my initial code I rewrite the whiel loop and is working so i'm gonna try to keep working with that one and using a few of the <list> funtions see if help me a lit
Oct 21 '06 #25
arne
315 Expert 100+
so how can I mimic the circular list I work good with exemples lol
Hmm, ok. So what distinguishes a serial from circular list?

The successor of the last element is the first element.
Since you want a singly linked list, you don't have to care about the predecessor of the first element, there is only one direction.

In principal, you can either write a wrapper class which simply contains a std::list or you could derive a class from std::and "extend" its functionality.
A wrapper class may have the advantage that you could also "provide" (reduce) the functionality of single-linkedness ... :-)
Oct 21 '06 #26
saraSS
69
I but with the initial program I cant mix and match looks like I mean the funtion that I have and the functions from the library ;(
Oct 21 '06 #27
arne
315 Expert 100+
I but with the initial program I cant mix and match looks like I mean the funtion that I have and the functions from the library ;(
It's a tradeoff. You have the choice between
- rewriting your program and writing a wrapper/derived class, and
- making your own circular list container work.

However:
Looking through your former posts, I would recommend that you clarify for yourself the requirements and decide _how_ you want to organize your data. (In the very beginning you talked about a stack, but currently we're at an array of singly-linked circular lists.)
This is the very first (design!) decision you have to take. After that you should think about which data structures are appropriate to implement your design. Otherwise we'll go in circles ...
Oct 21 '06 #28
saraSS
69
is not my decision actually is what the assigment says it says to implement a stack based postfix calculator for sets of integers and those sets need to be store as an ordered, singly-linked,circular list with a header, and I need to use some recycling list for the calculator.
but I though the array was gonna make it easy I guess i need to change the array for a stack of circular list? I dont know lol
Oct 21 '06 #29
arne
315 Expert 100+
is not my decision actually is what the assigment says it says to implement a stack based postfix calculator for sets of integers and those sets need to be store as an ordered, singly-linked,circular list with a header, and I need to use some recycling list for the calculator.
but I though the array was gonna make it easy I guess i need to change the array for a stack of circular list? I dont know lol
No, you need both, an array and a stack:

I took the time to read through your assignment. I think what you need are the following "ingredients"

- a node_struct (list element with value and successor pointer)
- a header_struct which points to a first node
- an array/vector of header structs, where you will store and keep the input
- a stack (maybe std::stack<header_struct *>?), where you store the links to the cloned lists
- stack operations (push, pop)
- list operations (clone and all the set mathematics stuff as required by the assignment)

The array/vector is used as a reference when you have to put the same set onto the stack again. The stack itself is what you use for the math. That's why you need both, I would say.
Oct 21 '06 #30
saraSS
69
sound like a plan I'm gonna work on that and let you know thanks
Oct 21 '06 #31
arne
315 Expert 100+
sound like a plan I'm gonna work on that and let you know thanks
Yes, please do so. Sounds like quite an interesting assignment :)
Oct 21 '06 #32
saraSS
69
ok so I need to work the postfix evaluation with a stack and I'm ok I guess
how can I set a string to nul in c++?
I've seen something like this but does not work with c++

strset(postfix_expression[count_2] = NULL);
Oct 21 '06 #33
arne
315 Expert 100+
ok so I need to work the postfix evaluation with a stack and I'm ok I guess
how can I set a string to nul in c++?
I've seen something like this but does not work with c++

strset(postfix_expression[count_2] = NULL);
Has postfix_expression[count_2] type std::string? Then use member functions
clear() to remove all elements from the string, or
erase() to remove only specific elements.
Oct 22 '06 #34
saraSS
69
I want to initialize Postfix_expression[100] to null but nothing work not even the clear() function
Oct 22 '06 #35
saraSS
69
and my get line function is not working either lol
give me like and infinite look just stuck there is
cin.getline(Postfix_expression,80); why why? lol
Oct 22 '06 #36
arne
315 Expert 100+
I want to initialize Postfix_expression[100] to null but nothing work not even the clear() function
Clear() only applies to std::strings, of course, such as in

Expand|Select|Wrap|Line Numbers
  1. std::string a_string;
  2. a_string.clear();
  3.  
That's why I asked of which type Postfix_expression is ...
Oct 22 '06 #37
saraSS
69
I'm using array of char for the postfix exprexion thinking that is gonna be easy to pop and pust every letter and do the operations but is getting complicated I got a lot confuse with the strings and that how can I do it with the char Postfix_expression[100] and the get line
Oct 22 '06 #38
arne
315 Expert 100+
and my get line function is not working either lol
give me like and infinite look just stuck there is
cin.getline(Postfix_expression,80); why why? lol
Try something like

Expand|Select|Wrap|Line Numbers
  1. int main( void )
  2. {
  3.         std::cout << "Please enter a phrase: ";
  4.  
  5.         string txt;
  6.  
  7.         getline( cin, txt );
  8.  
  9.         std::cout << txt << std::endl;
  10.  
  11.         return 0;
  12. }
  13.  
and copy the relevant parts into your code (if this works) ...
Oct 22 '06 #39
arne
315 Expert 100+
I'm using array of char for the postfix exprexion thinking that is gonna be easy to pop and pust every letter and do the operations but is getting complicated I got a lot confuse with the strings and that how can I do it with the char Postfix_expression[100] and the get line
You mean for the UUUUUIab... stuff? You can do most (all?) things with std::string that you can do with char[]. You can even convert the read-in string to an char[] by calling std::string::c_str().

But char[] works, too:
Expand|Select|Wrap|Line Numbers
  1. int main( void )
  2. {
  3.     char txt[100];
  4.  
  5.         std::cout << "Please enter a phrase: ";
  6.     cin.getline( txt, 100 );
  7.  
  8.     cout << txt << endl;
  9.  
  10.     return 0;
  11. }
  12.  
Oct 22 '06 #40
saraSS
69
still not geting anything the cursor just stay there this is what I'm doing in main
int main( )
{
int m;

Initialize ( ) ;
//cout<<"ok";
cin.getline (postfix,80) ;
cout<<"ok";
SetExpression (postfix) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
return 0;
}
Oct 22 '06 #41
arne
315 Expert 100+
still not geting anything the cursor just stay there this is what I'm doing in main
int main( )
{
int m;

Initialize ( ) ;
//cout<<"ok";
cin.getline (postfix,80) ;
cout<<"ok";
SetExpression (postfix) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
return 0;
}
But that you see nothing is no related to getline: comment out all other function call (except for the getline, puts and printf functions) and you will see that you read in the data. I guess you have an infinite loop somewhere else :)

You may also add an "endl" io manipulator like
Expand|Select|Wrap|Line Numbers
  1. cout << "ok" << endl;
  2.  
to see where the problem is. Without "endl" the ouput buffer is not flushed. and you cannot see where the program currently is.
Oct 22 '06 #42
saraSS
69
done that and i print out the ok before that line but not the one after so i think is the get line the mess up
Oct 22 '06 #43
arne
315 Expert 100+
done that and i print out the ok before that line but not the one after so i think is the get line the mess up
Try this code (it's _your_ code reduced to the essential parts with only the postfix declaration added):

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace::std;
  3.  
  4. int main( )
  5. {
  6.     int m;
  7.     char postfix[80];     // added this line 
  8.  
  9. //    Initialize ( ) ;
  10.     cout<<"ok" << endl;
  11.     cin.getline (postfix,80) ;
  12.     cout<<"ok" << endl;
  13. //    SetExpression (postfix) ;
  14.     puts(postfix);
  15. //    strcpy(eval,postfix);
  16. //    m=Evaluate( );
  17.     printf("answer: %d", m );
  18.     return 0;
  19. }
  20.  
This runs fine on my machine: It prints "ok" 2 times, the input string and the value of m. No endless loop here.
Oct 22 '06 #44
saraSS
69
I had char postfix[80]; as a gloval variable but even like the way you told me
still cant get my line ?

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. using namespace std;
  3. #define MAX 80
  4.  
  5. char stack[MAX] ;
  6. char eval[MAX] ;
  7. char *s, *t ; /*pointers to input and output strings*/
  8. int top; /*Stack top*/
  9.  
  10. /*Function Prototypes*/
  11. void Initialize (void);
  12. void SetExpression (char *);
  13. char Pop (void );
  14. void Push (char);
  15. int Evaluate(void);
  16.  
  17. int main( )
  18. {
  19. int m;
  20. char postfix[80]; 
  21.  
  22. Initialize ( ) ;
  23. cout<<"ok";
  24. cin.getline (postfix,80) ;
  25. cout<<"ok";
  26. SetExpression (postfix) ;
  27. puts(postfix);
  28. strcpy(eval,postfix);
  29. m=Evaluate( );
  30. printf("answer: %d", m );
  31. return 0;
  32. }
  33.  
  34. void Initialize (void)
  35. {
  36. top = -1 ;/*Make stack empty*/
  37. }
  38.  
  39. void SetExpression ( char *str )
  40. {
  41. s = str ;
  42. }
  43.  
  44. /* adds operator to the stack */
  45. void Push ( char c )
  46. {
  47. if ( top == MAX - 1 )
  48. printf ( "\nStack is full.\n" ) ;
  49. else
  50. {
  51. top++ ;
  52. stack[top] = c ;
  53. }
  54. }
  55.  
  56. /* pops an operator from the stack */
  57. char Pop ( void )
  58. {
  59. if ( top == -1 ) /* Stack is empty*/
  60. return -1 ;
  61. else
  62. {
  63. char item = stack[top] ;
  64. top-- ;
  65. return item ;
  66. }
  67. }
  68.  
  69. int Evaluate(void)
  70. {
  71. int i,l,a,b,q,z;
  72. l=strlen(eval);
  73. for(i=0;i<l;i++)
  74. {
  75. if ( isdigit ( eval[i] ) )
  76. {
  77. Push(eval[i]);
  78. }
  79. else if ( eval[i] == 'U' || eval[i] == 'I' || eval[i] == '-' || eval[i] == '\' || eval[i] == '=' 
  80. || eval[i] == '<' || eval[i] == '&' || eval[i] == '|' || eval[i] == '&' )
  81. {
  82. a = Pop ( );
  83. b = Pop ( );
  84.  
  85. switch( eval[i] )
  86. {
  87. case 'U' : break;
  88. case 'I' : break;
  89. case '-' : break;
  90. case '\' : break;
  91. case '<' : break;
  92. case '&' : break;
  93. case '|' : break;
  94. case '!' : break;
  95. }
  96. Push ( q );
  97. }
  98. }
  99. z = Pop ( );
  100. return z;
  101. }
  102.  
what I'm doing wrong?
Oct 22 '06 #45
arne
315 Expert 100+
See, I just commented out all insignificant function calls. This _definitively_ works and reads in a line from stdin. Please copy and try this code (without all the other functions you have written) in a new program, so that we have a basis on which we can proceed. BTW, the code you posted does not compile. May it be that you change your code, compile, ignore the errors and execute an old executable?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 80
  4.  
  5. char stack[MAX] ;
  6. char eval[MAX] ;
  7. char *s, *t ; /*pointers to input and output strings*/
  8. int top; /*Stack top*/
  9.  
  10. /*Function Prototypes*/
  11. void Initialize (void);
  12. void SetExpression (char *);
  13. char Pop (void );
  14. void Push (char);
  15. int Evaluate(void);
  16.  
  17. int main( )
  18. {
  19.     int m;
  20.     char postfix[80];
  21.  
  22. //    Initialize ( ) ;
  23.     cout<<"ok";
  24.     cin.getline (postfix,80) ;
  25.     cout<<"ok";
  26. //    SetExpression (postfix) ;
  27.     puts(postfix);
  28.     strcpy(eval,postfix);
  29. //    m=Evaluate( );
  30.     printf("answer: %d", m );
  31.     return 0;
  32. }
  33.  
Oct 22 '06 #46
saraSS
69
yes it does not compile Im trying to get the line from this file so i'm doing the compilation and getting this
-bash-2.05b$ g++ hi.cpp
-bash-2.05b$ a.out hw2file2.txt
ok
only one ok the imput file is abcdefUUUUIabIacIadIaeIafIUUUU=
and still nothing I dont know I am running the new version of the program
Oct 22 '06 #47
arne
315 Expert 100+
yes it does not compile Im trying to get the line from this file so i'm doing the compilation and getting this
-bash-2.05b$ g++ hi.cpp
-bash-2.05b$ a.out hw2file2.txt
ok
only one ok the imput file is abcdefUUUUIabIacIadIaeIafIUUUU=
and still nothing I dont know I am running the new version of the program
Ah, you tried to get the data from a file, not typing it in by hand? Then it's clear why you get no line :)

Try

-bash-2.05b$ a.out < hw2file2.txt

Note the '<' ...
Oct 22 '06 #48
saraSS
69
why is it different for this file with all my file I usually use only a.out hw2file2.txt?
Oct 22 '06 #49
arne
315 Expert 100+
why is it different for this file with all my file I usually use only a.out hw2file2.txt?
Because you try to get the data via cin! The file content is redirected to your program by '<'. If you use
a.out hw2file2.txt
you pass only the file name as a parameter. You still have to open the file then. Different approach.

BTW, you can also use '>' to redirect the output of your program to another file.

Did you get the data now from the file?
Oct 22 '06 #50

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by barias | last post: by
3 posts views Thread by Giampaolo Rodola' | last post: by
2 posts views Thread by morris11 | last post: by
5 posts views Thread by =?Utf-8?B?Qm9i?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.