Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 284 285 [286] 287 288 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907128 times)

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4275 on: March 22, 2013, 01:49:48 pm »

So I'm writing a primitive spellchecker this time, and I was given this function by the prof:

Code: [Select]
if (stricmp((filedata.c_str(),dictionarydata.c_str())==0))
Where filedata and dictionary data are two strings I have previously acquired. I get the error "function does not take 1 arguments" and also "argument of type 'bool' is incompatible with 'const char'." Why is this?
Logged
He's just keeping up with the Cardassians.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4276 on: March 22, 2013, 01:56:08 pm »

That's quite simple.

( (filedata.c_str(),dictionarydata.c_str())==0  )
is a boolean. And stricmp() doesn't take booleans. Actually, I don't know what the hell that is. I have no idea what (x, y) == 0 does.

I think the function should be
if (stricmp( filedata.c_str(), dictionarydata.c_str() ) == 0)
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4277 on: March 22, 2013, 02:10:20 pm »

Code: [Select]
if (stricmp(filedata.c_str(),dictionarydata.c_str())==0)
FTFY
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4278 on: March 22, 2013, 02:22:17 pm »

Thanks. That makes sense.

Next question, in the same program I have this loop to do the actual checking.

Code: [Select]
do {
        inputfile>>filedata;
[Removing the extraneous characters]
dictionary.open("dictionary.txt");
do {
cin>>dictionarydata;
if (stricmp(filedata.c_str(),dictionarydata.c_str() ) == 0)
dictionarycounter++;
} while (!dictionary.eof());
if (dictionarycounter==0)
cout<<filedata<<endl;
dictionarycounter=0;
} while (!inputfile.eof());

But it prints nothing. I've tried rearranging it several different ways and nothing seems to happen. If I take out the nested loop it'll print all the words from my file just fine. And I have made certain there are in fact misspelled words in the file I'm spellchecking.
Logged
He's just keeping up with the Cardassians.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4279 on: March 22, 2013, 02:52:48 pm »

Shouldn't you read from dictionary instead of cin?
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4280 on: March 22, 2013, 09:40:25 pm »

Oh man, of course. That helps. I hate when I do shit like that.
Logged
He's just keeping up with the Cardassians.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4281 on: March 23, 2013, 03:30:20 pm »

Also, may I suggest that you only read your dictionary into memory once? You read the entire dictionary from the disk for every word in your loop.
If you already have access to the container libraries, you can read the dictionary into a set<string>, and check for misspellings with (yourSet.count(possiblyMisspelledWord) == 0).
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4282 on: March 24, 2013, 11:34:55 am »

We aren't allowed to use anything not discussed in class. Anyway, I fixed a couple small problems, yielding this:

Code: [Select]
do {
        inputfile>>filedata;
[Removing the extraneous characters]
dictionary.open("dictionary.txt");
do {
dictionary>>dictionarydata;
if (stricmp(filedata.c_str(),dictionarydata.c_str() ) == 0)
dictionarycounter++;
} while (!dictionary.eof());
if (dictionarycounter==0)
cout<<filedata<<endl;
dictionarycounter=0;
} while (!inputfile.eof());

And it still outputs nothing. I know everything except the stricmp loop works, because when I take it out the program outputs all the words in the file, less the characters I removed, but when I put that loop back in it prints nothing. I've concluded that my logic isn't broken, because I've changed the if statement with dictionary counter to be if if (dictionarycounter>0), and still nothing happens. I'm afraid I have no clue what the issue is.
Logged
He's just keeping up with the Cardassians.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4283 on: March 25, 2013, 01:11:15 am »

Warning: may be incorrect.

Your logic probably is incorrect, somewhere. O_o Rarely is the code wrong and you right.

What does stricmp do? IBM docs say that it compares the strings and returns <0, 0, or >0, when they are lesser than, equivalent to, and greater than, respectively. What does greater than/lesser than mean? It implies length, but ... :/
I tried testing with codepad but it isn't in its library. Probably a compiler-specific extension.

Testing eof() in a do-while loop seems a bit weird, too—I think you'll end up pulling from the stream on time too many, but that isn't relevant.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4284 on: March 25, 2013, 01:27:45 am »

stricmp actually determines the lexicographical ordering of two words without regard to case.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4285 on: March 25, 2013, 01:39:35 am »

... Huh. Why would the class have to use stricmp instead of some other, less roundabout function? >_>
Some other suggestions; Step through the code with a debugger and smaller dictionary and input files to see if eof() happens too late, and when ++ is called. :D
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4286 on: March 25, 2013, 02:02:42 am »

So what exactly is the program supposed to do? I've looked at it several times and it seems to output all words in the input file that are not in the dictionary.
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4287 on: March 25, 2013, 09:00:56 am »

I found my problem; I was missing an "=" in an if statement, so it was resetting one of my counters instead of working as an actual if statement.

Yeah, I'm using stricmp because I have to, not because it's the best way.
Logged
He's just keeping up with the Cardassians.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4288 on: March 28, 2013, 09:09:23 am »

http://thc.org/root/phun/unmaintain.html

"How To Write Unmaintainable Code — Ensure a job for life! ;)"

This made me laugh today XD
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4289 on: March 28, 2013, 12:13:59 pm »

http://thc.org/root/phun/unmaintain.html

"How To Write Unmaintainable Code — Ensure a job for life! ;)"

This made me laugh today XD
That page is surprisingly informative, even if you don't intend to obfuscate your code.
Logged
Pages: 1 ... 284 285 [286] 287 288 ... 796