Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am a beginner in C++ and I was trying a program in C++ to reverse a string using C++ but its giving all sorts of problems. I don't intend to initialize the string in the code itself.

C
#include <iostream>
#include <string>
#include <stack>
#include <cstring>

using namespace std;

int main()
{
    int i=0;
    string text;
    stack<char> s;
    cout<<"Enter a string: ";
    cin>>text;
    for(int i=0;i<text.length();i++)
    {
    //text.length takes only one word and doesn't count whitespaces.
        s.push(text[i]);
    }
    while(!s.empty())
    {
        cout<<s.top();
        s.pop();
    }


I tried converting this string into text.c_str() and then pushing each of them in the stack but I was erring somewhere in the syntax since I new in this. What would be the correct way to do this?
Posted
Updated 23-Dec-10 2:39am
v2
Comments
Tony Richards 23-Dec-10 8:40am    
Cleaned up the code a little, fixing problems with angle brackets being mistaken for HTML tags.
Dr.Walt Fair, PE 23-Dec-10 10:11am    
I think you asked this twice. Don't do that!

Wouldn't you have to create a stack<char> instance first? I'm not even ankle deep into C++, so I'm taking a guess here: stack<char> s = new stack&lt;char&gt;();
or something like that. Look it up.
Please answer my question also, why do you keep posting questions with no inidication of what error actually occurred. That is the least you could do as it would make it easier for us to help you.

Cheers,


Manfred
 
Share this answer
 
v2
Comments
optimus_prime1 23-Dec-10 9:06am    
I actually wrote the problem in the code comment. Sorry about this :|
Manfred Rudolf Bihy 23-Dec-10 9:17am    
Well, what you wrote in your code is not correct. Read here:
http://msdn.microsoft.com/en-us/library/system.string.length.aspx
I do recommend you buy yourself a book on C++ programming.
optimus_prime1 23-Dec-10 10:38am    
Ya! trying programs from the book but sometimes mess up... :-/
optimus_prime1 23-Dec-10 10:41am    
You mean text.length() is not correct? or text.length() does count length of the string including spaces?
(btw, I am trying Win32 console and not MFC)
Manfred Rudolf Bihy 23-Dec-10 13:03pm    
I meant to say that text.length() counts the length of the whole string.
I'm not sure why you are using a stack to print out the reversed the string. Why not just iterate backwards?

for (int i=text.length()-1; i>=0; i--)
 
Share this answer
 
In your program cin only take one word for example if i type "my name is xyz" than the string only get "my".

getline(cin,text,'\n') 

may solve your problem.
 
Share this answer
 
Comments
optimus_prime1 24-Dec-10 1:15am    
That actually did solve but what I earlier used getline was like this: cin.getline(ch,256);
How should I have known to use getline(cin,text, '\n')? :-/
ShilpiP 24-Dec-10 1:54am    
cin.getline(ch,256); is ok if you are using char pointer and if you are using std::string than it is better you use std::getline.
optimus_prime1 24-Dec-10 2:09am    
So, for char and string getline has different syntax?
For char- cin.getline(ch,256);
For string- getline(cin,text,'\n');
Is it?
Then what's the difference between cin.get(text,'\n'); and cin.getline(ch, 256); ?
ShilpiP 24-Dec-10 4:11am    
cin is istream object

basic_istream Members are get and getline
get: Reads one or more characters from the input stream.
getline: Reads a line from the input stream.

you are using basic_string in your code it has the api with name getline
this api is declared in
#include<string>
using namespace std;
:)

Now please search on net and find out the difference of get and getline.
getline in istream and getline in basic_string. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900