Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Without using vectors, I need to fix this program to test users on the Spanish vocab word for an English word. The voacb 1 .

Without using vectors, I need to fix this program to test users on the Spanish vocab word for an English word. The voacb1.txt has both pairs of words with a space seperating them. This program does not actually quize the user or caculate overall totals for the total number of quizzes taken, quizzes pased, quizzes failed, or total words reviewed throughout all quizzes.
#include
#include
#include
#include
#include
using namespace std;
const int MAX_WORDS =25;
const string VOCAB_FILE = "vocab1.txt";
const string OUTPUT_FILE = "quizSummary.txt";
struct QuizResult {
string userName;
int numQuestions;
int numCorrect;
double grade;
};
int countWordsInFile(){
ifstream file(VOCAB_FILE);
if (!file.is_open()){
cout << "Error: Vocabulary file not found." << endl;
return -1;
}
int count =0;
string line;
while (getline(file, line)){
count++;
}
file.close();
return count;
}
void generateRandomIndices(int indices[], int numWords, int numQuestions){
srand(time(nullptr));
for (int i =0; i < numQuestions; i++){
indices[i]= rand()% numWords;
}
}
void displayQuizSummary(const QuizResult& result){
cout << "Name: "<< result.userName << endl;
cout <<"# of Question in the quiz: "<< result.numQuestions << endl;
cout <<"# of Correct answers: "<< result.numCorrect << endl;
cout << "Your % Grade: "<< result.grade <<"%"<< endl;
cout << "Decision: "<<(result.grade >=75? "PASSED" : "FAILED")<< endl;
}
void writeQuizSummaryToFile(const QuizResult& result){
ofstream outFile(OUTPUT_FILE, ios::app);
if (!outFile.is_open()){
cout << "Error: Unable to open output file." << endl;
return;
}
outFile << "Name: "<< result.userName << endl;
outFile <<"# of Question in the quiz: "<< result.numQuestions << endl;
outFile <<"# of Correct answers: "<< result.numCorrect << endl;
outFile << "Your % Grade: "<< result.grade <<"%"<< endl;
outFile << "Decision: "<<(result.grade >=75? "PASSED" : "FAILED")<< endl;
outFile <<"---------------------------------------------------------"<< endl;
outFile.close();
}
void startQuiz(){
string userName;
int numQuestions;
cout << "Welcome to English-Spanish Class. Please enter your name: ";
getline(cin, userName);
cout << "Maximum available words in the Quiz: "<< MAX_WORDS <<" words." << endl;
int numWords = countWordsInFile();
if (numWords ==-1){
return;
}
cout << "How many questions would you like to be quizzed on?";
cin >> numQuestions;
cin.ignore(); // Clear the input buffer
if (numQuestions <=0|| numQuestions > numWords){
cout << "Invalid number of questions." << endl;
return;
}
int indices[numQuestions];
generateRandomIndices(indices, numWords, numQuestions);
ifstream vocabFile(VOCAB_FILE);
if (!vocabFile.is_open()){
cout << "Error: Vocabulary file not found." << endl;
return;
}
int questionCount =0;
int correctCount =0;
string line;
cout <<"---------------------------------------------------------"<< endl;
cout << "Quiz" << endl;
cout <<"---------------------------------------------------------"<< endl;
while (getline(vocabFile, line) && questionCount < numQuestions){
if (indices[questionCount]== questionCount){
string english, spanish;
english = line.substr(0, line.find(","));
spanish = line.substr(line.find(",")+1);
string userTranslation;
cout << "Question "<<(questionCount +1)<< endl;
cout << "English word: "<< english << endl;
cout << "User answer in Spanish: ";
getline(cin, userTranslation);
if (userTranslation == spanish){
cout << "Result: Correct!" << endl;
correctCount++;
} else {
cout << "Result: Incorrect." << endl;
}
cout <<"---------------------------------------------------------"<< endl;
questionCount++;
}
}
vocabFile.close();
double grade = static_cast(correctCount)/ numQuestions *100;
QuizResult result ={userName, numQuestions, correctCount, grade};
displayQuizSummary(result);
writeQuizSummaryToFile(result);
}
int main(){
char choice;
do {
startQuiz();
cout <<"Do you want to take the quiz again (Y/N): ";
cin >> choice;
cin.ignore(); // Clear the input buffer
} while (choice =='Y'|| choice =='y');
cout << "Thanks for taking the quiz." << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions