ok guys this is my code here well my problem is the code wont show up the way its supposed this is what it should do:
//Read a line of 5 words separated by colons and no spaces.
//Extract the 5 words
//Print the 5 words in reverse order, one word per line.
i think my code is alright but when it tried to put them in reverse order it doesn't seem to do it right.
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
usingnamespace std;
//Progam 1b
//Rohan Shah
//6/2/11
//Read a line of 5 words separated by colons and no spaces.
//Extract the 5 words
//Print the 5 words in reverse order, one word per line.
string words;
string first;
string second;
string third;
string fourth;
string fifth;
string delim;
int pos;
int main ()
{
cout << "enter five words seperate them by colon's and start with a colon\n"
<< "" ;
getline(cin, words);
delim = words.substr (0, 1);
pos = words.find(delim);
words = words.substr (1);
first = words.substr (0, pos);
words = words.substr (pos+1);
pos = words.find(delim);
second = words.substr (0, pos);
words = words.substr (pos+1);
pos = words.find(delim);
third = words.substr (0, pos);
words = words.substr (pos+1);
pos = words.find(delim);
fourth = words.substr (0, pos );
words = words.substr (pos+1);
pos = words.find(delim);
fifth = words;
cout << fifth << fourth << third << second << first << endl; ;
system ("pause");
return 0;
}
You look for the position of the first delimiter before removing the colon from the beginning of the string, so your first word is always the empty string. Thus, when you get to the end you have two words remaining as your fifth word.