-1

I am new to programming and this code does not want to work and I have run out of ideas. It reads in the files fine but does not count anything. I know its something to do with the while statements. This is for two separate files but they both need to be shown at the end.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
char letter;
char filename1[50];
char filename2[50];

//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;

//Gets the user to enter name of file, then puts it in string.
printf("\n Enter first text document\n");
scanf("%s", filename1);
printf("\n Enter second text document\n");
scanf("%s", filename2);

//opens then reads the first file.
file_in = fopen(filename1, "r");

// counts the number of words, then lines, then letters in doc 1. 
while ((letter = getc(file_in)) != EOF);
{
    if (isspace(letter) && !isspace(getchar()))
    {
        wordcount++;
    }
    if (letter == '\n');
    {
        linecount++;
    }
    if (letter == '-')
    {
        charcount++;
    }
}
fclose(file_in);

//opens then reads the second file.
file_in = fopen(filename2, "r");

// counts the number of words, then lines, then letters in doc 2.
while ((letter = getc(file_in)) != EOF);
{
    if (isspace(letter) && !isspace(getchar()))
    {
        wordcount++;
    }
    if (letter == '\n');
    {
        linecount++;
    }
    if (letter == '-')
    {
        charcount++;
    }
}

//displays the total on screen.
printf_s("Words:", wordcount, "\n");
printf_s("Letters", charcount, "\n");
printf_s("Lines", linecount, "\n");

}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
l.stannard
  • 27
  • 3
  • 1
    You miss an important thing about [`getc`](http://en.cppreference.com/w/c/io/fgetc), it returns an `int`. This is important when comparing to `EOF`. Also, if you detect a space, you read another character, one that will not be counted. And there's no error checking, what happens if you fail to open a file? Lastly, please learn how to use a debugger. With a debugger you can step through the code, line by line, while monitoring variables and their values. This way you should easily be able to detect the problems you have. – Some programmer dude Dec 02 '16 at 13:00
  • 1
    `getchar()` reads from stdin. I think you are trying to ignore sequential spaces. If so, it might be easier just to keep track of the previous char read. – 001 Dec 02 '16 at 13:08
  • Also, your `printf` statements are wrong. Read this: http://en.cppreference.com/w/cpp/io/c/fprintf – 001 Dec 02 '16 at 13:13
  • SO isn't a debugging service. Compile with symbols, run the code inside a debugger to trace through the program(s) line by line inspecting the values of the relevant variables to learn what is really going on. If then a *specific* question arises feel free to come back here. – alk Dec 02 '16 at 14:28
  • Note that `while ((letter = getc(file_in)) != EOF);` reads all the characters in a loop because of the trailing semicolon. The loop leaves 'EOF' (converted to `char` — it should be an `int`) in `letter`, which is then analyzed. You have a similar problem at `if (letter == '\n'); { linecount++; }` — the semicolon after the condition means that `linecount++` occurs unconditionally. You have two blocks of code that are practically the same (down to the same bugs); they should be in a function that is called twice. – Jonathan Leffler Dec 02 '16 at 14:40
  • `printf_s("Words:", wordcount, "\n");` is invalid. – BLUEPIXY Dec 02 '16 at 15:43

1 Answers1

1

problems with your code were -:

  • you put ; just after while ((letter = getc(file_in)) != EOF); that's why while loop read all characters in a file and came out without checking if() conditions.
  • your if condition for linecount was unnecessary called because you also put ;(semicolon) after if() condition.
  • what do you want from getchar() here in if (isspace(letter) && !isspace(getchar()))? Because of this getchar() you have to give input otherwise while loop will not terminate.
  • there is function name is print_s().
  • You are receiving getc() result in char which is wrong. The return value from getc() and its relatives is an int, not a char.

for last point you can refer.

While (( c = getc(file)) != EOF) loop won't stop executing

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
char letter;
char filename1[50];
char filename2[50];

//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;

//Gets the user to enter name of file, then puts it in string.
printf("\n Enter first text document\n");
scanf("%s", filename1);
printf("\n Enter second text document\n");
scanf("%s", filename2);


//opens then reads the first file.
file_in = fopen(filename1, "r");

// counts the number of words, then lines, then letters in doc 1. 
while ((letter = getc(file_in)) != EOF)
{
    if (isspace(letter))
    {
        wordcount++;
    }
    if (letter == '\n')
    {
        linecount++;
    }
    if (letter == '-')
    {
        charcount++;
    }
}

fclose(file_in);

//opens then reads the second file.
file_in = fopen(filename2, "r");

// counts the number of words, then lines, then letters in doc 2.
while ((letter = getc(file_in)) != EOF);
{
    if (isspace(letter) && !isspace(getchar()))
    {
        wordcount++;
    }
    if (letter == '\n');
    {
        linecount++;
    }
    if (letter == '-')
    {
        charcount++;
    }
}

//displays the total on screen.
printf("Words......%d:", wordcount);
printf("Letters....%d", charcount);
printf("Linesi....%d", linecount);

}
Community
  • 1
  • 1
Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19