Stacks & Strings

This is a discussion on Stacks & Strings within the C++ Programming forums, part of the General Programming Boards category; Ok, I input a string of characters from the keyboard. I need to know how to read that string into ...

  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,138
    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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07: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, 04:03 PM
  5. Strings & Stacks
    By AdioKIP in forum C++ Programming
    Replies: 4
    Last Post: 03-07-2002, 03:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21