Help me please (correcting some command errors)

This is a discussion on Help me please (correcting some command errors) within the C Programming forums, part of the General Programming Boards category; when i always run the program then started to input a correct letter, the total_tries always trigger to decrements by ...

Help me please (correcting some command errors)

when i always run the program then started to input a correct letter, the total_tries always trigger to decrements by 1 and when i am going to input a wrong letter it is decrements by 2...i cant find what is the problem. can anyone help me for my problem? thanks alot, sorry for the long code...

@Hemerson:
First off, you could use some better indentation, it's hard to follow your code. If your code is messy, it means you're more likely to make mistakes and it's harder to find and fix them. Also, it would help if you provided complete code, because I don't know how you declared your variables, so you may have problems related to that too, and I can't put your code in my compiler to check for basic errors that way either.

I'm guessing your problem with decrementing total_tries is the new line. You type a letter, then press enter. That enter is put in the input buffer (as a '\n'), just like the letter you typed, so getchar will read it. Since the newline is not in the word you're guessing, so it acts like you guessed an incorrect letter. I would try something like:

Code:

letter = getchar();
if (letter == '\n')
continue; // don't process the new line, let the user enter another character

Also, I think your if statement around line 113 is missing a set of curly braces.

@anduril462, there is another error that i can't fix...the repitition of letter...when i enter again a letter, at first its ok...but entering again and again the letter, it automatically saying that "i guess it press enter to exit", how to fix it?, thanks...

I'd like to see your for loops changed. Instead of i=0;i<length;i++, I'd like to see good=0;good<length;no increment here. Instead, increment good only when it has matched another letter in the word being guessed. (And increment it by two, if the letter is matching a letter that is repeated in the word being guessed at, etc.)

You have loops, that should be consolidated to make the code easier and shorter. And please, get your indentation up to snuff.

I'd like to see your for loops changed. Instead of i=0;i<length;i++, I'd like to see good=0;good<length;no increment here. Instead, increment good only when it has matched another letter in the word being guessed. (And increment it by two, if the letter is matching a letter that is repeated in the word being guessed at, etc.)

You have loops, that should be consolidated to make the code easier and shorter. And please, get your indentation up to snuff.

i did'nt get it clearly, sorry sir. can you give me some examples?. thanks.