Problem executing compiled code.

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

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "words.h"
#include <ctype.h>

using namespace std;

void getWord (ifstream& file, string& s){		
		char c;	
		ifstream inputFile;	
		inputFile.get(c);
		if(isalpha(c) && c != EOF){
			s += c;			
			inputFile.get(c);
			while (isalpha(c)&& c != EOF){
				s += c;
				inputFile.get(c);				
			}
		} 
			else {
				while (isalpha(c) == false && c != EOF) {
					inputFile.get(c);
				}				
				if (isalpha(c) && c!= EOF){
					s += c;
					inputFile.get(c);
					while (isalpha(c) && c != EOF){
						s += c;
						inputFile.get(c);
					}
				}
			}
}			
bool eraseChar(char c, string& s){
	if (s.find(c) == true){
		s.erase(c,1);
		return true;
		} else{
			return false;
		}
	}
bool isSubset (string subset, string s){
	int index = 0;
	string j = s;
	while (index < s.length()) {
		char check = s[index];
		if (s.find(check)){
			eraseChar(check, s);
			index = index +1;			
		}
	}
	if (s == j){
		return false;
	} else {
		return true;
		}
}
string removeSubset(string subset, string s){
	int windex = 0;
	int Lsubset = subset.length();
	while (windex < Lsubset){
		char check = subset[windex];
		if (subset[check] && s[check]){
			s.erase(check,1);
		}
	}
	return s;	
}



and here is the program I was provided with for this assignment, written by my professor. I provide it as context.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "words.h"
using namespace std;

// global variables
string dictionary[50000];
int dictionaryLength = 0;
string solution[50];
int solutionLength = 0;

void findWords(int dictionaryStart, string s) {
  if (s.empty()) {
    for (int i = 0; i < solutionLength; i++) {
      cout << solution[i] << " ";
    }
    cout << endl;
  } else {
    for (int i = dictionaryStart; i < dictionaryLength; i++) {
      string word = dictionary[i];
      if (isSubset(word, s)) {
	string news = removeSubset(word, s);
	solution[solutionLength] = word;
	solutionLength = solutionLength + 1;
	findWords(i, news);
	solutionLength = solutionLength - 1;
      }
    }
  }
}  

int main (int argc, char *argv[]) {
  ifstream infile;
  string  source;
  string dummy;
  string dictionaryName;
  if (argc == 1) {
    source = "geoffrey";
    dictionaryName = "wordssorted.txt";
  } else if (argc == 2) {
    source = argv[1];
    dictionaryName = "wordssorted.txt";
  } else if (argc == 3) {
    source = argv[1];
    dictionaryName = argv[2];
  }
  infile.open(dictionaryName.c_str());
  while (! (infile.eof())) {
    getWord(infile, dummy);
    if (! (infile.eof())) {
      dictionary[dictionaryLength] = dummy;
      dictionaryLength = dictionaryLength + 1;
    }
  }
  infile.close();
  cout << dictionaryLength <<
    " words found in dictionary " << dictionaryName << endl;
  cout << "First word: " << dictionary[0] << endl;
  cout << "Last word:  " << dictionary[dictionaryLength-1] << endl;

  cout << endl << "Anagrams for " << source << ":" << endl << endl;
  findWords(0, source);

  return 0;
}


I also have a header file for this but I am pretty damn sure it not it so I omitted it.
Last edited on
What are the isSubset and removeSubset supposed to do?
isSubset is supposed to checks to see if the characters in subset occur in string s and then uses eraseChar to remove them.

removeSubset sees if subset is part of string s and if it is it removes those characters from s and returns the augmented string, s.

all functions defined in my code are required by the assignment to be there.

Ive figured out that getWord is the problem but I can't figure out why.
Last edited on
Parameter file of getWord. What does it do?
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);
1
2
3
4
5
6
void getWord (ifstream& file, string& s){		
		char c;	
		ifstream file;	
		infile.get(c);
                // rest of code 


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.
1
2
3
4
void getWord (ifstream& file, string& s){		
		char c;	
		ifstream file;	
		infile.get(c);

I get an error from the compiler stating "error C2065: 'infile' : undeclared identifier"


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?

What happens when you have a parameter with a name and create a local variable in the function with the same name?
Last edited on
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
It's not clear from the code what isSubset is supposed to do. For instance, the parameter subset is never used. That seems a bit odd.

1
2
		char check = s[index];
		if (s.find(check)){


Set check to a char contained in s, then see if check is contained in s. Anything pop out at you there?
Is there a reason to read word one character at a time? Are you allowed to:
http://www.cplusplus.com/reference/string/string/operator%3E%3E/
Are you in CSCI 140 op?
Are you in CSCI 140 op?


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
Last edited on
Topic archived. No new replies allowed.