Text Practice Mode
Try this out
created Saturday July 19, 08:57 by Anshul Kumar
2
819 words
2 completed
0
Rating visible after 3 or more votes
saving score / loading statistics ...
00:00
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
// ========= COLORS ==========
void printColor(const string &text, const string &color) {
if (color == "blue") cout << "\033[1;34m" << text << "\033[0m";
else if (color == "green") cout << "\033[1;32m" << text << "\033[0m";
else if (color == "yellow") cout << "\033[1;33m" << text << "\033[0m";
else if (color == "red") cout << "\033[1;31m" << text << "\033[0m";
else if (color == "cyan") cout << "\033[1;36m" << text << "\033[0m";
else cout << text;
}
// ========= SCORE SYSTEM =========
void saveHighScore(double score) {
ofstream file("highscore.txt", ios::app);
file << score << "\n";
file.close();
}
double getHighScore() {
ifstream file("highscore.txt");
double score, maxScore = 0;
while (file >> score) {
if (score > maxScore) maxScore = score;
}
return maxScore;
}
// ========= CODE SNIPPET STRUCTURE =========
struct CodeSnippet {
string heading;
string explanation;
vector<string> codeLines;
vector<string> lineExplain;
};
// ========= WORD POOL =========
class WordPool {
public:
vector<string> words;
WordPool() {
words = {
"int","float","double","if","else","for","while","class",
"public","private","protected","virtual","override","friend",
"+","-","*","/","%","&&","||","==","!=","<",">",
"{","}","(",")","[","]","::","->","<<",">>", ";",
"return","new","delete","namespace","using","std","template",
"vector","map","set","queue","stack","list","pair","tuple",
// ... Expand this to 1000 coding words/symbols
};
}
string getRandomWord() {
return words[rand() % words.size()];
}
};
// ========= LEARNING TUTOR =========
class LearningTutor {
vector<CodeSnippet> snippets;
public:
LearningTutor() {
addArrayExample();
addLoopExample();
addFunctionExample();
// TODO: Add 20+ more snippets (OOP, Pointers, Recursion, etc.)
}
void addArrayExample() {
CodeSnippet arr;
arr.heading = "ARRAY EXAMPLE";
arr.explanation = "Is code me hum ek array banate hain aur uski values print karte hain.";
arr.codeLines = {
"int arr[5] = {1, 2, 3, 4, 5};",
"for (int i = 0; i < 5; i++) {",
" cout << arr[i] << endl;",
"}"
};
arr.lineExplain = {
"Array declare aur initialize ho raha hai.",
"Loop start hota hai, i=0 se i<5 tak.",
"Array ke har element ko print kar rahe hain.",
"Loop khatam hota hai."
};
snippets.push_back(arr);
}
void addLoopExample() {
CodeSnippet loop;
loop.heading = "LOOP EXAMPLE";
loop.explanation = "Is code me ek simple for loop likha hai jo numbers print karta hai.";
loop.codeLines = {
"for (int i = 1; i <= 10; i++) {",
" cout << i << \" \";",
"}"
};
loop.lineExplain = {
"Loop 1 se 10 tak chalega.",
"Har i ki value print ho rahi hai.",
"Loop block close hota hai."
};
snippets.push_back(loop);
}
void addFunctionExample() {
CodeSnippet func;
func.heading = "FUNCTION EXAMPLE";
func.explanation = "Is code me ek function banate hain jo do numbers ka sum return karta hai.";
func.codeLines = {
"int add(int a, int b) {",
" return a + b;",
"}"
};
func.lineExplain = {
"Function add declare hota hai jo 2 parameters leta hai.",
"Return statement a+b return kar raha hai.",
"Function ka end."
};
snippets.push_back(func);
}
void learnMode() {
for (auto &snip : snippets) {
cout << "\n";
printColor("[ " + snip.heading + " ]\n", "blue");
printColor("→ " + snip.explanation + "\n\n", "green");
int correctChars = 0, totalChars = 0;
time_t start = time(0);
for (size_t i = 0; i < snip.codeLines.size(); i++) {
printColor(snip.codeLines[i] + "\n", "yellow");
printColor(" Explanation: " + snip.lineExplain[i] + "\n", "green");
cout << "Type this line:\n> ";
string input;
getline(cin, input);
totalChars += snip.codeLines[i].size();
for (size_t j = 0; j < min(input.size(), snip.codeLines[i].size()); j++) {
if (input[j] == snip.codeLines[i][j]) correctChars++;
}
if (input == snip.codeLines[i])
printColor("✅ Correct!\n", "green");
else
printColor("❌ Error! Expected: " + snip.codeLines[i] + "\n", "red");
}
time_t end = time(0);
double timeTaken = difftime(end, start);
double wpm = (correctChars / 5.0) / (timeTaken / 60.0);
double accuracy = (correctChars * 100.0) / totalChars;
printColor("\n=== RESULT ===\n", "cyan");
cout << "Speed: " << fixed << setprecision(2) << wpm << " WPM\n";
cout << "Accuracy: " << fixed << setprecision(2) << accuracy << "%\n";
saveHighScore(wpm);
}
}
void typingChallenge(WordPool &pool, int count = 50) {
printColor("\n=== TYPING CHALLENGE ===\n", "blue");
int correct = 0;
string input;
time_t start = time(0);
for (int i = 0; i < count; i++) {
string word = pool.getRandomWord();
cout << "Word " << i + 1 << ": " << word << "\n> ";
cin >> input;
if (input == word) correct++;
}
time_t end = time(0);
double timeTaken = difftime(end, start);
double wpm = (correct / timeTaken) * 60;
printColor("\nResult: ", "cyan");
cout << correct << "/" << count << " correct. Speed: " << wpm << " WPM.\n";
saveHighScore(wpm);
}
};
// ========= MAIN =========
int main() {
srand(time(0));
WordPool pool;
LearningTutor tutor;
while (true) {
printColor("\n=== C++ TYPING MASTER ===\n", "cyan");
cout << "1. Learn Mode (20+ Examples)\n";
cout << "2. Typing Challenge (1000 Words)\n";
cout << "3. View High Score\n";
cout << "4. Exit\n";
cout << "Choose: ";
int choice;
cin >> choice;
cin.ignore();
if (choice == 1) tutor.learnMode();
else if (choice == 2) tutor.typingChallenge(pool, 50);
else if (choice == 3) cout << "High Score: " << getHighScore() << " WPM\n";
else if (choice == 4) break;
else cout << "Invalid choice.\n";
}
return 0;
}
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
// ========= COLORS ==========
void printColor(const string &text, const string &color) {
if (color == "blue") cout << "\033[1;34m" << text << "\033[0m";
else if (color == "green") cout << "\033[1;32m" << text << "\033[0m";
else if (color == "yellow") cout << "\033[1;33m" << text << "\033[0m";
else if (color == "red") cout << "\033[1;31m" << text << "\033[0m";
else if (color == "cyan") cout << "\033[1;36m" << text << "\033[0m";
else cout << text;
}
// ========= SCORE SYSTEM =========
void saveHighScore(double score) {
ofstream file("highscore.txt", ios::app);
file << score << "\n";
file.close();
}
double getHighScore() {
ifstream file("highscore.txt");
double score, maxScore = 0;
while (file >> score) {
if (score > maxScore) maxScore = score;
}
return maxScore;
}
// ========= CODE SNIPPET STRUCTURE =========
struct CodeSnippet {
string heading;
string explanation;
vector<string> codeLines;
vector<string> lineExplain;
};
// ========= WORD POOL =========
class WordPool {
public:
vector<string> words;
WordPool() {
words = {
"int","float","double","if","else","for","while","class",
"public","private","protected","virtual","override","friend",
"+","-","*","/","%","&&","||","==","!=","<",">",
"{","}","(",")","[","]","::","->","<<",">>", ";",
"return","new","delete","namespace","using","std","template",
"vector","map","set","queue","stack","list","pair","tuple",
// ... Expand this to 1000 coding words/symbols
};
}
string getRandomWord() {
return words[rand() % words.size()];
}
};
// ========= LEARNING TUTOR =========
class LearningTutor {
vector<CodeSnippet> snippets;
public:
LearningTutor() {
addArrayExample();
addLoopExample();
addFunctionExample();
// TODO: Add 20+ more snippets (OOP, Pointers, Recursion, etc.)
}
void addArrayExample() {
CodeSnippet arr;
arr.heading = "ARRAY EXAMPLE";
arr.explanation = "Is code me hum ek array banate hain aur uski values print karte hain.";
arr.codeLines = {
"int arr[5] = {1, 2, 3, 4, 5};",
"for (int i = 0; i < 5; i++) {",
" cout << arr[i] << endl;",
"}"
};
arr.lineExplain = {
"Array declare aur initialize ho raha hai.",
"Loop start hota hai, i=0 se i<5 tak.",
"Array ke har element ko print kar rahe hain.",
"Loop khatam hota hai."
};
snippets.push_back(arr);
}
void addLoopExample() {
CodeSnippet loop;
loop.heading = "LOOP EXAMPLE";
loop.explanation = "Is code me ek simple for loop likha hai jo numbers print karta hai.";
loop.codeLines = {
"for (int i = 1; i <= 10; i++) {",
" cout << i << \" \";",
"}"
};
loop.lineExplain = {
"Loop 1 se 10 tak chalega.",
"Har i ki value print ho rahi hai.",
"Loop block close hota hai."
};
snippets.push_back(loop);
}
void addFunctionExample() {
CodeSnippet func;
func.heading = "FUNCTION EXAMPLE";
func.explanation = "Is code me ek function banate hain jo do numbers ka sum return karta hai.";
func.codeLines = {
"int add(int a, int b) {",
" return a + b;",
"}"
};
func.lineExplain = {
"Function add declare hota hai jo 2 parameters leta hai.",
"Return statement a+b return kar raha hai.",
"Function ka end."
};
snippets.push_back(func);
}
void learnMode() {
for (auto &snip : snippets) {
cout << "\n";
printColor("[ " + snip.heading + " ]\n", "blue");
printColor("→ " + snip.explanation + "\n\n", "green");
int correctChars = 0, totalChars = 0;
time_t start = time(0);
for (size_t i = 0; i < snip.codeLines.size(); i++) {
printColor(snip.codeLines[i] + "\n", "yellow");
printColor(" Explanation: " + snip.lineExplain[i] + "\n", "green");
cout << "Type this line:\n> ";
string input;
getline(cin, input);
totalChars += snip.codeLines[i].size();
for (size_t j = 0; j < min(input.size(), snip.codeLines[i].size()); j++) {
if (input[j] == snip.codeLines[i][j]) correctChars++;
}
if (input == snip.codeLines[i])
printColor("✅ Correct!\n", "green");
else
printColor("❌ Error! Expected: " + snip.codeLines[i] + "\n", "red");
}
time_t end = time(0);
double timeTaken = difftime(end, start);
double wpm = (correctChars / 5.0) / (timeTaken / 60.0);
double accuracy = (correctChars * 100.0) / totalChars;
printColor("\n=== RESULT ===\n", "cyan");
cout << "Speed: " << fixed << setprecision(2) << wpm << " WPM\n";
cout << "Accuracy: " << fixed << setprecision(2) << accuracy << "%\n";
saveHighScore(wpm);
}
}
void typingChallenge(WordPool &pool, int count = 50) {
printColor("\n=== TYPING CHALLENGE ===\n", "blue");
int correct = 0;
string input;
time_t start = time(0);
for (int i = 0; i < count; i++) {
string word = pool.getRandomWord();
cout << "Word " << i + 1 << ": " << word << "\n> ";
cin >> input;
if (input == word) correct++;
}
time_t end = time(0);
double timeTaken = difftime(end, start);
double wpm = (correct / timeTaken) * 60;
printColor("\nResult: ", "cyan");
cout << correct << "/" << count << " correct. Speed: " << wpm << " WPM.\n";
saveHighScore(wpm);
}
};
// ========= MAIN =========
int main() {
srand(time(0));
WordPool pool;
LearningTutor tutor;
while (true) {
printColor("\n=== C++ TYPING MASTER ===\n", "cyan");
cout << "1. Learn Mode (20+ Examples)\n";
cout << "2. Typing Challenge (1000 Words)\n";
cout << "3. View High Score\n";
cout << "4. Exit\n";
cout << "Choose: ";
int choice;
cin >> choice;
cin.ignore();
if (choice == 1) tutor.learnMode();
else if (choice == 2) tutor.typingChallenge(pool, 50);
else if (choice == 3) cout << "High Score: " << getHighScore() << " WPM\n";
else if (choice == 4) break;
else cout << "Invalid choice.\n";
}
return 0;
}
