如何检测C WPM程序中的Enter键?

时间:2016-01-14 04:40:33

标签: c algorithm

我应该创建一个打字导师程序,它基本上类似于明确一个段落的WPM测试。这个想法是程序将在按下回车时自动检查结果,无论用户是否完成了整个段落的输入(即:结束段落中段)。问题是,如何检测回车键或' \ n'不使用诸如使用getch的粗暴编码?

到目前为止,以下是以下代码:

#include <stdio.h>
#include <string.h>
#include <conio.h>

// Initializer

int x=0, y=0;
int mistake=0;
char entry [200][20];
char paragraph [200][20] = { // paragraph[0][0][0] = paragraph[WORD][LETTER]
/* 1) 92 WORDS, 579 CHARS */ "English", "students", "are", "sometimes", "taught", "that", "a", "paragraph", "should", "have", "a", "topic", "sentence", "or", "main", "idea,", "preferably", "first,", "and", "multiple", "supporting", "or", "detail", "sentences", "which", "explain", "or", "supply", "evidence.", "One", "technique", "of", "this", "type,", "intended", "for", "essay", "writing,", "is", "known", "as", "the", "Schaffer", "paragraph.", "For", "example,", "the", "following", "excerpt", "from", "Dr.", "Samuel", "Johnson's", "Lives", "of", "the", "English", "Poets,", "the", "first", "sentence", "is", "the", "main", "idea:", "that", "Joseph", "Addison", "is", "a", "skilled", "describer", "of", "life", "and", "manners.", "The", "succeeding", "sentences", "are", "details", "that", "support", "and", "explain", "the", "main", "idea", "in", "a", "specific", "way."};
int wordcount = { 92 };

// Interface

printf("Welcome to the Typing Tutor!\n");
printf("===============================================================================\n");
for (x=0;x<wordcount[0];x++) printf ("%s ", paragraph[0][x]);
printf("\n===============================================================================\n");
printf("Please type this paragraph below:\n\n");

// Input / Word Count Incrementer

for (x=0;x<wordcount[0];x++){
    scanf ("%s", &entry[x]);
    if (entry[x] == "\n") break; // Supposedly detects "enter", then breaks the loop. Doesn't seem to work.
    word++;
}

// Typo Checker

for (x=0;x<word;x++) if (strcmp(entry[x],paragraph[0][x]) != 0) mistake++;

// Output

y=wordcount[0]-word;

if (word>0) printf("You did not type %i words.\n", y);
else if (word<0) printf("You overtyped %i words.\n", y);
printf("You have made %i typoes.\n\n", mistake);

1 个答案:

答案 0 :(得分:0)

你可以使用getch()或kbhit()来记录输入键并存储在变量中。喜欢getch()
K =的getch()
Kbhit() - 这是一个非常好的功能,用于查找按下了哪个键