Searching and Looping within Stacks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Searching and Looping within Stacks

  1. #1
    Join Date
    Oct 2011
    Posts
    8

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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    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...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Oct 2011
    Posts
    8

    Re: Searching and Looping within Stacks

    how do i edit my post? I am new on this site and cant find the editing button

  4. #4
    Join Date
    Oct 2011
    Posts
    8

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center