Thread: Stacks & Strings

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    Stacks & Strings

    Ok, I input a string of characters from the keyboard. I need to know how to read that string into a stack. Everytime I do it seems to be reading the whole string in as one character. HOw can I get it to read it one character at a time.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Here is a sample of my code which I know you will probably laugh at but the people who taught me C++ were jackasses and never taught me how to use template classes or anything else I need to know right now.

    int main ()
    {//Opens Main Program

    string name2_reverse;
    cout<<"Please enter a string of characters to have reversed.\n";
    cin>>name2_reverse;
    stack<string,deque<string>> Mystack;

    int i;
    for (i=0; i < 10; i++)
    {
    Mystack.push(string(name2_reverse));
    cout << Mystack.top() <<endl;

    }


    //int a;
    //Mystack.empty()=a;
    //for (a != 0)
    //{
    // cout << Mystack.top() << endl;
    // Mystack.pop();

    // }

    cout<<"Reversed this string = " << name2_reverse <<"\n";
    getch();

    }//Closes Main Program

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Ok, I input a string of characters from the keyboard. I need to know how to read that string into a stack.
    String is a generic term. Stack is a generic term. There are countless implementations of strings and stacks. So until you post more specific descriptions, preferably with code, I can only give you a generic answer:

    When you have read the string, take a for loop that runs from the beginning of the string to the end of the string. In each loop, get the character at this position in the string and put it on the stack.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  5. Strings & Stacks
    By AdioKIP in forum C++ Programming
    Replies: 4
    Last Post: 03-07-2002, 04:51 PM