Hello all. I'm new to the language and trying to finish off a lab problem. We are expected to prompt a five word, spaceless input divided by hashes to read to a string, so in this format:
word1#word2#word3#word4#word5
Next we are to repeat the input as an output, but this is of no issue. My concern involves the next task, extracting each word to its own substring in order to reverse the original order of inputted words, and read these reversed words to a new string which is also divided by hashes.
My issue is that I do not get a successful result from running the program unless the length of every word (or substring) is the same. If one word is varied in character length from the others, I get a jumble which sometimes also contains more than four hashes.
I've got a good chunk of code done, and feel like I'm just missing a small stupid detail. I will provide my code. While I realize it's probably a bit unwieldy, the general form of it should do as I showed my progress to my professor and he attested that I'm very close to a solution. My work to date is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string totalF;
string totalR;
string word1;
string word2;
string word3;
string word4;
string word5;
cout << "Please enter five words separated by hashes (#) and without spaces.";
cout << "\n(Example: one#two#three#four#five)\n\n";
cin >> totalF; //input string
cout << endl << totalF << endl << endl;
string delimiter = "#";
size_t pos = 0;
pos = totalF.find(delimiter) != string::npos;
word1 = totalF.substr(0, pos); //create substring containing first word in input string
totalF.erase(0, pos + delimiter.length());
word2 = totalF.substr(0, pos); //create substring containing second word in input string
totalF.erase(0, pos + delimiter.length());
word3 = totalF.substr(0, pos); //create substring containing third word in input string
totalF.erase(0, pos + delimiter.length());
word4 = totalF.substr(0, pos); //create substring containing fourth word in input string
totalF.erase(0, pos + delimiter.length());
word5 = totalF.substr(0, pos); //create substring containing final word in input string
totalF.erase(0, pos + delimiter.length());
totalR = word5 + "#" + word4 + "#" + word3 + "#" + word2 + "#" + word1; //combine substrings in reverse order
cout << totalR << endl << endl;
system("pause");
return 0;
}
|
I am asking for any advice on how to clean this up and make the code work to return a string with each of the five words in reversed order from the input. My professor hinted that I will need to assign/reassign a pos value after each iteration of substr/erase, so I believe 4 times more total. My problem is that I have tried both reassigning
'size_t pos = 0;'
or reassigning
'pos = totalF.find(delimiter) != string::npos;'
after each substr/erase and this doesn't seem to do the trick. I'm not sure if the problem has something to do with the pos or delimiter values within the substr/erase statements themselves, though this could be possible too. Thanks for any and all help!