Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,111 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,734 people online right now. Registration is fast and FREE... Join Now!




Strings

 
Reply to this topicStart new topic

Strings

k0b13r
12 Apr, 2007 - 08:55 AM
Post #1

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 196



Thanked: 1 times
Dream Kudos: 250
My Contributions
Hi ! I need a little help with strings. In my program I write a text, and then 'for' loop move at every single char. But if there is a space in text, program crashes at empty char.... how can I prevent it?
My code, nothing much for now:
CODE

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

int main()
{
    int letter;
    string before, after;
    cin >> before;
    for(unsigned int i = 0; i < before.length(); i++)
    {
        letter = (int)before[i]+13;
        cout << (char)letter;
    }
    cout << endl;
}


And, yes, this will MAYBE be ROT13 algorithm implementation ;]
Thx for fast answers
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
12 Apr, 2007 - 08:57 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Are you sure it's crashing at a space? the >> operator will only take text as far as the first whitespace...it will not take anything after that.
User is offlineProfile CardPM
+Quote Post

k0b13r
RE: Strings
12 Apr, 2007 - 08:59 AM
Post #3

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 196



Thanked: 1 times
Dream Kudos: 250
My Contributions
Holy shit ! Fast answer.
Ok, so how can I "move around" whole text ?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
12 Apr, 2007 - 09:01 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
No, my point is you're not getting the whole text. Try your code with single word and see if it works. then try it with a string of multiple words. I think you'll find that only the first word makes it in.

If you want to take multiple words, you'll have to either overload the >> operator, or use something like the getline() function.
User is offlineProfile CardPM
+Quote Post

k0b13r
RE: Strings
12 Apr, 2007 - 10:02 AM
Post #5

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 196



Thanked: 1 times
Dream Kudos: 250
My Contributions
It works with single word. I need multiple words.
I'll try your hints. Thnx
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month