this may be a little bit better way of implementing this....
it's basically the same, but try to make your functions do one thing and one thing only. that's called cohesion. you should probably break it up even more (than i have)....like a function the prints "Typing tutor..."
and then one that is the actual typing part of it....and then a function that grades it....although this program doesn't really need such cohesion, if you wanted to enhance it and make it bigger and better, then breaking it up into smaller pieces is the best way to plan for the future....or even the best way to build, manage, and think about any program....
i typically try to make main have nothing but function calls...i think of main as the conducter of the program....
Code:
#include <stdio.h>
#include <time.h>
#define HOME "asdfghjkl;\'\""
#define RANGE sizeof(HOME)-1
#define LINES 2 //number of lines to type
void fillstring(char *s,int error);
int typestring(char *s,int *size);
int main()
{
char string[16];
char *s;
s = string;
int do_again = 1;
int er = 0;
int *error;
error = &er;
c = getche();
while(c != 'Y' || c != 'y')
{
typestring(s, error);
printf("try again (y/n)");
c = getche();
}
return 0;
}
void typestring(char *s,int *error)
{
char c;
*error = 0;
int line = 0;
do
{
fillstring(s,RANGE);
system("cls");
printf("Typing Tutor 0.1\n");
printf("Type the following lines:\n");
printf("\n%s\n",s);
do
{
while((c = getch()) != *s)
(*error)++;
printf("%c",c);
s++;
}
while(*s);
line++;
if(line == LINES)
break;
}
while(1);
printf("\n");
printf("\nYou typed %i characters wrong!\n",*error);
if(*error > 5) printf("\nYou suck!");
}
void fillstring(char *s,int size)
{
int i;
char home[] = HOME;
srand((unsigned)time(NULL));
for(i=0;i<16;i++) //fill string with home row characters
{
*(s+i) = home[rand()%size];
while(*(s+i) == *(s+i-1)) //this loop keeps letters from repeating
*(s+i) = home[rand()%size];
}
*(s+16) = '\0';
}