include <iostream>
include <fstream>
include <string>
include <stdio.h>
include <stdlib.h>
include <time.h>
//These are what libaries this program needs to function correctly
using namespace std;
//Below are a number of functions that govern what is shown when the program is run.
//The one below governs what is shown when the questions are displayed. In this case it pulls in question number and size. These are declared below the functions.
void displayQuestion(string question[], int size, int questionNumber){ cout << "Question[" << questionNumber << "]=" << question[questionNumber] << endl; }
//The function below governs what is shown when the the question is answered and displays the answer and the answernumber void displayAnswer(string answer[], int size, int answerNumber){
cout << "answer[" << answerNumber << "] = " << answer[answerNumber] << endl;
}
//function that governs what shows up in the scorenumber when that is run void displayScore(string score[], int size, int scoreNumber){
cout << "score[" << scoreNumber << "] = " << score[scoreNumber] << endl;
}
//This line is the intial prompt that shows up when the program is first run and stores the answer in the useranswer varible string enterAnswer(){
string userAnswer;
cout << "Please enter an answer ";
cin >> userAnswer;
return userAnswer;
}
//This function governs what happens when the question is answered. If answer is correct then the program will pring the first line and if not then the second line is printed void answerQuestion(int questionNumber,string question[], string answer[], string score[]){ string userAnswer = enterAnswer(); if (userAnswer == answer[questionNumber]) cout << "answer correct" << score[0] << endl;
else
cout << "wrong answer" << endl;
}
//This function governs what shows up when the program reads from the file that is used by it void readFrom(string filename,int size ,string question[], string answer[], string score[]){ string line; ifstream myfile(filename); if (myfile.is_open()) { for (int i = 0; i < size; i++){ getline(myfile, line, ';'); question[i] = line; getline(myfile, line, ';'); answer[i] = line; getline(myfile, line, ';'); score[i] = line;
}
myfile.close();
}
else cout << "Unable to access file";
}
//Random number generator that will be used to randomise the questions that get picked from the question pool int randomGenerator(int max_value){ srand((unsigned)time(NULL)); rand();
int x;
x = rand() / (RAND_MAX / max_value + 1);
return x;
} //Main program part int main(){
//Varibles that are used in this program
int const SIZE = 9;
string question[SIZE];
string answer[SIZE];
string score[SIZE];
int questionNumber = 0;
int answerNumber = 0;
int scoreNumber = 0;
int randomNumber = 0;
int max_value = 10;
char a, b, level;
//The following is the if statement that gives the user the choice of whether to do the easy test or the medium test which is a and b respectively
cout << "Please enter level of quiz either a or b ";
cin >> level;
if (level == 'a'){
readFrom("example.txt", SIZE, question, answer, score);
for (int i = 0; i < SIZE; i++){
displayQuestion(question, SIZE, randomNumber);
answerQuestion(questionNumber, question, answer, score);
displayAnswer(answer, SIZE, answerNumber);
displayScore(score, SIZE, scoreNumber);
}
If you put you code in code tags with some semblance of formatting someone might help you.
I imagine its something to do with your for loops doing exactly the same thing in every iteration. In other words you are passing/using 'i' variable in any of your loops. Pass I into your functions rather than SIZE? Just a guess because your code it's difficult to read.