Weird problem with this C++ program

I know there are a lot of coders that hang around here, so I have a question.

I made a pretty short program that reads an input file with text already in it, and produces an output file with all the internal letters of each word scrambled up (the theory is that your brain can still easily discern what each word is if the first and last letters are the same). It’s working fine, no compile errors or warnings, but it just stops reading around word 35 and the program sits there doing nothing until I force-close. Any ideas?

Program:

/* Matthew Shubert
7/02/2009
A program that scrambles the inner portion of words from a file
to show that the words are still readable by the human mind
*/

#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<fstream>

using namespace std;

/*
This function takes an input word and scrambles the interior letters
by creating a substring and using the STL algorithm ‘random_shuffle’
*/
string scramble(string initial) {
if (initial.size() <= 3) // If the word is too short, don’t do anything
return initial;

string final;
string middle = initial.substr(1, initial.size()-2);
string compare = middle;

random_shuffle(middle.begin(), middle.end());

while (middle == compare)
	random_shuffle(middle.begin(), middle.end());  // Ensures that the substring is actually changed

final = initial.at(0) + middle + initial.at(initial.size()-1);
return final;

}

int main() {
string word;
string s_word;

ifstream ifs;
ofstream ofs;
ifs.open("input_file.txt");
ofs.open("output_file.txt", ios::trunc);

int count = 1;
while (ifs &gt;&gt; word) {
	s_word = scramble(word);
	ofs &lt;&lt; count &lt;&lt; ' ' &lt;&lt; s_word &lt;&lt; endl; // numbers the lines and has one word per line
	++count;
}

cout &lt;&lt; "Task is complete

";

return 0;

}

i tested it with 62400 words and it worked fine…

Hm ok, I guess I’ll try it again then…

Lol ok weird, it just worked for me too. Odd. Well thanks :smiley:

i tested it a few times more, and it seems there are some problems.
it stops when there is the number “3000” in my text, but for example when there is the number “480” it works fine. :thinking: :thinking: :thinking:

There is your problem, if the middle of the string is the same however you mix it up, the while loop will never exit. e.g. “food” will stay “food” no matter how often you random_shuffle “oo” :slight_smile:

Ohhhhh, haha, I didn’t even think about that! Thanks, I can get around that :smiley:

I did some programming at one time (I even took to classes on it). It was hard and not that fun so I quite. I hear that programmers get paid very well.

Er, ok.

it depends: if you are from Bangalore and are a very good programmer then you are rich (by local standards), if you are from Europe and are a very good programmer you’d better emigrate (to the US … or to Bangalore :smiley: ).
but , as usual, if you want to get paid very well it is better to be a salesperson for very bad programmers :astonished:
<back to the thread>
programming in C++ is a sin! you should be ashamed of yourself :stuck_out_tongue:

Ohh yus! You should use Java!

Nooooo, use COBOL instead :smiley:

pah, if you want to go old-school use FORTRAN

real machos still go for APL!
http://en.wikipedia.org/wiki/APL_(programming_language)[IMG]http://upload.wikimedia.org/wikipedia/en/f/ff/LifeInApl.gif[/IMG]

I have used Basic, Unix shell scripts and more recently JavaScript. For some reason, APL made me think of AWK. I always had to question the sanity of someone who could understand coding like your APL example. It is just bizarre. :roll_eyes:

nuh uh…

ahaha the guy above either didnt get what the cartoon b
above was about or he just wanted to start a whole new debate.

VIM rocks.

threadjack continues

well IMHO:

  • C++ is byzantine and is a path to damnation
  • Perl is exotic mishmash for those who think hell is not warm enough
  • APL is for programmers who inhabit multiple space/time warps (I know: in a former life I was in a team who committed an APL interpreter and ever since I pray for the lost souls who used it :D)

Darn. I missed all the action.

APL is for wusses, use brainf*ck! http://en.wikipedia.org/wiki/Brainfuck