Witam, otóż chciałem zrobić program dzięki któremu mógłbym uczyć się języka. Miałoby to polegać na tym, że jest zestaw pytań w pliku txt. Program losuje pytanie, w którym jest krótki kod i losowo wygenerowane miejsce w którym brakuje znaku i zastępuje je znakiem zapytania. Ja wpisuje czego brakuje, program albo mówi dobrze albo źle i daje kolejne pytanie. Wydawało mi się to dobrym pomysłem bo takim, który by mnie czegoś nauczył i takim, na który mnie stać. Jednak nie dałem rady kompletnie chat gpt także nie pomógł a tylko namieszał. Jest problem z wygenerowaniem miejsca w którym znika znak. To co udało mi się sklecić po dobrych 3 godzinach daje niżej, a także plik z pytaniami. Nie wiedziałem co zrobić więc przyszedłem tutaj. Liczę na waszą pomoc na prawdę już nie wiem co mam zrobić. Program:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <random>
using namespace std;
// Structure representing a question and its answer
struct Question {
string question;
string code;
char missingChar;
};
// Function to read questions from a file
vector<Question> readQuestionsFromFile(const string& filename) {
ifstream file(filename);
vector<Question> questions;
if (file.is_open()) {
string line;
while (getline(file, line)) {
size_t pos = line.find("|");
if (pos != string::npos && pos > 0 && pos < line.length() - 1) {
Question q;
q.question = line.substr(0, pos);
q.code = line.substr(pos + 1);
questions.push_back(q);
}
}
file.close();
} else {
cerr << "Unable to open the file " << filename << endl;
}
return questions;
}
// Function to replace a random non-space character with '?'
void replaceRandomNonSpaceWithQuestionMark(Question& q) {
// Find all non-space characters
vector<size_t> nonSpaceIndices;
for (size_t i = 0; i < q.code.length(); ++i) {
if (q.code[i] != ' ') {
nonSpaceIndices.push_back(i);
}
}
if (!nonSpaceIndices.empty()) {
// Choose a random index
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, nonSpaceIndices.size() - 1);
size_t randomIndex = nonSpaceIndices[dis(gen)];
// Replace the character at the random index with '?'
q.missingChar = q.code[randomIndex];
q.code[randomIndex] = '?';
}
}
int main() {
const string questionsFile = "pytania.txt";
vector<Question> questions = readQuestionsFromFile(questionsFile);
if (questions.empty()) {
cout << "Brak pytań. Sprawdź plik " << questionsFile << endl;
return 1;
}
while (true) {
for (size_t i = 0; i < questions.size(); ++i) {
// Display the question
cout << "Pytanie: " << questions[i].question << endl;
// Display the code
cout << "Kod: " << questions[i].code << endl;
// Replace a random non-space character with '?'
replaceRandomNonSpaceWithQuestionMark(questions[i]);
// Display the code with the missing character
cout << "Kod (z brakującym znakiem): " << questions[i].code << endl;
// Get user's answer
char userAnswer;
cout << "Czego brakuje w kodzie? ";
cin >> userAnswer;
// Check user's answer
if (userAnswer == questions[i].missingChar) {
cout << "Dobrze!" << endl;
} else {
cout << "Źle. Poprawna odpowiedź to: " << questions[i].missingChar << endl;
}
// Wait for the next question
cout << "Naciśnij Enter, aby przejść do kolejnego pytania...";
cin.ignore(); // Ignore extra Enter
cin.get(); // Wait for Enter
}
}
return 0;
}
Plik z odpowiedziami:
obliczanie kwadratu liczby int a; a = a*a; cout a;
wybieranie losowej liczby z zakresu srand(time(NULL)); int randomNumber = rand() % range + minNumber; cout randomNumber;
obliczanie sumy elementów tablic |int sum = 0; for(int i = 0; i < size; i++) { sum += array[i]; } cout sum;