Saturday, May 22, 2010

C++ Strings?

I have to take in a a string and scramble it 5 times, using string methods. I have to separate it using 2 radnom num's, and into three parts. It won't execute for some reason. Any help will be appreciated. Here's the code:


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter a phrase to shuffle: ";


getline(cin,phrase_input);





for(int i = 0;i %26lt;= 5;i++)


{


beg = phrase_new.substr(0,random_one);


mid = phrase_new.substr(random_one,random_two - random_one);


endphr = phrase_new.substr(random_two,phr_len - random_two);





shuffled_phrased+= endphr;


shuffled_phrased+= beg;


shuffled_phrased+= mid;





}


cout%26lt;%26lt;shuffled_phrased;

C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.





second, make sure your random_one and random_two are not greater than the length of the string phrase_new.





third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"





fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?


If not, I suggest you learn.





A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.





Learn to use breakpoints and the call stack.


No comments:

Post a Comment