-2

So I have a program that takes user input and compares it to a line in the file, before going down onto the next line, if the user gets the word right they get 2 points, if it's wrong they get 1 point. As a failsafe I have added a small function within the program that will take out any spaces from the word.

The program works as expected, the spaces are removed and when run all words are scanned and compared effectively.

HOWEVER, once on the last line of the file, the correct spelling of the word will give the wrong output, this might have something to do with the loops but I'm not sure.

In a nutshell: All I need is one of you talented programmers out there to take a look at my code and see what's causing this to happen.

File content (just a list of random words)

Baby
Milk
Car
Face
Library
Disc
Lollipop
Suck
Food
Pig

(libraries are stdio,conio and string)

char text[100], blank[100];
int c = 0, d = 0;

void space(void);

int main()
{
    int loop = 0;
    char str[512];
    char string[512];
    int line = 1;
    int dis = 1;
    int score = 0;
    char text[64];

    FILE *fd;

    fd = fopen("Student Usernames.txt", "r");   // Should be test

    if (fd == NULL)
    {
        printf("Failed to open file\n");
        exit(1);
    }

    do
    {
        printf("Enter the string: ");
        gets(text);

        while (text[c] != '\0')
        {
            if (!(text[c] == ' ' && text[c] == ' '))
            {
                string[d] = text[c];
                d++;
            }
            c++;
        }

        string[d] = '\0';
        printf("Text after removing blanks\n%s\n", string);

        getch();

        for(loop = 0;loop<line;++loop)
        {
            fgets(str, sizeof(str), fd);
        }
        printf("\nLine %d: %s\n", dis, str);
        dis=dis+1;
        str[strlen(str)-1] = '\0';
        if(strcmp(string,str) == 0 )
        {
            printf("Match\n");
            score=score+2;
        }
        else
        {
            printf("Nope\n");
            score=score+1;
        }
        getch();
        c=0;
        d=0;
    }
    while(!feof(fd));
    printf("Score: %d",score);
    getch();
}

For any input on the last line, the output will always be incorrect, I believe this is something to do with the for loop not turning it into the next variable, but seeing as the <= notation makes this program worse, I really just need a simple fix for the program thanks.

P.S. For anyone who is going to comment about my coding for the spaces function, Yes, I could make it better, but it's not a problem right now. So please don't write anything concerning it.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Avoid using `gets()`. Consider `fgets()`. – chux - Reinstate Monica Feb 25 '14 at 21:57
  • 2
    [Usage of `feof` by non-experts is usually wrong](http://stackoverflow.com/q/5431941/509868), especially when you have problems at end-of-file – anatolyg Feb 25 '14 at 21:57
  • Also consider `strncmp`. – pattivacek Feb 25 '14 at 21:57
  • @anatolyg is there an alternative to EOF? – user3344560 Feb 25 '14 at 21:59
  • 3
    This is the 5th+ posting of this baseline code and the problem here is answered in http://stackoverflow.com/questions/22023144/c-programming-file-not-read-right/22023233#22023233. Suggest reviewing all answer to previous posts before posting again. – chux - Reinstate Monica Feb 25 '14 at 22:00
  • 4
    I [already answered](http://stackoverflow.com/questions/22023144/c-programming-file-not-read-right/22024067#22024067) many of the flaws on one of the several times you've asked about nearly the same code; please fix at least the known bugs before starting a new question. You still have the `feof` and the `gets`, etc. – Arkku Feb 25 '14 at 22:01
  • @chux and Arkku yes, after 5 times asking no one has given a suitable answer, as I recall I've had plenty of hints about my perfectly functioning space remover. All I want is to known what the hell I can do to make the program actually compare the last line of file correctly. – user3344560 Feb 25 '14 at 22:10
  • 1
    Test `fgets(str, sizeof(str), fd) == NULL` for your loop exit condition, drop using `feof()`, drop using `gets()`. – chux - Reinstate Monica Feb 25 '14 at 22:14
  • Thanks, but it still doesn't work. I might just give up for now. – user3344560 Feb 25 '14 at 22:20
  • @user3344560: Repeatedly asking about code that uses `gets()` and `feof()` is not going to be useful. Fix those problems as others have suggested. If your program still doesn't work, then ask about code that has those problems fixed. We've *never seen* your revised code that you say still doesn't work, and we can't help you with it until we do. – Keith Thompson Feb 25 '14 at 22:25
  • @user3344560 You can also *debug* your program: for example, add `puts(str)` and `puts(string)` near your `strcmp` call. BTW you should have done it before asking; maybe you forgot about this or did it unsuccessfully somehow? – anatolyg Feb 25 '14 at 22:26
  • @anatolyg Noted, did the puts thing, the two words are both equal, input isn't right still. As for the guy above I'm not going to change the gets() and the FEOF() if they work, unless you have some alternate solution that works better – user3344560 Feb 25 '14 at 22:31
  • `feof()` _is_ the "last line" problem. – chux - Reinstate Monica Feb 25 '14 at 22:34
  • So, if feof is the problem, how can I fix this?, do you want another function that will find the amount of lines in a file and stick it into a for loop? – user3344560 Feb 25 '14 at 22:37
  • 1
    As said before "Test `fgets(str, sizeof(str), fd) == NULL` for your loop exit condition". – chux - Reinstate Monica Feb 25 '14 at 22:38
  • `while(fgets(str, sizeof(str), fd) == NULL );` Is this correct? – user3344560 Feb 25 '14 at 22:41
  • The code does not have an easy way to quit reading so I can not provide code. But `fgets()` will return NULL when the is no more data. That is the code's cue to quit processing input. Testing `feof()` to see if you are done is too late for it happens _after_ `fgets()` returns NULL. – chux - Reinstate Monica Feb 25 '14 at 22:49

2 Answers2

0

I guess your file is not terminated by a newline. So the last word, Pig, gets truncated by this line of code:

str[strlen(str)-1] = '\0';

(which is unconditional).

Either put a newline at the end of your file, or check the end-of-string before truncating:

if (isspace(str[strlen(str)-1]))
    str[strlen(str)-1] = '\0';

(Might also use strtok to remove all whitespace from the string without writing tricky code)

anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

You need to check if the last character is a linefeed in both the user input and the line read from the file, and remove it if and only if it is. (You also need to fix the other bugs, such as the use of gets and feof, and not all the changes can be done in isolation because some of your bugs depend on one another so fixing only one will break it until you fix the others.)

Arkku
  • 41,011
  • 10
  • 62
  • 84