CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2011
    Posts
    8

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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

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

  3. #3
    Join Date
    Oct 2011
    Posts
    8

    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.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.)

  5. #5
    Join Date
    Oct 2011
    Posts
    8

    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?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: REPOST :: Search and Loop Within Stacks

    Quote Originally Posted by planetlver View Post
    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.

  7. #7
    Join Date
    Oct 2011
    Posts
    8

    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.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured