-
October 15th, 2011, 06:06 PM
#1
REPOST :: Search and Loop Within Stacks
I am having issues with my loop. Basically, I am trying to
- pop an item off the original stack, storing it in a temporary gumball
-then look at the color field of that temporary gumball
-if it is what you want, you have a match
-if not, push the gumball onto the temporary stack
-repeat this process until you find what you want or the original stack is empty
-put all the items from the temp stack back onto the original stack in its original order
I AM HAVING PROBLEMS WITH MY SYNTAX, AND WHICH LOOPS TO USE TO GET THIS TO WORK THE WAY IT IS INTENDED TO. Any Suggestions???
The loop I am having problems with right now is under my "case e" for Eat:
int main():
Code:
#include <iostream>
#include "Stack.h"
#include "Gumball.h"
using namespace std;
int main()
{
Stack s, gumballStack;
Gumball g, temp;
char choice;
bool choice_flag = true;
do {
cin >> choice;
cin >> g.color;
switch(choice)
{
case 'b':
case 'B':
cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
g.counter = 0;
s.isempty();
s.push(g);
if(!s.isfull())
cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
else
cout << "There is no room for another gumball." << endl << endl;
break;
case 'e':
case 'E':
s.isempty();
do(s.pop()) //ERROR!
{
s.pop() = temp;
}
while(!s.isempty() && g.color != temp.color)
{
temp.counter++;
gumballStack.push(temp);
s.pop();
cout << "A gumball has been eaten." << endl << endl;
//cout << " " << g.counter << endl;
}
if(!s.isempty())
{
//cout << " " << g.counter++ << endl;
s.pop();
cout << "A gumball has been eaten." << endl << endl;
// cout << "A" << " " << g.color << " was not found." << endl << endl;
}
else
{
cout << "A" << " " << g.color << " was not found." << endl << endl;
}
while(!gumballStack.isempty())
{
//gumballStack.pop();
s.push(gumballStack.pop());
gumballStack.pop();
}
break;
case 'q':
case 'Q':
choice_flag = false;
break;
}
} while(choice_flag);
return 0;
}
-
October 15th, 2011, 06:11 PM
#2
Re: REPOST :: Search and Loop Within Stacks
Your code doesn't appear to be properly indented. That's the point of code tags---to preserve indentation.
What is the intention here? I do not think you are doing what you think you are doing.
Code:
s.isempty();
do(s.pop()) //ERROR!
{
s.pop() = temp;
}
-
October 15th, 2011, 06:47 PM
#3
Re: REPOST :: Search and Loop Within Stacks
Sincere apologies about that, I didn't check to make sure it copied with the correct indentation. In that section, I intend to pop off the top element of Stack s , saving it into a temp Gumball.
-
October 15th, 2011, 06:51 PM
#4
Re: REPOST :: Search and Loop Within Stacks
Well, what you are actually doing is using incorrect loop syntax, popping two elements off the stack, and attempting to overwrite one of the popped elements with the contents of "temp", whatever that is.
And by the way, it's usually not a good idea for a stack class to return a value on pop(). It is better to provide a separate top() accessor, and make pop() return void, because this design is more flexible and more efficient. (There's a reason the STL stack adapter is designed that way.)
-
October 15th, 2011, 07:11 PM
#5
Re: REPOST :: Search and Loop Within Stacks
The contents of temp are simply defaults from the constructor. Oh ok, I see how I'm popping off two elements now but how else can I get the contents of the popped Gumball into the temp Gumball? Which loop is most efficient?
-
October 15th, 2011, 07:16 PM
#6
Re: REPOST :: Search and Loop Within Stacks
Originally Posted by planetlver
how else can I get the contents of the popped Gumball into the temp Gumball?
A simple assignment will suffice (presuming the Gumball type is safely copyable, of course). You just aren't doing it right at the moment. Or more to the point, you aren't doing it left.
Which loop is most efficient?
There is no difference in efficiency between loop types, merely convenience of coding. However, you appear to be confusing do-while and while syntax at the moment, and ending up with something that isn't valid for either.
-
October 15th, 2011, 07:24 PM
#7
Re: REPOST :: Search and Loop Within Stacks
Simply removing the intended do-while loop{} and saying s.pop() = temp should pop it off Stack s and copy the info (string color and int count) into the temp Gumball then.
-
October 15th, 2011, 07:34 PM
#8
Re: REPOST :: Search and Loop Within Stacks
Assignment works right-to-left. I tried to hint at that but apparently you didn't get it.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|