-
October 15th, 2011, 04:54 PM
#1
Searching and Looping 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():
#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, 05:36 PM
#2
Re: Searching and Looping within Stacks
Please go back and edit your post. Add [code]...[/code]around your code and then add a proper indentation. It hurts the eyes to read unintented code...
-
October 15th, 2011, 05:57 PM
#3
Re: Searching and Looping within Stacks
how do i edit my post? I am new on this site and cant find the editing button
-
October 15th, 2011, 06:08 PM
#4
Re: Searching and Looping within Stacks
I reposted it, just so I dont waste time trying to edit.
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
|