#include<iostream>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream>
#include<string.h>
#include<windows.h>
#include<fstream.h>
#include<ctime>


void checking();
void display(double);

enum check{correct,incorrect};

class words
    {
        public:
            char word[20];
            check cword;
            check cchar[20];
    };


void main()
{
    ifstream fin("Book.txt");
    char ch;
    char wrd[20];
    time_t start,end;
    double dif;

    cout<<"\n Enter the text below. Press 0 to finish typing. \n";
    Sleep(100);

    while(!fin.eof())
        {
            fin>>wrd;
            cout<<wrd<<" ";
        }
    cout<<endl;

    fin.close();

    ofstream fout("Typed.txt");

    time (&start);
    while(ch!='0')
        {
            ch=getche();
            if(ch!='0')
                fout<<ch;
        }
    time (&end);

    cout<<"\n Please wait while we process your result. \n";

    dif = difftime (end,start);
    fout.close();
    
    checking();
    display(dif);

    
}

void checking()
{
    words w;
    char wrd[20];

    ifstream fin("Book.txt");
    ifstream type("Typed.txt");
    fstream fout;
    fout.open("Corrected.dat",ios::out);

    int i,j,k,l;

    while(!type.eof())
        {
            fin>>wrd;
            type>>w.word;

            j=0;
            k=0;
            l=strlen(w.word);
            for(i=0;w.word[i]!='\0';i++)
                {
                rajat:
                    if(w.word[i]==wrd[j])
                        w.cchar[i]=correct;
                    else
                        {
                            w.cchar[i]=incorrect;
                            k++;
                            if(i!=l)
                                {
                                    i++;
                                    goto rajat;
                                }
                            else
                                i=j;
                        }
                j++;
                }
            if(k==0)
                w.cword=correct;
            else
                w.cword=incorrect;

            fout.write((char*)&w,sizeof(w));

        }

    fin.close();
    type.close();
    fout.close();
}

void display(double dif)
{

    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

    fstream fin;
    fin.open("Corrected.dat",ios::in);
    words w;
    cout<<"\n You took "<<dif<<" seconds to enter the text.";
    cout<<"\n The typed text with mistakes are highlighted in red. \n";
    while(fin.read((char*)&w,sizeof(w)))
        {
        if(w.cword==correct)
            {
                SetConsoleTextAttribute
                (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                cout<<w.word;
            }
        else
            {
                for(int i=0;w.word[i]!='\0';i++)
                    {
                        if(w.cchar[i]==correct)
                            {
                                SetConsoleTextAttribute
                                (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                                cout<<w.word[i];
                            }

                        else
                            {
                                SetConsoleTextAttribute
                                (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
                                cout<<w.word[i];
                            }
                }
            }
        cout<<" ";
        }

    SetConsoleTextAttribute
    (hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
    cout<<endl;

}

ABOVE IS THE PROJECT TYPING TUTOR WHICH WORKS ABSOLUTELY FINE ON "NETBEANS" COMPILER..

BUT THE PROBLEM I AM FACING IS THAT EACH TIME THE USER HAS TO ENTER 0 AFTER FINISH TYPING.....IS THERE ANY WAY FOR THE USER TO ENTER "ENTER" TO FINISH TYPING....

ALSO....I WANTED TO KNOW THAT ..IS THERE ANY WAY TO REMOVE EXTRA CHARACTERS BY BACKSPACE...IF SUPPOSE SOMEONE ACCIDENTALLY TYPES A FEW EXTRA CHARACTERS, THEN IS THERE ANY WAY TO REMOVE THOSE EXTRA CHARACTERS BY BACKSPACE??? :/

NOW , IN THIS STATE WHENEVER I TYPE BACKSPACE, THE POINTER COMES BACKWARDS AND I CAN ONLY REPLACE THE CHARACTER...NOT REMOVE IT !! THATS THE PROBLEM :(

AND ALSO.... THIS PROJECT IS A BIT SHORT ....COULD YOU PLEASE SUGGEST SOME WAYS TO MAKE IT A BIT LONGER....... PLEASE PLEASE ITS A REQ. :((
ALSO, HOW CAN I ADD THE OPTION TO "CHANGE PASSWORD" IN THE "MENU OPTION"
AND HOW WILL IT SHOW UP IN ASTERISKS... LIKE THIS > ******** WHICH WILL NOT SHOW UP WHEN SOMEONE TYPES THEIR PASSWORD.....
PLEASE HELP ME WITH THESE THINGS.... I SHALL BE REALLY GRATEFUL ONCE AGAIN...

AND THE CURRENT OUTPUT OF THIS PROGRAM YOU CAN SEE HERE >> http://i53.tinypic.com/5kj9u8.png

Recommended Answers

All 2 Replies

IS THERE ANY WAY FOR THE USER TO ENTER "ENTER" TO FINISH TYPING....

Since you're using getche() to read characters, your loop needs to recognize the first part of a newline. Since you're using Windows, that character would be the first part of a CRLF sequence: '\r'.

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

int main()
{
    std::string line;
    int ch;
    
    std::cout << "Enter a line: " << std::flush;
    
    while ((ch = getche()) != '\r')
        line += (char)ch;
        
    std::cout << "\nYou entered '" << line << "'\n";
}

IS THERE ANY WAY TO REMOVE EXTRA CHARACTERS BY BACKSPACE...

Without the shell to help you, this logic needs to be a part of your code. You need to recognize the '\b' character and act accordingly:

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

int main()
{
    std::string line;
    int ch;
    
    std::cout << "Enter a line: " << std::flush;
    
    while ((ch = getch()) != '\r') {
        if (ch == '\b') {
            if (!line.empty()) {
                line.erase(line.end() - 1); // Update the line
                std::cout << "\b \b";       // Update the display
            }
        }
        else {
            line += (char)ch;
            putch(ch);
        }
    }
        
    std::cout << "\nYou entered '" << line << "'\n";
}

Note that special character handling like this requires that you use getch() rather than getche() to properly manage the display of characters as they're typed. Specifically for backspace, you don't want to display a destructive backspace unless the underlying string also has a destructive backspace.

AND HOW WILL IT SHOW UP IN ASTERISKS... LIKE THIS > ********

This falls out of the backspace solution. Rather than displaying the typed character, you simply need to display an asterisk:

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

int main()
{
    std::string line;
    int ch;
    
    std::cout << "Enter a line: " << std::flush;
    
    while ((ch = getch()) != '\r') {
        if (ch == '\b') {
            if (!line.empty()) {
                line.erase(line.end() - 1); // Update the line
                std::cout << "\b \b";       // Update the display
            }
        }
        else {
            line += (char)ch;
            putch('*');
        }
    }
        
    std::cout << "\nYou entered '" << line << "'\n";
}

wow!! thanks a lotttt!!!!! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.