2016-12-02 6 views
-1

私はプログラミングの初心者です。このコードは動作したくないので、アイデアが足りなくなりました。それはファイルをうまく読み込みますが、何も数えません。私はwhileステートメントとは何かを知っています。これは2つの別々のファイル用ですが、どちらも最後に表示する必要があります。あなたのコードで単語、行、文字の数がcで機能していない

#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"); 

} 
+1

あなたは[ 'getc'](http://en.cppreference.com/w/c/io/fgetc)について重要なことを見逃し、それは' int'を返します。これは 'EOF'と比較するときに重要です。また、スペースを検出すると、別の文字、つまりカウントされない文字が読み込まれます。エラーチェックはありません。ファイルを開けないとどうなりますか?最後に、デバッガの使い方を学んでください。デバッガを使用すると、変数とその値を監視しながら、コードを1行ずつ進めることができます。このようにすれば、問題を簡単に検出できるはずです。 –

+1

'getchar()'はstdinから読み込みます。私はあなたが連続したスペースを無視しようとしていると思います。そうであれば、読み込まれた前のcharを追跡するほうが簡単かもしれません。 –

+0

また、あなたの 'printf'文が間違っています。これを読んでください:http://en.cppreference.com/w/cpp/io/c/fprintf –

答えて

1

問題だった - :

  • あなたが置きます。直後((文字= getc(ファイル_イン))!= EOF);そのためwhileループはファイル内のすべての文字を読み込み、if()条件をチェックせずに出てきたのです。
  • linecountのif条件は、if()条件の後に;(セミコロン)を置くので不要です。
  • if(isspace(letter)& &!isspace(getchar()))のgetchar()から何をしたいですか?このgetchar()のために、ループが終了しない間に入力をしなければなりません。
  • 関数名はprint_s()です。
  • charのgetc()結果が間違っています。 getc()とその親戚からの戻り値はcharではなくintです。

最後に参考にしてください。

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); 

} 
関連する問題