-
Typing Tutor Project
I am doing this project on a typing tutor software. Done a few stuff already. But stuck with certain problems. Would be very grateful if someone helped me out :-)
Here goes my code:
Code:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <time.h>
void extview(int, int);
void main(){
FILE *fp;
int i;
int x=0;
int y=0;
int z=4;
int errcnt=0;
int cnt=0;
int spcnt=0;
float acc;
time_t start=0, end=0;
char str[650];
char ch;
clrscr();
fp=fopen("C:\\test.txt","rt");
fgets(str,650,fp);
extview(z, i);
/* Loop To Print The String */
for(i=0;i<strlen(str);i++){
gotoxy(10+(x++),5+y);
printf("%c", str[i]);
if ((i%50==0) && (i!=0)){
y+=3;
x=0;
}
}
x=0;
y=0;
ch=0;
/* Getting Input From The User & Highlighting Wrong Input */
for(i=0;i<strlen(str) && ch!=27;i++){
gotoxy(10+(x++), 6+y);
ch=getch();
if (start==0){
start=time(NULL);
}
cnt++;
textcolor(WHITE);
if (ch!=str[i]){
textcolor(RED);
if (ch==32) cprintf("%c", 24);
else cprintf("%c", ch);
sound(500);
delay(100);
nosound();
errcnt++;
}
else
printf("%c",ch);
if((i%50==0) && (i!=0)){
y+=3;
x=0;
}
}
/* Accuracy & WPM Calculation */
end=time(NULL);
cnt--;
errcnt--;
acc = (1.0*(cnt - errcnt) / cnt) * 100.0;
gotoxy(64,8);
printf("Accuracy: %.1f%", acc);
gotoxy(64,14);
printf("WPM: %.2f", (65/(difftime(end, start)/60)));
gotoxy(64,18);
printf("%d", spcnt);
getch();
}
void extview(int z, int i){
gotoxy(8,3);
printf("%c", 218);
for(i=0;i<53;i++){
printf("%c", 196);
}
gotoxy(62,3);
printf("%c", 191);
gotoxy(8,4);
for(i=0;i<40;i++){
printf("%c", 179);
gotoxy(8,z++);
}
z=4;
for(i=0;i<40;i++){
printf("%c", 179);
gotoxy(62,z++);
}
gotoxy(8,43);
printf("%c", 192);
gotoxy(62,43);
printf("%c", 217);
z=4;
gotoxy(9,43);
for(i=0;i<53;i++){
printf("%c", 196);
}
gotoxy(63,3);
printf("%c", 218);
gotoxy(63,4);
z=4;
for(i=0;i<40;i++){
printf("%c", 179);
gotoxy(63,z++);
}
gotoxy(63,43);
printf("%c", 192);
gotoxy(64,3);
for(i=0;i<16;i++){
printf("%c", 196);
}
gotoxy(80,3);
printf("%c", 191);
gotoxy(80,4);
z=4;
for(i=0;i<40;i++){
printf("%c", 179);
gotoxy(80, z++);
}
gotoxy(80,43);
printf("%c", 217);
gotoxy(64,43);
for(i=0;i<16;i++){
printf("%c", 196);
}
}
What I wanna do here is that after the text file is opened the number of spaces in the whole string (650 characters in total and 65 words) will be calculated. But I have no idea how to do that.
-
Do you know how to use a loop of any kind? Do you know how to increment a variable? Do you know how to test a character to see if it's a space? There ya go.
Quzah.
-
Thx for replyin :-) I know all that. But I can't do it. I tried using this:
Code:
while(1){
ch=fgetc(fp);
if (ch==" ") spcnt++; //spcnt is the count of spaces
}
That didn't work. Any other suggestions? :-)
-
Well for starters, if you had paid attention to your compiler's warnings, it would have told you what you were doing wrong. But, in the event you don't have them on:This is a string. You can't compare strings with the == operator. You want to compare a single character.Notice the single quotes. Single quotes, single character.
A better way perhaps would be:
Code:
while( (c = fgetc( fp )) != EOF )
if( c == ' ' )
spcnt++;
There are a number of ways to do this, but that's fairly simple, and it breaks out of the loop correctly when it hits the end of the file. Of course, 'c' needs to be int in this case to handle EOF. It's in the FAQ if you care for further explanation.
[edit] void main is wrong, and is also covered in the FAQ. [/edit]
Quzah.
-
Thx for replying. I will definately try that out. So I need to declare c as an integer haa. I seen a lot of books using int main() some using only main() others using void main(). Dunno the difference. Where is the FAQ located? Am new here! :-)
-
This isn't working actually :S:S
-
There is a link to the FAQ at the top of the page, or else click here.
-
-
Code:
fp=fopen("C:\\test.txt","rt");
You should test to see if the open was successfull.
Code:
if((fp=fopen("C:\\test.txt", "rt")) == NULL) perror("Can't open file"), exit(1);
And this
Could be
-
Also, I think your screen is probably 80x25, os 79 is the maximim column accessable, not 80:
And 43 . . . I don't know, maybe you're using a larger screen size.
-
Thx for the advice. But gotoxy(80,43) actually works and my monitor is 15 inch!
-
Awrite. Have been able to get it working. But minor glitches remain. I will be back wit em later! Thx all! :-)
-
The monitor screen doesn't have anything to do with it, it's the screen mode. But gotoxy() probably limits it's arguments to 80x25 with %.
-
Code:
void extview(int z, int i){
It seems to me that you're using i in for loops . . . so just declare it locally.
Code:
void extview(void) {
int z, i;
And you're aware, of course, that 0 is a valid time? :)
Code:
time_t start=0, end=0;
This
Code:
z=4;
for(i=0;i<40;i++){
printf("%c", 179);
gotoxy(62,z++);
}
could be
Code:
for(i = 0; i < 40; i ++) {
printf("%c", 179);
gotoxy(62, i+4);
}
And, as you may have discovered in the FAQ, main() should be declared as
-
Well I learnt about the int main thingy from the faq. And time has been set to 0 so that the time keeping starts when the first key is pressed!