I need to read a file one word at a time. When I read the word it needs to be stored in a char* so I can pass it into my hash function. Whenever I try getline or infile>> my program just crashes. could someone please tell me what I need to use for this?

>could someone please tell me what I need to use for this?
Something besides char* because you obviously don't know what you're doing. I'd suggest using the string class, then you can use the c_str member function to get a pointer to const char for your hash function.

I already tried that but then the compiler spits out
cannot convert parameter 1 from 'const char *' to 'char *'

Member Avatar for iamthwee

Do you know what a std::string is?

The string I was using was declared as an std::string, I just get that error when I try to compile.

Member Avatar for iamthwee

Can you post your code along with a sample of your input file.

std::string tmp;     //temp string value
	char* temp="";  //Temp value to hold strings
	ifstream dict, input; //input files for the dictionary file and the article file
	ofstream outfile;      //outfile file
	dict.open("words08k.txt", ios::in);
	while (!dict.eof())
	{
		dict.getline(str,20);
		hsh.insert(temp);
	}

This is without using the std:string, it compiles correctly this way, but crashes when it reaches dict.getline . the input file is a text file with a word on each line.

a simple example is

ahead
angst
angular


I know my hash functions work fine, I've tested them, I just need a way to get my input into a char* variable before I pass it to the hash function.

I feel like an idiot, I figured out the problem. I'm really sorry for wasting you guys time on this.

on line 1 you have

char* temp="";  //Temp value to hold strings

but, I think that you should assign this pointer to some memory before you use it, i.e. something like

char *temp = new char;

Also, you have

dict.getline(str,20);
hsh.insert(temp);

but, you don't declare str at any point?

If you fix these bits then you might have more luck.

Sorry, I think I was posting that reply as you were posting yours. Glad you fixed your code :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.