Hi,
can anyone help me with my code? Im trying to reverse my string using stack... but when it compiles and put in a string and press enter... nothing happens...
Any suggestions?

#include <iostream>
#include<stack>
#include<string>

using namespace std;

int main()
{    
stack <string> s;
string a;

cout << "Enter: " << endl;

while(getline(cin, a) && a != "\n")
{
       s.push(a);
}


while(!s.empty())
{
      cout << s.top();
      s.pop();
}

    cout << endl;
    system("pause");
    return 0;
}

Most of your problems can be fixed by using a stack of characters instead of strings:

#include <iostream>
#include <stack>

using namespace std;

int main()
{    
  stack<char> s;
  char a;

  cout << "Enter: " << endl;

  while ( cin.get ( a ) && a != '\n' )
    s.push(a);

  while ( !s.empty() ) {
    cout << s.top();
    s.pop();
  }

  cout<<'\n';
}

Wow, that was quick! Thanks a lot :)

also can be done recursively..but stack almost equals recursion... for example:

#include <iostream>
#include <string>
using namespace std;

void do_the_stuff() {
     char c; c = getchar();
     if( c == '\n' ) return;
     do_the_stuff();
     putchar( c );
}

int main( void )
{
    cout << "Enter:" << "\n";
    do_the_stuff();   
    return 0;
}

I have another proposition for a recursive solution:

#include <iostream>
#include <string>

std::string GetName(); // String to get user's name.
void reverseString(std::string str);

int main()
{
	std::string name;
	name=GetName();
	std::cout<<"The string is: "<<name<<std::endl;
	std::cout<<"Now the reverseString function is going to reverse the string: "<<std::endl;
	reverseString(name);
	return 0;
}

std::string GetName()
{
	std::string name;
	std::cout << "What is your name?" << std::endl;
	std::cin >> name;
	return name;
} 
//The recursive function
void reverseString(std::string str)
{
	if (str=="")//the base case
		return;
	else //the recursion step
	{
		reverseString(str.substr(1));			
		std::cout<<str.at(0);
	}
}

The recursive function in the recursive step calls itself to display the rest of the string before displaying the first character in the current string argument. This fact causes a delay on displaying the first character in the current string until all characters in the rest of the string are first displayed.

Here is an easier version. It does the same thing.
#include <stack>
#include <string>

using namespace std;

 int main
 {
    string words;//stores separate words
    stack<string> sentence;//stores all the words

    cout << "Please type the word lol at the end of your sentence\n\n";
    cout << "Please Enter a Sentence: ";


    while(words != "lol")
    {
        cin >> words;
        sentence.push(words);
    }

    cout << "\n";
    cout << "The Sentence contains " << sentence.size() << " words" << endl; 
    cout << "\n";

    while(!sentence.empty())
    {
       cout << sentence.top() << " ";
       sentence.pop();
    }

    system("pause>null"); 
    return 0;
}
commented: Wow, that's a THREAD BUMP! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.