So I finally got my assignment to compile but I have no idea how to run it. I am writing a program for anagrams and I was provided with the main program and I was supposed to write the subprogram. I got it to compile and link correctly but when I try to execute the program with ./anagram louisiana, the cursor just blinks and nothing happens. I am not sure if it is because I typed in the incorrect commands to execute or if my program is just on an infinite loop and technically running correctly so it doesn't crash or just end.
Please don't solve this for me, just point me in the right direction. I don't want to get accused of plagiarism.
I think the error is in my code for void getWord but I am not sure.
here is what I came up with
so as far as I can tell, the anagram.cpp file passes the name of the dictionary file into getWord. this is done on lines 49 and 51 of my professors code and file is the name of the parameter in my code. Ive been playing around with my code and if i change the boolean value on line 23 from false to true I get an error that says "Segmentation fault (core dumped)" and if the value is true then the program just hangs on execution.
so as far as I can tell, the anagram.cpp file passes the name of the dictionary file into getWord.
Not correct. An object of type ifstream is not a string. It is a file stream. You ignore it completely. Instead you create a new file stream in getWord that is never opened, but you treat it as if it were.
An object of type ifstream is not a string. It is a file stream. You ignore it completely. Instead you create a new file stream in getWord that is never opened, but you treat it as if it were.
okay so thats what I originally thought but I couldnt get it to compile. So I would need to open the file each time getWord is called and then close it at the end of its execution, right? Im gonna play around with it some more and think about it, I think theres an infinite loop in the isSubset procedure.
okay so thats what I originally thought but I couldnt get it to compile. So I would need to open the file each time getWord is called and then close it at the end of its execution, right?
No. You simply need to utilize the already open file stream that is fed to getWord to extract a single word from the file and place it into the string that is also fed as a parameter to getWord.
You simply need to utilize the already open file stream that is fed to getWord to extract a single word from the file and place it into the string that is also fed as a parameter to getWord.
hmm okay so the file is already open i just need to use it. In our past assignments we have used something like the following to extract single characters from file streams.
1 2 3
ifstream infile; //this is done to initialize the file?? not sure the purpose of this statement
infile.open(file); //this is already done in anagrams.cpp so dont need to do it in mine
infile.get(c); // to grab a character from file stream for manipulation
however when I substitute infile.get(c); for inputFile.get(c);
I get an error from the compiler stating "error C2065: 'infile' : undeclared identifier"
and "error C2228: left of '.get' must have class/struct/union type is "unknown-type"
I've never had this problem before when writing programs that extract character like this in main. This is my first time writing a subprogram that will be used by main so is the reason it does not work because infile does not work for subprograms? I don't understand why inputFile.get(c) compiles (but seemingly fails to extract character) and infile.get(c) does not.
The compiler says it doesn't see a variable named infile in getWord. Do you? What is the name of the parameter of type ifstream&? Is it infile?
damn, your right. I feel silly. So there is not an infinite loop in getWords making it hang after all. I executed the code with the correct file.get(c) and then the cursor started blinking so I walked away in disgust and when I got back the command prompt was just outputing a very very large string of words over and over again. Guess it needed time to process before it output. I included an empty string at the beginning of getWords so that the string resets and I don't get a string the entire length of the dictionary.
s= ""; // inserted this at the very beginning of getWords
however there is an infinite loop in the findWords procedure which was written by my professor and the findWords procedure uses the functions I wrote. eraseChar does not have a loop in it so thats not it but isSubset and removeSubset do contain loops so it must be in there. Any hints are appreciated
yes and please don't copy my code. You will get caught and we'll both get fucked. I imagine its not hard for a comp sci department to automatically check submitted assignments for similarities. In fact, one of the upcoming projects is to do pretty much exactly that I think.
I think Ive about figured this out my only problem now is that string removeSubset() is supposed to return a string right? I know that the procedures in removeSubset() work because before every statement in removeSubset, ive done
1 2 3
cout<<"entering procedure"
//procedure code here
cout<<"exiting procedure"
and the procedures and loops have gone fine but when I get to the end and the program gets to
return s;
the program crashes. I dont want to post the exact code so it does not get copied by any unscrupulous classmate but I have removeSubset(string Subset, string s) pass in Subset and s, augments s by eliminating the characters in s that appear in subset with the same frequency after checking if it is a truly a subset and then returns the final augmented s string.
Am I supposed to return something else? I have to return a string but returning s causes it to crash. Not sure whats going on
Edit: Figured it out, problem solved. Thanks for everyones help